Creating custom nGram analyzers with mongoosastic


(Abhishek Pratap Singh) #1

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 , 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

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);
});
});


(system) #2

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