MongoDB fulltext search + workaround for partial word match MongoDB fulltext search + workaround for partial word match mongodb mongodb

MongoDB fulltext search + workaround for partial word match


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.


A simple workaround I am doing right now is to break the text into individual chars stored as a text indexed array.

Then when you do the $search query you simply break up the query into chars again.

Please note that this only works for short strings say length smaller than 32 otherwise the indexing building process will take really long thus performance will be down significantly when inserting new records.


You can not query for all the words in the index, but you can of course query the original document's fields. The words in the search index are also not always the full words, but are stemmed anyway. So you probably wouldn't find "blueberry" in the index, but just "blueberri".