node.js - 如何使用 MongoDB 全文搜索创建自动完成

标签 node.js mongodb mongoose full-text-search

我想创建一个自动完成输入框,在用户键入时显示单词建议。

基本上,我的问题是当我使用 $text 运算符在文档中搜索字符串时,查询将只匹配完整的词干词。这与如果文档字段包含单词 blueberry 的原因相同,则对术语 blue 的搜索将不会匹配该文档。但是,对 blueberry 或 blueberries 的搜索将匹配。

find = {$text: { $search:  'blue' } };

^(与文档中的 blueberry 或 bluebird 不匹配。)

我希望能够做到这一点。我想将 'blueberry' 或 'bluebird' 与 'blue' 匹配,最初我认为这可以通过使用 'starts with' (^) 正则表达式来实现,但似乎 $text 和 $search 只接受一个字符串;不是正则表达式。

我想知道是否有一种实现/维护起来不太复杂的方法。到目前为止,我只看到人们试图通过创建一个新集合来实现这一点,结果是使用文本索引在集合中运行 map/reduce。

我不想使用 ElasticSearch 或 Solr,因为我认为这对我想做的事情来说太过分了,虽然我有时认为最终我别无选择,但我仍然无法相信没有更简单的方法方法来完成这个。

最佳答案

MongoDB 全文搜索只匹配整个单词,所以它天生不适合自动完成。

The $text operator can search for words and phrases. The query matches on the complete stemmed words. For example, if a document field contains the word blueberry, a search on the term blue will not match the document. However, a search on either blueberry or blueberries will match.

(来源:http://docs.mongodb.org/manual/core/index-text/)

关于node.js - 如何使用 MongoDB 全文搜索创建自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32258993/

相关文章:

node.js - 在 mongoose pre/save/(serial/parallel) 中间件中绑定(bind)了哪些函数 done/next

javascript - Mongoose 嵌套对象不更新

node.js - $addToSet 与子文档数组

node.js - Mongoose avg 函数(按请求字段)

node.js - 网络驱动程序错误: cannot find dict 'desiredCapabilities'

javascript - 将 Node JS 库与 QML 一起使用

c# - 如何从控制台应用程序跟踪 MongoDB 请求

javascript - Heroku 在部署时不指定端口

html - SVG 在 Node.js 中显示不正确

javascript - Mongoose - 如何更新 MongoDB 数组中的所有对象?