# Creating custom nGram analyzers with mongoosastic

**URL:** <https://discuss.elastic.co/t/creating-custom-ngram-analyzers-with-mongoosastic/101685>\
**Category:** Elasticsearch\
**Created:** [September 25, 2017, 11:09am UTC](https://discuss.elastic.co/t/creating-custom-ngram-analyzers-with-mongoosastic/101685 "2017-09-25T11:09:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcose18](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marcose18/32/22485_2.png) [@marcose18](https://discuss.elastic.co/u/marcose18)\
**Post date:** [September 25, 2017, 11:09am UTC](https://discuss.elastic.co/t/creating-custom-ngram-analyzers-with-mongoosastic/101685/1 "2017-09-25T11:09:02Z")

</div>

Hi !  
I am trying to use elastic search with mongoosastic to find associated search terms.  
Let's say If I have some email id as [abcdef@xyz.com](mailto:abcdef@xyz.com) , then if the user types 'cde', this email should be returned as a search result. I am using ngram, analyzer to do this but it's not doing what it is supposed to do. It doesn't return above email as a search result. It will be returned if I type the whole email which is [abcdef@xyz.com](mailto:abcdef@xyz.com)

Currently my code is :-

var mongoose = require('mongoose')  
, mongoosastic = require('mongoosastic')  
, Schema = mongoose.Schema  
, elasticsearch = require('elasticsearch');

var UserSchema = new Schema({  
name: { type: String, es\_indexed: true, analyzer: "edge\_nGram\_analyzer"}  
, email: { type: String, es\_indexed: true}  
, city: {type: String, es\_indexed: true}  
});

var esClient = new elasticsearch.Client({  
host: 'localhost:9200'  
});  
UserSchema.plugin(mongoosastic, {  
esClient: esClient  
});

var Users = mongoose.model('Users', UserSchema);

Users.createMapping({  
"settings": {  
"analysis": {  
"analyzer": {  
"edge\_nGram\_analyzer": {  
"type": "custom",  
"tokenizer": "edge\_ngram\_tokenizer",  
"filter": [  
"lowercase",  
"asciifolding",  
"edgeNGram\_filter"  
]  
}  
},  
"filter": {  
"edgeNGram\_filter": {  
"type": "edgeNGram",  
"min\_gram": 2,  
"max\_gram": 20,  
"side": "front"  
}  
},  
"tokenizer": {  
"edge\_ngram\_tokenizer": {  
"type": "edgeNGram",  
"min\_gram": "2",  
"max\_gram": "5",  
"token\_chars": ["letter", "digit"]  
}  
}  
}  
}  
}  
, function (err, mapping) {  
if (err) throw err;  
else console.log('Mapping created');  
});

// Users.createMapping(function(err, mapping) {  
// if (err) {  
// console.log('error creating mapping (you can safely ignore this)');  
// console.log(err);  
// } else {  
// console.log('mapping created!');  
// console.log(mapping);  
// }  
// });

module.exports = Users;

* * *

and the search query looks like

router.get('/search', function(req, res){  
Users.search({  
query\_string: {  
query: req.query.queryString  
}  
}, function(err, results) {  
if(err)console.log("Not found");  
else return res.json(results);  
});  
});

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 23, 2017, 11:09am UTC](https://discuss.elastic.co/t/creating-custom-ngram-analyzers-with-mongoosastic/101685/2 "2017-10-23T11:09:12Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
