MongoDB全文搜索+用于部分单词匹配的解决方法 [英] MongoDB fulltext search + workaround for partial word match

查看:545
本文介绍了MongoDB全文搜索+用于部分单词匹配的解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于无法通过使用mongodb全文搜索来查找blue这个词的blueberry,我想帮助我的用户完成blue一词到blueberry。为此,是否可以查询mongodb全文索引中的所有单词 - >我可以使用这些单词作为建议,即typeahead.js?

解决方案

文字搜索中的语言词汇使用算法尝试关联从一个共同基础派生的词(例如跑步应该与跑步相匹配)。这与您要为自动完成功能实现的前缀匹配(例如,蓝色匹配蓝莓)不同。



为了最有效地使用 typeahead.js 与MongoDB文本搜索我建议重点关注 prefetch 在typeahead中的支持:





然后,您可以使用生成的关键字JSON进行客户端自动完成,并使用typeahead的 prefetch 功能:

  $('。mysearch .typeahead')。typeahead({
名称:'mysearch',
prefetch: '/data/keywords.json'
});

typeahead.js 会缓存 prefetch localStorage中的JSON数据用于客户端搜索。当提交搜索表单时,您的应用程序可以使用服务器端 MongoDB文本搜索以相关顺序返回完整结果。


Since it is not possible to find "blueberry" by the word "blue" by using a mongodb full text search, I want to help my users to complete the word "blue" to "blueberry". To do so, is it possible to query all the words in a mongodb full text index -> that I can use the words as suggestions i.e. for typeahead.js?

解决方案

Language stemming in text search uses an algorithm to try to relate words derived from a common base (eg. "running" should match "run"). This is different from the prefix match (eg. "blue" matching "blueberry") that you want to implement for an autocomplete feature.

To most effectively use typeahead.js with MongoDB text search I would suggest focusing on the prefetch support in typeahead:

  • Create a keywords collection which has the common words (perhaps with usage frequency count) used in your collection. You could create this collection by running a Map/Reduce across the collection you have the text search index on, and keep the word list up to date using a periodic Incremental Map/Reduce as new documents are added.

  • Have your application generate a JSON document from the keywords collection with the unique keywords (perhaps limited to "popular" keywords based on word frequency to keep the list manageable/relevant).

You can then use the generated keywords JSON for client-side autocomplete with typeahead's prefetch feature:

$('.mysearch .typeahead').typeahead({
  name: 'mysearch',
  prefetch: '/data/keywords.json'
});

typeahead.js will cache the prefetch JSON data in localStorage for client-side searches. When the search form is submitted, your application can use the server-side MongoDB text search to return the full results in relevance order.

这篇关于MongoDB全文搜索+用于部分单词匹配的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆