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
- createIndex
db.articles.createIndex( { subject: "text" } )
- 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 }
] )
- query
db.articles.find( { $text: { $search: "coffee" } } )
- 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 }