Home > Software engineering >  How do I create a search engine that find similar results in case it does not find a specific match?
How do I create a search engine that find similar results in case it does not find a specific match?

Time:02-25

I am building a question function in my website where a user can search up a question for example: "how do I cook a cake?" and get a link to the question someone else asked before of whom title is: "how do I make a cake?" the question is almost the same yet the writing is different and for now a user cant find question 2 if they search for question 1 search i just input the search bar into collection.find({}) how do I fix this? is there an API who can maybe generate similar and same meaning sentences to search for?

Thanks!

CodePudding user response:

I dont think this answer is a search engine tat you want but it is the best that mongodb can do.

Use $text

  1. createIndex
db.articles.createIndex( { subject: "text" } )
  1. data
db.articles.insertMany( [
     { _id: 1, subject: "coffee", author: "xyz", views: 50 },
     { _id: 2, subject: "Coffee Shopping", author: "efg", views: 5 },
     { _id: 3, subject: "Baking a cake", author: "abc", views: 90  },
     { _id: 4, subject: "baking", author: "xyz", views: 100 },
     { _id: 5, subject: "Café Con Leche", author: "abc", views: 200 },
     { _id: 6, subject: "Сырники", author: "jkl", views: 80 },
     { _id: 7, subject: "coffee and cream", author: "efg", views: 10 },
     { _id: 8, subject: "Cafe con Leche", author: "xyz", views: 10 }
] )
  1. query
db.articles.find( { $text: { $search: "coffee" } } )
  1. result
{ _id: 1, subject: 'coffee', author: 'xyz', views: 50 },
{ _id: 7, subject: 'coffee and cream', author: 'efg', views: 10 },
{ _id: 2, subject: 'Coffee Shopping', author: 'efg', views: 5 }
  • Related