如果找不到特定的匹配项,我该如何创建一个搜索引擎来找到相似的结果? (mongoDB)

How do I create a search engine that find similar results in case it does not find a specific match? (mongoDB)

我正在我的网站上构建一个问题功能,用户可以在其中搜索问题 例如: “我怎么做蛋糕?” 并得到 link 之前别人问过的问题 who title is: “我怎么做蛋糕?” 问题几乎相同,但写作不同,现在用户无法找到问题 2,如果他们搜索问题 1 搜索我只是将搜索栏输入 collection.find({}) 我该如何解决?有没有 API 可以生成相似和相同含义的句子来搜索?

谢谢!

我不认为这个答案是您想要的搜索引擎,但它是 mongodb 可以做的最好的。

使用$text

  1. 创建索引
db.articles.createIndex( { subject: "text" } )
  1. 数据
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. 查询
db.articles.find( { $text: { $search: "coffee" } } )
  1. 结果
{ _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 }