MongoDB fulltext search + workaround for partial word match

KIC picture KIC · Jan 9, 2014 · Viewed 12.4k times · Source

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?

Answer

Stennie picture Stennie · Jan 9, 2014

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.