@fairwords/loopback-connector-es 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +18 -0
- package/CHANGELOG.md +66 -0
- package/Jenkinsfile +7 -0
- package/LICENCE +19 -0
- package/README.md +430 -0
- package/cacert.pem +141 -0
- package/docker-compose-for-testing-all.yml +22 -0
- package/docker-compose-for-testing-v1.yml +11 -0
- package/docker-compose-for-testing-v2.yml +11 -0
- package/docker-compose-for-testing-v5.yml +8 -0
- package/docker-compose.yml +20 -0
- package/docker-entrypoint-es1-plugins.sh +10 -0
- package/docker-entrypoint-es2-plugins.sh +21 -0
- package/index.js +1 -0
- package/lib/automigrate.js +75 -0
- package/lib/esConnector.js +1357 -0
- package/lib/setupIndex.js +57 -0
- package/lib/setupMapping.js +70 -0
- package/lib/setupMappings.js +44 -0
- package/package.json +54 -0
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
'extends': 'eslint:recommended'
|
|
3
|
+
,'rules': {
|
|
4
|
+
'indent': ['error', 2]
|
|
5
|
+
}
|
|
6
|
+
,'env': {
|
|
7
|
+
'mocha': true
|
|
8
|
+
,'node': true
|
|
9
|
+
},
|
|
10
|
+
'globals': {
|
|
11
|
+
'expect': true,
|
|
12
|
+
'getDataSource': true,
|
|
13
|
+
'should': true,
|
|
14
|
+
'assert': true,
|
|
15
|
+
'getSchema': true,
|
|
16
|
+
'getSettings': true
|
|
17
|
+
}
|
|
18
|
+
};
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
### v1.4.1 - Jun 6, 2017
|
|
2
|
+
- Contributed by @AhsanAyaz
|
|
3
|
+
- Fixed Bug: when `and` was nested inside `or`, the ES query was built incorrectly (#93, #99 and #100)
|
|
4
|
+
- Many thanks to @aquid for jumping in multiple times and offering valuable advice during code reviews.
|
|
5
|
+
|
|
6
|
+
### v1.4.0 - Apr 23, 2017
|
|
7
|
+
- contributed by @wolfgang-s
|
|
8
|
+
- add support for ES 5.x (PR #87 and PR #89)
|
|
9
|
+
|
|
10
|
+
### v1.3.5 - March 7, 2017
|
|
11
|
+
- Fixed Issue - Remove refresh option for unsupported methods
|
|
12
|
+
- This issue was fixed in PR #86
|
|
13
|
+
|
|
14
|
+
### v1.3.4 - Feb 25, 2017
|
|
15
|
+
- Fixed Issue #79 - Wait until the document is ready for searching
|
|
16
|
+
- this issue was fixed in PR #81
|
|
17
|
+
- Fixed Issue #85 - Update All for es2.3 and above
|
|
18
|
+
- this issue was fixed in PR #84
|
|
19
|
+
|
|
20
|
+
### v1.3.3 - Feb 23, 2017
|
|
21
|
+
- Fixed Issue #79 - Multiple where clause filter support without `and` filter
|
|
22
|
+
- this issue was fixed in PR #78
|
|
23
|
+
- Fixed Issue #73 - Nested and, or filter support added
|
|
24
|
+
- this issue was fixed in PR #75
|
|
25
|
+
- Fixed Issue #73 - `inq`,`nin`,`between`,`neq` filter support added
|
|
26
|
+
- this issue was fixed in PR #75
|
|
27
|
+
- Fixed Issue #28 - Include filter added
|
|
28
|
+
- Minimum workflow to make include filter work until we optimize it for better performance.
|
|
29
|
+
This issue was fixed in PR #71
|
|
30
|
+
|
|
31
|
+
### v1.3.2 - Jan 31, 2017
|
|
32
|
+
- Fixed Issue #64 - Date strings were not returned as Javascript Data objects
|
|
33
|
+
- this issue was fixed in PR #68
|
|
34
|
+
- Fixed Issue #37 - MakeId refactoring applied for save method
|
|
35
|
+
- Save method still had an old implementation of makeId method which was updated with
|
|
36
|
+
getDocumentId method. This issue was fixed in PR #69
|
|
37
|
+
- Fixed Issue - Objects inside an array were returned as strings
|
|
38
|
+
- This issue was fixed in PR #70
|
|
39
|
+
|
|
40
|
+
### v1.3.1 - Dec 02, 2016
|
|
41
|
+
- Fixed Regression - analyzers for index weren't being created anymore
|
|
42
|
+
- originally this was added in #25 and got lost somewhere along the way
|
|
43
|
+
|
|
44
|
+
### v1.3.0 - Oct 17, 2016
|
|
45
|
+
- contributed by @dtomasi
|
|
46
|
+
- Issue #57: Fix for defaultSize setting
|
|
47
|
+
- Issue #55: Support Signed Requests for AWS Elasticsearch-Service
|
|
48
|
+
|
|
49
|
+
### v1.2.0 - Sep 25, 2016
|
|
50
|
+
- Add eslint infrastructure
|
|
51
|
+
- `npm install --save-dev eslint@2.13.1`
|
|
52
|
+
- ESLint v3.0.0 now requires Node.js 4 or higher. If you still need ESLint to run on Node.js < 4, then we recommend staying with ESLint v2.13.1 until you are ready to upgrade your Node.js version.
|
|
53
|
+
- https://github.com/strongloop/loopback-contributor-docs/blob/master/eslint-guide.md
|
|
54
|
+
- https://github.com/strongloop/eslint-config-loopback
|
|
55
|
+
|
|
56
|
+
### v1.1.0 - Sep 25, 2016
|
|
57
|
+
- Fixed Issue #52
|
|
58
|
+
- Multi Index usage
|
|
59
|
+
- any model specific indices or mappings should be setup when the connector is initialized
|
|
60
|
+
- mappings array in datasource.<env>.json may contain the index and type properties, which will be used to setup that model's index and mappings during connector initialization
|
|
61
|
+
|
|
62
|
+
### v1.0.8 - Sep 21, 2016
|
|
63
|
+
- Fixed Issue #51
|
|
64
|
+
|
|
65
|
+
### v1.0.7 - Aug 11, 2016
|
|
66
|
+
- Fixed Issue #45
|
package/Jenkinsfile
ADDED
package/LICENCE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
MIT license
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
# loopback-connector-elastic-search
|
|
2
|
+
|
|
3
|
+
[](https://gitter.im/strongloop-community/loopback-connector-elastic-search?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
|
4
|
+
|
|
5
|
+
Basic Elasticsearch datasource connector for [Loopback](http://strongloop.com/node-js/loopback/).
|
|
6
|
+
|
|
7
|
+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
|
8
|
+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
|
9
|
+
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*
|
|
10
|
+
|
|
11
|
+
- [Overview](#overview)
|
|
12
|
+
- [Install this connector in your loopback app](#install-this-connector-in-your-loopback-app)
|
|
13
|
+
- [Configuring connector](#configuring-connector)
|
|
14
|
+
- [Required properties](#required)
|
|
15
|
+
- [Recommended properties](#recommended)
|
|
16
|
+
- [Optional properties](#optional)
|
|
17
|
+
- [Sample for copy paste](#sample)
|
|
18
|
+
- [About the example app](#about-the-example-app)
|
|
19
|
+
- [Run both example and ES in docker](#run-both-example-and-es-in-docker)
|
|
20
|
+
- [Run example locally and ES in docker](#run-example-locally-and-es-in-docker)
|
|
21
|
+
- [Run example locally](#run-example-locally)
|
|
22
|
+
- [How to achieve Instant search](#how-to-achieve-instant-search)
|
|
23
|
+
- [Troubleshooting](#troubleshooting)
|
|
24
|
+
- [Testing](#testing)
|
|
25
|
+
- [Contributing](#contributing)
|
|
26
|
+
- [Frequently Asked Questions](#faqs)
|
|
27
|
+
- [Release notes](#release-notes)
|
|
28
|
+
|
|
29
|
+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
30
|
+
|
|
31
|
+
## Overview
|
|
32
|
+
|
|
33
|
+
1. `lib` directory has the entire source code for this connector
|
|
34
|
+
1. this is what gets downloaded to your `node_modules` folder when you run `npm install loopback-connector-es --save --save-exact`
|
|
35
|
+
1. `examples` directory has a loopback app which uses this connector
|
|
36
|
+
1. this is not published to NPM, it is only here for demo purposes
|
|
37
|
+
1. it will not be downloaded to your `node_modules` folder!
|
|
38
|
+
1. similarly the `examples/server/datasources.json` and `examples/server/datasources.<env>.js` files are there for this demo app to use
|
|
39
|
+
1. you can copy their content over to `<yourApp>/server/datasources.json` or `<yourApp>/server/datasources.<env>.js` if you want and edit it there but don't start editing the files inside `examples/server` itself and expect changes to take place in your app!
|
|
40
|
+
1. `test` directory has unit tests
|
|
41
|
+
1. it does not reuse the loopback app from the `examples` folder
|
|
42
|
+
1. instead, loopback and ES/datasource are built and injected programatically
|
|
43
|
+
1. this directory is not published to NPM.
|
|
44
|
+
1. Refer to `.npmignore` if you're still confused about what's part of the *published* connector and what's not.
|
|
45
|
+
1. You will find the `datasources.json` files in this repo mention various configurations:
|
|
46
|
+
1. `elasticsearch-ssl`
|
|
47
|
+
2. `elasticsearch-plain`
|
|
48
|
+
3. `db`
|
|
49
|
+
4. You don't need them all! They are just examples to help you see the various ways in which you can configure a datasource. Delete the ones you don't need and keep the one you want. For example, most people will start off with `elasticsearch-plain` and then move on to configuring the additional properties that are exemplified in `elasticsearch-ssl`. You can mix & match if you'd like to have mongo and es and memory, all three! These are basics of the "connector" framework in loooback and not something we added.
|
|
50
|
+
1. Don't forget to edit your `model-config.json` file and point the models at the `dataSource` you want to use.
|
|
51
|
+
|
|
52
|
+
## Install this connector in your loopback app
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
cd <yourApp>
|
|
56
|
+
npm install loopback-connector-es --save --save-exact
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Configuring connector
|
|
60
|
+
|
|
61
|
+
### Required:
|
|
62
|
+
- **host:** Elasticsearch engine host address.
|
|
63
|
+
- **port:** Elasticsearch engine port.
|
|
64
|
+
- **name:** Connector name.
|
|
65
|
+
- **connector:** Elasticsearch driver.
|
|
66
|
+
- **index:** Search engine specific index.
|
|
67
|
+
- **apiVersion:** specify the major version of the Elasticsearch nodes you will be connecting to.
|
|
68
|
+
|
|
69
|
+
### Recommended:
|
|
70
|
+
- **mappings:** an array of elasticsearch mappings for your various loopback models.
|
|
71
|
+
- if your models are spread out across different indexes then you can provide an additional `index` field as an override for your model
|
|
72
|
+
- if you don't want to use `type:ModelName` by default, then you can provide an additional `type` field as an override for your model
|
|
73
|
+
|
|
74
|
+
### Optional:
|
|
75
|
+
- **log:** sets elasticsearch client's logging, you can refer to the docs [here](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/configuration.html#config-log)
|
|
76
|
+
- **defaultSize:** total number of results to return per page.
|
|
77
|
+
- **refreshOn** optional array with method names you want to set refresh option as true
|
|
78
|
+
- **requestTimeout:** this value is in milliseconds
|
|
79
|
+
- **ssl:** useful for setting up a secure channel
|
|
80
|
+
- **protocol:** can be `http` or `https` (`http` is the default if none specified) ... *must* be `https` if you're using `ssl`
|
|
81
|
+
- **auth**: useful if you have access control setup via services like `es-jetty` or `found` or `shield`
|
|
82
|
+
- **amazonES**: configuration for `http-aws-es` NOTE: The package needs to be installed in your project. Its not part of this Connector.
|
|
83
|
+
|
|
84
|
+
### Sample:
|
|
85
|
+
1. Edit **datasources.json** and set:
|
|
86
|
+
|
|
87
|
+
```
|
|
88
|
+
"db": {
|
|
89
|
+
"connector": "es",
|
|
90
|
+
"name": "<name>",
|
|
91
|
+
"index": "<index>",
|
|
92
|
+
"hosts": [
|
|
93
|
+
{
|
|
94
|
+
"protocol": "http",
|
|
95
|
+
"host": "127.0.0.1",
|
|
96
|
+
"port": 9200,
|
|
97
|
+
"auth": "username:password"
|
|
98
|
+
}
|
|
99
|
+
],
|
|
100
|
+
"apiVersion": "<apiVersion>",
|
|
101
|
+
"refreshOn": ["save","create", "updateOrCreate"],
|
|
102
|
+
"log": "trace",
|
|
103
|
+
"defaultSize": <defaultSize>,
|
|
104
|
+
"requestTimeout": 30000,
|
|
105
|
+
"ssl": {
|
|
106
|
+
"ca": "./../cacert.pem",
|
|
107
|
+
"rejectUnauthorized": true
|
|
108
|
+
},
|
|
109
|
+
"amazonES": {
|
|
110
|
+
"region": "us-east-1",
|
|
111
|
+
"accessKey": "AKID",
|
|
112
|
+
"secretKey": "secret"
|
|
113
|
+
},
|
|
114
|
+
"mappings": [
|
|
115
|
+
{
|
|
116
|
+
"name": "UserModel",
|
|
117
|
+
"properties": {
|
|
118
|
+
"realm": {"type": "string", "index" : "not_analyzed" },
|
|
119
|
+
"username": {"type": "string", "index" : "not_analyzed" },
|
|
120
|
+
"password": {"type": "string", "index" : "not_analyzed" },
|
|
121
|
+
"email": {"type": "string", "analyzer" : "email" }
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
"name": "CoolModel",
|
|
126
|
+
"index": <useSomeOtherIndex>,
|
|
127
|
+
"type": <overrideTypeName>,
|
|
128
|
+
"properties": {
|
|
129
|
+
"realm": {"type": "string", "index" : "not_analyzed" },
|
|
130
|
+
"username": {"type": "string", "index" : "not_analyzed" },
|
|
131
|
+
"password": {"type": "string", "index" : "not_analyzed" },
|
|
132
|
+
"email": {"type": "string", "analyzer" : "email" }
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
],
|
|
136
|
+
"settings": {
|
|
137
|
+
"analysis": {
|
|
138
|
+
"filter": {
|
|
139
|
+
"email": {
|
|
140
|
+
"type": "pattern_capture",
|
|
141
|
+
"preserve_original": 1,
|
|
142
|
+
"patterns": [
|
|
143
|
+
"([^@]+)",
|
|
144
|
+
"(\\p{L}+)",
|
|
145
|
+
"(\\d+)",
|
|
146
|
+
"@(.+)"
|
|
147
|
+
]
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
"analyzer": {
|
|
151
|
+
"email": {
|
|
152
|
+
"tokenizer": "uax_url_email",
|
|
153
|
+
"filter": ["email", "lowercase", "unique"]
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
2. You can peek at `/examples/server/datasources.json` for more hints.
|
|
161
|
+
|
|
162
|
+
## About the example app
|
|
163
|
+
|
|
164
|
+
1. The `examples` directory contains a loopback app which uses this connector.
|
|
165
|
+
1. You can point this example at your own elasticsearch instance or use the quick instances provided via docker.
|
|
166
|
+
|
|
167
|
+
### Run both example and ES in docker
|
|
168
|
+
|
|
169
|
+
As a developer, you may want a short lived ES instance that is easy to tear down when you're finished dev testing. We recommend docker to facilitate this.
|
|
170
|
+
|
|
171
|
+
**Pre-requisites**
|
|
172
|
+
You will need [docker-engine](https://docs.docker.com/engine/installation/) and [docker-compose](https://docs.docker.com/compose/install/) installed on your system.
|
|
173
|
+
|
|
174
|
+
**Step-1**
|
|
175
|
+
- Set desired versions for **node** and **Elasticsearch**
|
|
176
|
+
- here are the [valid values](https://hub.docker.com/r/library/node/tags/) to use for **Node**
|
|
177
|
+
- here are the [valid values](https://hub.docker.com/r/library/elasticsearch/tags/) to use for **Elasticsearch**
|
|
178
|
+
```
|
|
179
|
+
# combination of node v0.10.46 with elasticsearch v1
|
|
180
|
+
export NODE_VERSION=0.10.46
|
|
181
|
+
export ES_VERSION=1
|
|
182
|
+
echo 'NODE_VERSION' $NODE_VERSION && echo 'ES_VERSION' $ES_VERSION
|
|
183
|
+
|
|
184
|
+
# similarly feel free to try relevant combinations:
|
|
185
|
+
## of node v0.10.46 with elasticsearch v2
|
|
186
|
+
## of node v0.12 with elasticsearch v2
|
|
187
|
+
## of node v0.4 with elasticsearch v2
|
|
188
|
+
## of node v5 with elasticsearch v2
|
|
189
|
+
## elasticsearch v5 will probably not work as there isn't an `elasticsearch` client for it, as of this writing
|
|
190
|
+
## etc.
|
|
191
|
+
```
|
|
192
|
+
**Step-2**
|
|
193
|
+
- Run the setup with `docker-compose` commands.
|
|
194
|
+
|
|
195
|
+
```
|
|
196
|
+
git clone https://github.com/strongloop-community/loopback-connector-elastic-search.git myEsConnector
|
|
197
|
+
cd myEsConnector/examples
|
|
198
|
+
npm install
|
|
199
|
+
docker-compose up
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
**Step-3**
|
|
203
|
+
- Visit `localhost:3000/explorer` and you will find our example loopback app running there.
|
|
204
|
+
|
|
205
|
+
### Run example locally and ES in docker
|
|
206
|
+
|
|
207
|
+
1. Empty out `examples/server/datasources.json` so that it only has the following content remaining: `{}`
|
|
208
|
+
1. Set the `NODE_ENV` environment variable on your local/host machine
|
|
209
|
+
1. Set the environment variable `NODE_ENV=sample-es-plain-1` if you want to use `examples/server/datasources.sample-es-plain-1.js`
|
|
210
|
+
1. Set the environment variable `NODE_ENV=sample-es-plain-2` if you want to use `examples/server/datasources.sample-es-plain-2.js`
|
|
211
|
+
1. Set the environment variable `NODE_ENV=sample-es-ssl-1` if you want to use `examples/server/datasources.sample-es-ssl-1.js`
|
|
212
|
+
1. a sample docker instance for this hasn't been configured yet, so it doesn't work out-of-the-box, use it only as readable (not runnable) reference material for now
|
|
213
|
+
1. You can configure your own `datasources.json` or `datasources.<env>.js` based on what you learn from these sample files.
|
|
214
|
+
1. Technically, to run the example, you don't need to set `NODE_ENV` **if you won't be configuring via the `.<env>.js` files** ... configuring everything within `datasources.json` is perfectly fine too. Just remember that you will lose the ability to have inline comments and will have to use double-quotes if you stick with `.json`
|
|
215
|
+
1. Start elasticsearch version 1.x and 2.x using:
|
|
216
|
+
|
|
217
|
+
```
|
|
218
|
+
git clone https://github.com/strongloop-community/loopback-connector-elastic-search.git myEsConnector
|
|
219
|
+
cd myEsConnector
|
|
220
|
+
docker-compose -f docker-compose-for-tests.yml up
|
|
221
|
+
|
|
222
|
+
# in another terminal window or tab
|
|
223
|
+
cd myEsConnector/examples
|
|
224
|
+
npm install
|
|
225
|
+
DEBUG=boot:test:* node server/server.js
|
|
226
|
+
```
|
|
227
|
+
1. Visit `localhost:3000/explorer` and you will find our example loopback app running there.
|
|
228
|
+
|
|
229
|
+
### Run example locally
|
|
230
|
+
|
|
231
|
+
1. Install dependencies and start the example server
|
|
232
|
+
|
|
233
|
+
```
|
|
234
|
+
git clone https://github.com/strongloop-community/loopback-connector-elastic-search.git myEsConnector
|
|
235
|
+
cd myEsConnector/examples
|
|
236
|
+
npm install
|
|
237
|
+
```
|
|
238
|
+
2. [Configure the connector](#configuring-connector)
|
|
239
|
+
* Don't forget to create an index in your ES instance: `curl -X POST https://username:password@my.es.cluster.com/shakespeare`
|
|
240
|
+
* If you mess up and want to delete, you can use: `curl -X DELETE https://username:password@my.es.cluster.com/shakespeare`
|
|
241
|
+
* Don't forget to set a [valid value](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/configuration.html#config-api-version) for `apiVersion` field in `examples/server/datasources.json` that matches the version of ES you are running.
|
|
242
|
+
3. Set up a `cacert.pem` file for communicating securely (https) with your ES instance. Download the certificate chain for your ES server using this **sample** (will need to be edited to *use* your provider) command:
|
|
243
|
+
|
|
244
|
+
```
|
|
245
|
+
cd myEsConnector
|
|
246
|
+
openssl s_client -connect my.es.cluster.com:9243 -showcerts | tee cacert.pem
|
|
247
|
+
```
|
|
248
|
+
1. The command may not self terminate so you may need to use `ctrl+c`
|
|
249
|
+
2. It will be saved at the base of your cloned project
|
|
250
|
+
3. Sometimes extra data is added to the file, you should delete everything after the following lines:
|
|
251
|
+
|
|
252
|
+
```
|
|
253
|
+
---
|
|
254
|
+
No client certificate CA names sent
|
|
255
|
+
---
|
|
256
|
+
```
|
|
257
|
+
4. Run:
|
|
258
|
+
|
|
259
|
+
```
|
|
260
|
+
cd myEsConnector/examples
|
|
261
|
+
DEBUG=boot:test:* node server/server.js
|
|
262
|
+
```
|
|
263
|
+
* The `examples/server/boot/boot.js` file will automatically populate data for UserModels on your behalf when the server starts.
|
|
264
|
+
5. Open this URL in your browser: [http://localhost:3000/explorer](http://localhost:3000/explorer)
|
|
265
|
+
* Try fetching all the users via the rest api console
|
|
266
|
+
* You can dump all the data from your ES index, via cmd-line too: `curl -X POST username:password@my.es.cluster.com/shakespeare/_search -d '{"query": {"match_all": {}}}'`
|
|
267
|
+
6. To test a specific filter via GET method, use for example: `{"q" : "friends, romans, countrymen"}`
|
|
268
|
+
|
|
269
|
+
## How to achieve Instant search
|
|
270
|
+
|
|
271
|
+
From version 1.3.4, `refresh` option is added which support's instant search after `create` and `update`. This option is configurable and one can activate or deactivate it according to their need. `By default refresh is true` which makes response to come only after documents are indexed(searchable).
|
|
272
|
+
To know more about `refresh` go through this [article](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-refresh.html)
|
|
273
|
+
|
|
274
|
+
* [Related Issue](https://github.com/strongloop-community/loopback-connector-elastic-search/issues/72)
|
|
275
|
+
* [Related PR](https://github.com/strongloop-community/loopback-connector-elastic-search/pull/81)
|
|
276
|
+
|
|
277
|
+
### Ways to configure refresh
|
|
278
|
+
**Datasource File:** Pass `refreshOn` array from datasource file including methods name in which you want this to be `true`
|
|
279
|
+
```
|
|
280
|
+
"es": {
|
|
281
|
+
"name": "es",
|
|
282
|
+
"refreshOn": ["save","create", "updateOrCreate"],
|
|
283
|
+
.....
|
|
284
|
+
```
|
|
285
|
+
**Model.json file:** Configurable on per model and operation level (`true`, `false`, `wait_for`)
|
|
286
|
+
```
|
|
287
|
+
"elasticsearch": {
|
|
288
|
+
"create": {
|
|
289
|
+
"refresh": false
|
|
290
|
+
},
|
|
291
|
+
"destroy": {
|
|
292
|
+
"refresh": false
|
|
293
|
+
},
|
|
294
|
+
"destroyAll": {
|
|
295
|
+
"refresh": "wait_for"
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
```
|
|
299
|
+
###### NOTE:- *While a refresh is useful, it still has a performance cost. A manual refresh can be useful, but avoid manual refresh every time you index a document in production; it will hurt your performance. Instead, your application needs to be aware of the near real-time nature of Elasticsearch and make allowances for it.*
|
|
300
|
+
|
|
301
|
+
## Troubleshooting
|
|
302
|
+
|
|
303
|
+
1. Do you have both `elasticsearch-ssl` and `elasticsearch-plain` in your `datasources.json` file? You just need one of them (not both), based on how you've setup your ES instance.
|
|
304
|
+
1. Did you forget to set `model-config.json` to point at the datasource you configured? Maybe you are using a different or misspelled name than what you thought you had!
|
|
305
|
+
1. Did you forget to set a [valid value](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/configuration.html#config-api-version) for `apiVersion` field in `datasources.json` that matches the version of ES you are running?
|
|
306
|
+
1. Maybe the version of ES you are using isn't supported by the client that this project uses. Try removing the `elasticsearch` sub-dependency from `<yourApp>/node_modules/loopback-connector-es/node_modules` folder and then install the latest client:
|
|
307
|
+
1. `cd <yourApp>/node_modules/loopback-connector-es/node_modules`
|
|
308
|
+
1. then remove the `elasticsearch` folder
|
|
309
|
+
1. unix/mac quickie: `rm -rf elasticsearch`
|
|
310
|
+
1. `npm install --save --save-exact https://github.com/elastic/elasticsearch-js.git`
|
|
311
|
+
1. to "academically" prove to yourself that this will work with the new install:
|
|
312
|
+
1. on unix/mac you can quickly dump the supported versions to your terminal with: `cat elasticsearch/package.json | grep -A 5 supported_es_branches`
|
|
313
|
+
2. on other platforms, look into the `elasticsearch/package.json` and search for the `supported_es_branches` json block.
|
|
314
|
+
1. go back to yourApp's root directory
|
|
315
|
+
1. unix/mac quickie: `cd <yourApp>`
|
|
316
|
+
1. And test that you can now use the connector without any issues!
|
|
317
|
+
1. These changes can easily get washed away for several reasons. So for a more permanent fix that adds the version you want to work on into a release of this connector, please look into [Contributing](#contributing).
|
|
318
|
+
|
|
319
|
+
## Testing
|
|
320
|
+
|
|
321
|
+
1. You can edit `test/resource/datasource-test.json` to point at your ES instance and then run `npm test`
|
|
322
|
+
1. If you don't have an ES instance and want to leverage docker based ES instances then:
|
|
323
|
+
1. If you want to run all tests across all versions in one go, then:
|
|
324
|
+
1. Run `docker-compose -f docker-compose-for-testing-all.yml up`
|
|
325
|
+
1. Then run `npm test`
|
|
326
|
+
1. When you're finished and want to tear down the docker instances, run: `docker-compose -f docker-compose-for-testing-all.yml down`
|
|
327
|
+
1. You can test a specific version of elasticsearch if you want
|
|
328
|
+
1. elasticsearch version 1.x
|
|
329
|
+
1. Run `docker-compose -f docker-compose-for-testing-v1.yml up`
|
|
330
|
+
1. Then run `npm run testv1`
|
|
331
|
+
1. To run tests with additional logging, use:
|
|
332
|
+
1. `DEBUG=test:es-v1:* npm run testv1`
|
|
333
|
+
1. `DEBUG=test:es-v1:*,loopback:connector:elasticsearch npm run testv1`
|
|
334
|
+
1. [Troubleshoot test with node-inspector](http://blog.andrewray.me/how-to-debug-mocha-tests-with-chrome/) if the level of details is still not enough:
|
|
335
|
+
1. `npm run testv1 -- --debug-brk`
|
|
336
|
+
1. `DEBUG=test:es-v1:* npm run testv1 -- --debug-brk`
|
|
337
|
+
1. `DEBUG=test:es-v1:*,loopback:connector:elasticsearch npm run testv1 -- --debug-brk`
|
|
338
|
+
1. When you're finished and want to tear down the docker instances, run: `docker-compose -f docker-compose-for-testing-v1.yml down`
|
|
339
|
+
1. elasticsearch version 2.x
|
|
340
|
+
1. Run `docker-compose -f docker-compose-for-testing-v2.yml up`
|
|
341
|
+
1. Then run `npm run testv2`
|
|
342
|
+
1. To run tests with additional logging, use:
|
|
343
|
+
1. `DEBUG=test:es-v2:* npm run testv2`
|
|
344
|
+
1. `DEBUG=test:es-v2:*,loopback:connector:elasticsearch npm run testv2`
|
|
345
|
+
1. [Troubleshoot test with node-inspector](http://blog.andrewray.me/how-to-debug-mocha-tests-with-chrome/) if the level of details is still not enough:
|
|
346
|
+
1. `npm run testv2 -- --debug-brk`
|
|
347
|
+
1. `DEBUG=test:es-v2:* npm run testv2 -- --debug-brk`
|
|
348
|
+
1. `DEBUG=test:es-v2:*,loopback:connector:elasticsearch npm run testv2 -- --debug-brk`
|
|
349
|
+
1. When you're finished and want to tear down the docker instances, run: `docker-compose -f docker-compose-for-testing-v2.yml down`
|
|
350
|
+
1. elasticsearch version 5.x
|
|
351
|
+
1. Run `docker-compose -f docker-compose-for-testing-v5.yml up`
|
|
352
|
+
1. Then run `npm run testv5`
|
|
353
|
+
1. To run tests with additional logging, use:
|
|
354
|
+
1. `DEBUG=test:es-v5:* npm run testv5`
|
|
355
|
+
1. `DEBUG=test:es-v5:*,loopback:connector:elasticsearch npm run testv5`
|
|
356
|
+
1. [Troubleshoot test with node-inspector](http://blog.andrewray.me/how-to-debug-mocha-tests-with-chrome/) if the level of details is still not enough:
|
|
357
|
+
1. `npm run testv5 -- --debug-brk`
|
|
358
|
+
1. `DEBUG=test:es-v5:* npm run testv5 -- --debug-brk`
|
|
359
|
+
1. `DEBUG=test:es-v5:*,loopback:connector:elasticsearch npm run testv5 -- --debug-brk`
|
|
360
|
+
1. When you're finished and want to tear down the docker instances, run: `docker-compose -f docker-compose-for-testing-v5.yml down`
|
|
361
|
+
|
|
362
|
+
## Contributing
|
|
363
|
+
|
|
364
|
+
1. Feel free to [contribute via PR](https://github.com/strongloop-community/loopback-connector-elastic-search/pulls) or [open an issue](https://github.com/strongloop-community/loopback-connector-elastic-search/issues) for discussion or jump into the [gitter chat room](https://gitter.im/strongloop-community/loopback-connector-elastic-search) if you have ideas.
|
|
365
|
+
1. I recommend that project contributors who are part of the team:
|
|
366
|
+
1. should merge `master` into `develop` ... if they are behind, before starting the `feature` branch
|
|
367
|
+
1. should create `feature` branches from the `develop` branch
|
|
368
|
+
1. should merge `feature` into `develop` then create a `release` branch to:
|
|
369
|
+
1. update the changelog
|
|
370
|
+
1. close related issues and mention release version
|
|
371
|
+
1. update the readme
|
|
372
|
+
1. fix any bugs from final testing
|
|
373
|
+
1. commit locally and run `npm-release x.x.x -m "<some comment>"`
|
|
374
|
+
1. merge `release` into both `master` and `develop`
|
|
375
|
+
1. push `master` and `develop` to GitHub
|
|
376
|
+
1. For those who use forks:
|
|
377
|
+
1. please submit your PR against the `develop` branch, if possible
|
|
378
|
+
1. if you must submit your PR against the `master` branch ... I understand and I can't stop you. I only hope that there is a good reason like `develop` not being up-to-date with `master` for the work you want to build upon.
|
|
379
|
+
1. `npm-release <versionNumber> -m <commit message>` may be used to publish. Pubilshing to NPM should happen from the `master` branch. It should ideally only happen when there is something release worthy. There's no point in publishing just because of changes to `test` or `examples` folder or any other such entities that aren't part of the "published module" (refer to `.npmignore`) to begin with.
|
|
380
|
+
|
|
381
|
+
## FAQs
|
|
382
|
+
|
|
383
|
+
1. How do we enable or disable the logs coming from the underlying elasticsearch client? There may be a need to debug/troubleshoot at times.
|
|
384
|
+
1. Use the `"log": "trace"` field in your datasources file or omit it. You can refer to the detailed docs [here](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/configuration.html#config-log) and [here](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/logging.html)
|
|
385
|
+
1. How do we enable or disable the logs coming from this connector?
|
|
386
|
+
1. By default if you do not set the following env variable, they are disabled: `DEBUG=loopback:connector:elasticsearch`
|
|
387
|
+
1. For example, try running tests with and without it, to see the difference:
|
|
388
|
+
1. with: `DEBUG=loopback:connector:elasticsearch npm test`
|
|
389
|
+
1. without: `npm test`
|
|
390
|
+
1. What are the tests about? Can you provide a brief overview?
|
|
391
|
+
1. Tests are prefixed with `01` or `02` etc. in order to run them in that order by leveraging default alphabetical sorting.
|
|
392
|
+
1. The `02.basic-querying.test.js` file uses two models to test various CRUD operations that any connector must provide, like `find(), findById(), findByIds(), updateAttributes()` etc.
|
|
393
|
+
1. the two models are `User` and `Customer`
|
|
394
|
+
2. their ES *mappings* are laid out in `test/resource/datasource-test.json`
|
|
395
|
+
3. their loopback *definitions* can be found in the first `before` block that performs setup in `02.basic-querying.test.js` file ... these are the equivalent of a `MyModel.json` in your real loopback app.
|
|
396
|
+
1. naturally, this is also where we define which property serves as the `id` for the model and if its [generated](https://docs.strongloop.com/display/APIC/Model+definition+JSON+file#ModeldefinitionJSONfile-IDproperties) or not
|
|
397
|
+
1. How do we get elasticserch to take over ID generation?
|
|
398
|
+
1. An automatically generated id-like field that is maintained by ES is `_uid`. Without some sort of es-field-level-scripting-on-index (if that is possible at all) ... I am not sure how we could ask elasticsearch to take over auto-generating an id-like value for any arbitrary field! So the connector is setup such that adding `id: {type: String, generated: true, id: true}` will tell it to use `_uid` as the actual field backing the `id` ... you can keep using the doing `model.id` abstraction and in the background `_uid` values are mapped to it.
|
|
399
|
+
1. Will this work for any field marked as with `generated: true` and `id: true`?
|
|
400
|
+
1. No! The connector isn't coded that way right now ... while it is an interesting idea to couple any such field with ES's `_uid` field inside this connector ... I am not sure if this is the right thing to do. If you had `objectId: {type: String, generated: true, id: true}` then you won't find a real `objectId` field in your ES documents. Would that be ok? Wouldn't that confuse developers who want to write custom queries and run 3rd party app against their ES instance? Don't use `obejctId`, use `_uid` would have to be common knowledge. Is that ok?
|
|
401
|
+
|
|
402
|
+
## Release notes
|
|
403
|
+
|
|
404
|
+
* Release `1.0.6` of this connector updates the underlying elasticsearch client version to `11.0.1`
|
|
405
|
+
* For this connector, you can configure an `index` name for your ES instance and the loopback model's name is conveniently/automatically mapped as the ES `type`.
|
|
406
|
+
* Users must setup `string` fields as `not_analyzed` by default for predictable matches just like other loopback backends. And if more flexibility is required, multi-field mappings can be used too.
|
|
407
|
+
|
|
408
|
+
```
|
|
409
|
+
"name" : {
|
|
410
|
+
"type" : "multi_field",
|
|
411
|
+
"fields" : {
|
|
412
|
+
"name" : {"type" : "string", "index" : "not_analyzed"},
|
|
413
|
+
"native" : {"type" : "string", "index" : "analyzed"}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
...
|
|
417
|
+
// this will treat 'George Harrison' as 'George Harrison' in a search
|
|
418
|
+
User.find({order: 'name'}, function (err, users) {..}
|
|
419
|
+
// this will treat 'George Harrison' as two tokens: 'george' and 'harrison' in a search
|
|
420
|
+
User.find({order: 'name', where: {'name.native': 'Harrison'}}, function (err, users) {..}
|
|
421
|
+
```
|
|
422
|
+
* Release `1.3.4` add's support for updateAll for elasticsearch `v-2.3` and above. To make updateAll work you will have to add below options in your `elasticsearch.yml` config file
|
|
423
|
+
|
|
424
|
+
```
|
|
425
|
+
script.inline: true
|
|
426
|
+
script.indexed: true
|
|
427
|
+
script.engine.groovy.inline.search: on
|
|
428
|
+
script.engine.groovy.inline.update: on
|
|
429
|
+
```
|
|
430
|
+
* TBD
|