Skip to content

Commit

Permalink
feat: add transform function
Browse files Browse the repository at this point in the history
  • Loading branch information
wozniakm authored and nodkz committed Jun 2, 2019
1 parent 709dcbb commit ed61012
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Options are:
* `protocol` - the protocol the Elasticsearch server uses. Defaults to http.
* `hydrate` - whether or not to replace ES source by mongo document.
* `filter` - the function used for filtered indexing.
* `transform` - the function used for transforming a document before indexing it, accepts the document as an argument, expects transformed document to be returned (if returned value is falsy, the original document will be used).
* `idsOnly` - whether or not returning only mongo ids in `esSearch`.
* `countOnly` - whether or not returning only the count value in `esCount`.
* `mappingSettings` - default settings to use with `esCreateMapping`.
Expand All @@ -99,7 +100,6 @@ Options are:
* `bulk.delay` - idle time to wait before calling the `client.bulk` function. Defaults to 1000.
* `onlyOnDemandIndexing` - whether or not to demand indexing on CRUD operations. If set to true middleware hooks for save, update, delete do not fire. Defaults to false.


To have a model indexed into Elasticsearch simply add the plugin.

```javascript
Expand Down Expand Up @@ -378,6 +378,24 @@ MovieSchema.plugin(mexp, {
});
```

### Transforming a document before indexing

You can specify a function to transform a document before indexing it in ElasticSearch.

```javascript
var MovieSchema = new mongoose.Schema({
title: {type: String},
genre: {type: String, enum: ['horror', 'action', 'adventure', 'other']}
});

MovieSchema.plugin(mexp, {
transform: function (doc) {
delete doc.genre;
return doc;
}
});
```

Instances of Movie model having 'action' as their genre will be indexed to Elasticsearch.

### Indexing On Demand
Expand Down
8 changes: 7 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,13 @@ function indexDoc(update, callback) {
}
return utils.run(callback, (resolve, reject) => {
const esOptions = self.esOptions();
const body = utils.serialize(self, esOptions.mapping);
let body = utils.serialize(self, esOptions.mapping);
if (typeof esOptions.transform === 'function') {
const transformedBody = esOptions.transform(body);
if (transformedBody) {
body = transformedBody;
}
}
if (update && update.unset) {
(typeof update.unset === 'string'
? [update.unset]
Expand Down
15 changes: 13 additions & 2 deletions test/es7/esIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,23 @@ describe('esIndex', () => {
es_type: 'geo_point',
es_boost: 1.5,
},
doNotIndexMe: Boolean,
});

UserSchema.plugin(plugin);
UserSchema.plugin(plugin, {
transform: document => {
delete document.doNotIndexMe;
return document;
},
});
const UserModel = mongoose.model('User', UserSchema);

const john = new UserModel({ name: 'John', age: 35, pos: [5.7333, 43.5] });
const john = new UserModel({
name: 'John',
age: 35,
pos: [5.7333, 43.5],
doNotIndexMe: true,
});

return utils
.deleteModelIndexes(UserModel)
Expand Down

0 comments on commit ed61012

Please sign in to comment.