Fuzzy search in full text search using mongoosastic

512 Views Asked by At

I've been working on a website with a search feature which matches the queries with the various article present in MongoDB. currently mongoDB does not support fuzzy search with is what I want with my search feature. For that I've found that Elasticsearch works the best with this type of problem. I've use mongoosastic client for the node.js for this purpose. I was able to save data item and search the query but it can't search if there is any spelling mistake present in it. How can I customise the query that help finding the text even with some typo or word missing.

const mongoose     = require('mongoose');
const mongoosastic = require('mongoosastic');

mongoose.connect('mongodb://localhost:27017/mongosync'); 

var UserSchema = new mongoose.Schema({
    name: String
  , email: String
  , city: String
});

UserSchema.plugin(mongoosastic, {
    "host": "localhost",
    "port": 9200
}, {hydrate:true, hydrateOptions: {lean: true}});

var User = mongoose.model('user', UserSchema);

// User.createMapping((err, mapping) => {
//     console.log('mapping created');
// });

// var newUser = new User({
//     name: 'Abhishek',
//     email: '[email protected]',
//     city: 'bhopal'
// });

// newUser.save((err) => {
//     if(err) {
//         console.log(err);
//     }
//     console.log('user added in both the databases');
// })

// newUser.on('es-indexed', (err, result) => {
//     console.log('indexed to elastic search');
// });

User.search(
    {query_string: {query: "abheshek"}}, 
    function(err, results) {
    if(err){
        console.log('ERROR OCCURED');
    } else {
        console.log(results);
    }
});

1

There are 1 best solutions below

0
Todor Pavlovic On

I think this will help :)

Place.search({
  match: {
    name: {
      query: q,
      fuzziness: "auto"
    }
  }
}, (err, results) => {
  if (err) return next(err);

  const data = results.hits.hits.map(hit => hit);

  // return res.json(data);
  return res.status(200).json({locations: data});
});