You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 19, 2024. It is now read-only.
@@ -44,7 +45,7 @@ This plugin handle both callback and promise syntaxes. It uses the mongoose Prom
44
45
45
46
## Installation
46
47
47
-
The latest version of this package will be as close as possible to the latest `elasticsearch` and `mongoose` packages.
48
+
The latest version of this package will be as close as possible to the latest `elasticsearch` and `mongoose` packages.
48
49
49
50
```bash
50
51
npm install --save mongoose-elasticsearch-xp
@@ -73,7 +74,7 @@ The examples below use the version 5 syntax.
73
74
Options are:
74
75
75
76
*`index` - the index in Elasticsearch to use. Defaults to the collection name.
76
-
*`type` - the type this model represents in Elasticsearch. Defaults to the model name.
77
+
*`type` - the type this model represents in Elasticsearch. Defaults to the model name. It may be a function `(modelName) => typeName`.
77
78
*`client` - an existing Elasticsearch `Client` instance.
78
79
*`hosts` - an array hosts Elasticsearch is running on.
79
80
*`host` - the host Elasticsearch is running on.
@@ -100,8 +101,8 @@ var mongoose = require('mongoose');
100
101
var mexp =require('mongoose-elasticsearch-xp');
101
102
102
103
var UserSchema =newmongoose.Schema({
103
-
name:String,
104
-
email:String,
104
+
name:String,
105
+
email:String,
105
106
city:String
106
107
});
107
108
@@ -110,18 +111,18 @@ UserSchema.plugin(mexp);
110
111
var User =mongoose.model('User', UserSchema);
111
112
```
112
113
113
-
This will by default simply use the collection name as the index while using the model name itself as the type.
114
-
So if you create a new User object and save it, you can see it by navigating to http://localhost:9200/users/user/_search
115
-
(this assumes Elasticsearch is running locally on port 9200).
114
+
This will by default simply use the collection name as the index while using the model name itself as the type.
115
+
So if you create a new User object and save it, you can see it by navigating to http://localhost:9200/users/user/_search
116
+
(this assumes Elasticsearch is running locally on port 9200).
116
117
117
-
The default behavior is all fields get indexed into Elasticsearch.
118
+
The default behavior is all fields get indexed into Elasticsearch.
118
119
This can be a little wasteful especially considering that the document is now just being duplicated between mongodb and Elasticsearch so you should consider opting to index only certain fields by specifying `es_indexed` on the fields you want to store:
119
120
120
121
121
122
```javascript
122
123
var UserSchema =newmongoose.Schema({
123
-
name: {type:String, es_indexed:true},
124
-
email:String,
124
+
name: {type:String, es_indexed:true},
125
+
email:String,
125
126
city:String
126
127
});
127
128
@@ -177,7 +178,7 @@ User
177
178
});
178
179
```
179
180
180
-
To connect to more than one host, you can use an array of hosts.
181
+
To connect to more than one host, you can use an array of hosts.
181
182
182
183
```javascript
183
184
MyModel.plugin(mexp, {
@@ -202,8 +203,8 @@ MyModel.plugin(mexp, {
202
203
## Indexing
203
204
204
205
### Saving a document
205
-
The indexing takes place after saving inside the mongodb and is a deferred process.
206
-
One can check the end of the indexion catching `es-indexed` event.
206
+
The indexing takes place after saving inside the mongodb and is a deferred process.
207
+
One can check the end of the indexion catching `es-indexed` event.
207
208
This event is emitted both from the document and the model (which make unit tests easier).
208
209
209
210
```javascript
@@ -223,15 +224,15 @@ In order to index nested models you can refer following example.
@@ -268,10 +269,10 @@ var CitySchema = new mongoose.Schema({
268
269
var City =mongoose.model('City', CitySchema);
269
270
270
271
var UserSchema =newmongoose.Schema({
271
-
name:String,
272
+
name:String,
272
273
city: {
273
-
type:mongoose.Schema.Types.ObjectId,
274
-
ref:'City',
274
+
type:mongoose.Schema.Types.ObjectId,
275
+
ref:'City',
275
276
es_type: {
276
277
name: {
277
278
es_type:'string'
@@ -300,7 +301,7 @@ var User = mongoose.model('User', UserSchema);
300
301
301
302
302
303
### Indexing An Existing Collection
303
-
Already have a mongodb collection that you'd like to index using this plugin?
304
+
Already have a mongodb collection that you'd like to index using this plugin?
304
305
No problem! Simply call the `esSynchronize` method on your model to open a mongoose stream and start indexing documents individually.
305
306
306
307
```javascript
@@ -407,7 +408,7 @@ If [dynamic-scripting](https://www.elastic.co/guide/en/elasticsearch/reference/2
407
408
408
409
409
410
### Adding fields
410
-
`es_extend` allows to add some fields which does not exist in the mongoose schema.
411
+
`es_extend` allows to add some fields which does not exist in the mongoose schema.
411
412
It is defined in the options of the schema definition.
412
413
When adding some fields, `es_type` and `es_value` are mandatories.
413
414
@@ -436,7 +437,7 @@ The `es_value` parameter can be either a value or a function returning a value,
436
437
### Change fields value
437
438
`es_value` allows to replace the value of a field. It can be either a value or a function which will return the value to index.
438
439
If the type changes, it is mandatory to set the correct `es_type`.
439
-
440
+
440
441
```javascript
441
442
var TagSchema =newmongoose.Schema({
442
443
_id:false,
@@ -447,7 +448,7 @@ var UserSchema = new mongoose.Schema({
447
448
name:String,
448
449
xyz: {
449
450
type:Number,
450
-
es_value:123// <= whatever the model.xyz value is, the xyz indexed will be 123 in ES
451
+
es_value:123// <= whatever the model.xyz value is, the xyz indexed will be 123 in ES
451
452
},
452
453
tags: {
453
454
type: [TagSchema],
@@ -487,35 +488,66 @@ context contains:
487
488
* `container` the container of the original value (which is equal to the `document` when it is not a nested object)
488
489
* `field` the key name
489
490
491
+
### Using with mongoose discriminators
492
+
493
+
You may save discriminator models' data in different Elasticsearch types with different mappings. To make it possible you should provide `type` option as a function. You will get `modelName` as an argument and must return type name for Elasticsearch.
// add mexp plugin to the base Schema, with `type` as a function
512
+
BaseSchema.plugin(mexp, {
513
+
index:'user',
514
+
type:kind=> {
515
+
if (kind ==='User') return'userType';
516
+
if (kind ==='Admin') return'adminType';
517
+
return'base';
518
+
},
519
+
});
520
+
```
521
+
490
522
491
523
## Mapping
492
524
493
-
Schemas can be configured to have special options per field. These match with the existing [mapping parameters](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-params.html) defined by Elasticsearch with the only difference being they are all prefixed by `es_`.
525
+
Schemas can be configured to have special options per field. These match with the existing [mapping parameters](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-params.html) defined by Elasticsearch with the only difference being they are all prefixed by `es_`.
494
526
495
527
So for example. If you wanted to index a book model and have the boost for title set to 2.0 (giving it greater priority when searching) you'd define it as follows:
This example uses a few other mapping fields... such as null_value and type (which overrides whatever value the schema type is, useful if you want stronger typing such as float).
506
538
507
-
There are various mapping options that can be defined in Elasticsearch. Check out [https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html/](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html) for more information.
539
+
There are various mapping options that can be defined in Elasticsearch. Check out [https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html/](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html) for more information.
508
540
509
541
### Creating Mappings On Demand
510
542
511
543
You can do on-demand create a mapping using the `esCreateMapping` function.
512
544
513
545
Creating the mapping is a one time operation and can be done as follows:
514
546
515
-
```javascript
547
+
```javascript
516
548
var UserSchema =newmongoose.Schema({
517
-
name:String,
518
-
email:String,
549
+
name:String,
550
+
email:String,
519
551
city:String
520
552
});
521
553
@@ -540,19 +572,19 @@ User
540
572
541
573
```
542
574
543
-
You'll have to manage whether or not you need to create the mapping, mongoose-elasticsearch-xp will make no assumptions and simply attempt to create the mapping.
544
-
If the mapping already exists, an Exception detailing such will be populated in the `err` argument.
575
+
You'll have to manage whether or not you need to create the mapping, mongoose-elasticsearch-xp will make no assumptions and simply attempt to create the mapping.
576
+
If the mapping already exists, an Exception detailing such will be populated in the `err` argument.
545
577
546
578
## Queries
547
-
The full query DSL of Elasticsearch is exposed through the `esSearch` method.
579
+
The full query DSL of Elasticsearch is exposed through the `esSearch` method.
548
580
For example, if you wanted to find all people between ages 21 and 30:
549
581
550
582
```javascript
551
583
Person
552
584
.esSearch({
553
585
range: {
554
586
age: {
555
-
from:21,
587
+
from:21,
556
588
to:30
557
589
}
558
590
}
@@ -588,7 +620,7 @@ Person
588
620
```
589
621
590
622
### Hydration
591
-
By default objects returned from performing a search will be the objects as is in Elasticsearch.
623
+
By default objects returned from performing a search will be the objects as is in Elasticsearch.
592
624
This is useful in cases where only what was indexed needs to be displayed (think a list of results) while the actual mongoose object contains the full data when viewing one of the results.
593
625
594
626
However, if you want the results to be actual mongoose objects you can provide {hydrate: true} as the second argument to a search call.
@@ -675,7 +707,7 @@ User
675
707
676
708
677
709
### Getting only Ids
678
-
A variant to hydration may be to get only ids instead of the complete Elasticsearch result.
710
+
A variant to hydration may be to get only ids instead of the complete Elasticsearch result.
679
711
Using `idsOnly` will return the ids cast in mongoose ObjectIds.
680
712
681
713
```javascript
@@ -687,7 +719,7 @@ User
687
719
```
688
720
689
721
## Count
690
-
The [count API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-count.html) is available using the `esCount` function.
722
+
The [count API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-count.html) is available using the `esCount` function.
691
723
It handle the same queries as the `esSearch` method (string query, full query...).
0 commit comments