6

I want to create an autocomplete input box that shows word suggestions as users type.

Basically, my problem is that when I use the $text operator for searching strings in a document, the queries will only match on complete stemmed words. This is for the same reason that 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 would match.

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

^ (doesn't match blueberry or bluebird on a document.)

I want to be able to do this. I want to match 'blueberry' or 'bluebird' with 'blue', and initially I thought this was possible by using a 'starts with' (^) regular expression, but it seems like $text and $search only accepts a string; not a regexp.

I would like to know if there is a way to do this that is not excessively complex to implement/maintain. So far, I've only seen people trying to accomplish this by creating a new collection with the results of running a map/reduce across the collection with the text index.

I do not want to use ElasticSearch or Solr because I think it is overkill for what I am trying to do, and although I sometimes think that eventually I will have no other choice, I still cannot believe that there is not a simpler way to accomplish this.

pedrommuller
  • 15,360
  • 9
  • 71
  • 124
cyber ice
  • 283
  • 3
  • 12

1 Answers1

4

MongoDB full text search matches whole words only, so it is inherently not suitable for auto complete.

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.

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

Florian Winter
  • 4,103
  • 1
  • 39
  • 61