@gigster/module-loopback-elasticsearch
v2.0.0
Published
Role | Name | Email | Slack ---- | ---- | ----- | ----- *Product Owner* | Frances Haugen | [[email protected]](mailto:[email protected]) | [@frances] *Maintainer* | Alice Wang | [[email protected]](mailto:[email protected]) | [@alicebot] *Developer* |
Downloads
62
Readme
loopback-elasticsearch
Role | Name | Email | Slack ---- | ---- | ----- | ----- Product Owner | Frances Haugen | [email protected] | [@frances] Maintainer | Alice Wang | [email protected] | [@alicebot] Developer | Jerome Curlier | [email protected] | [@jerome]
Why Use ElasticSearch?
Traditional relational databases excel at precise and exact matches. While ElasticSearch can perform exact matching and statistical calculations, its strength lies in an approximation task based on scores of its search results.
Taken from the following Stackexchange answer:
The following examples highlight some good use cases for ElasticSearch:
- Searching a large number of product descriptions for the best match for a specific phrase (ie "chef’s knife") and returning the most relevant results
- Given the previous example, breaking down the various departments where "chef’s knife" appears
- Searching text for words that sound like "season"
- Auto-completing a search box based on partially typed words based on previously issued searches, accounting for misspellings
- Storing a large quantity of semi-structured (JSON) data in a distributed fashion, with a specified level of redundancy across a cluster of machines (multitenancy)
Not so good candidates for ElasticSearch:
- Calculating how many items are left in the inventory
- Figuring out the sum of all line-items on all the invoices sent out in a given month
- Executing two operations transactionally with rollback support
- Creating records that are guaranteed to be unique across multiple given terms, for instance a phone number and extension
Overview
This module provides elastic search for Loopback.
The module is based on the blog post Integrating LoopBack with ElasticSearch. Please therefore review the post to learn about this module.
It consumes the ElasticSearch API through the JavaScript client: ElasticSearch API
Usage
- name: loopback-elasticsearch
location: 'npm:@gigster/[email protected]'
spec:
models:
- name: user
- name: expense
Specification
Name | Status ---- | ----- models | List of models to apply the search to.
Endpoints
The following endpoints are provided for the search.
Endpoint | Method | Description ---- | ---- | ----- POST /model/search | search | Performs a search on the model
Examples
NYT Article Search
Search over 20 NYT articles.
Dependencies
The loopback-elasticsearch depends on the loopback-models module.
The following npm packages are used:
npm | version ---- | ----- elasticsearch | ^13.3.1
Environment variables
Name | Description ---- | ----- ESHOST | Elastic search host, for example https://497d894184dd5958f9b477a5982fb1cf.ap-southeast-1.aws.found.io:9243 ESAUTH | Elastic search password
Tests
Module tests are defined using a test/scenarios.yaml
file. This file defines the set of example gigs that we generate as part of integration testing. To run all tests, run yarn test
at the root of this module.
Each scenario is generated in test/scenario/<name>
which you can then cd
into and run the actual app. For a scenario called default
, this is done via:
cd test/scenario/default
yarn install
# Run tests.
yarn test
# Start the app.
yarn start
Docker
docker pull docker.elastic.co/elasticsearch/elasticsearch:5.6.3
docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:5.6.3
Then start the applccation with the default password.
ESAUTH=elastic:changeme npm start
Generation
Models
For each model, the module contributes a search endpoint. The implementation of the search is provided as a method on the model in a file search.js in the model folder.
Helper
The module contribute the elasticsearch.js helper to connect to Elasticsearh.
Troubleshooting
DEBUG=gdt:loopback:elasticsearch npm start
TODO
- Use mixins instead of generating the search method for each model