3

What is the best strategy for selecting mongodb entries in which a string value contains a set of words or phrases? I'm thinking of something equivalent to mysql's LIKE function, e.g.

    WHERE (TEXT LIKE "% apple %") or (TEXT LIKE "% banana %")

I've seen options that involve tokenizing the string, but this would involve building unigrams for all the text, which would be huge no?

Stuart
  • 1,251
  • 2
  • 11
  • 18

4 Answers4

4

Mongo now supports text search since 2.4. My experience has been pretty positive

http://docs.mongodb.org/manual/applications/text-search/

You start the server with setParameter text search enabled Then enable the index on the collection Then search with runCommand

Scott
  • 16,711
  • 14
  • 75
  • 120
chris
  • 581
  • 10
  • 25
3

MongoDB has no full text search capability right now, but it's easy to use external search engines like SOLR.

I strongly discourage you trying to rebuild text search with Regex or word stemming etc. yourself. You should rather focus on your app own features :)

I am using this combination: Mongoid, Sunspot and Mongoid-Sunspot. It works very well in production, and development setup is easy.

Rafa Viotti
  • 9,998
  • 4
  • 42
  • 62
Blacksad
  • 14,906
  • 15
  • 70
  • 81
  • +1 for this - and I would also recommend voting this up (it's already number 2, but all votes help): https://jira.mongodb.org/browse/SERVER-380 – Adam Comerford Mar 09 '12 at 10:55
1

You can use the regular expression support in MongoDB queries. More details available @ the following link

http://docs.mongodb.org/manual/reference/operator/regex/

Here are two examples should the above link move again in the future:

db.collection.find( { field: /acme.*corp/i } );
db.collection.find( { field: { $regex: 'acme.*corp', $options: 'i' } } );
Scott
  • 16,711
  • 14
  • 75
  • 120
Samarth Bhargava
  • 4,196
  • 3
  • 17
  • 16
0

Somehow MongoDB built-in text search failed to meet my requirements on an existing database which used a compound index. I am now using mongoose-search-plugin and it has been working superbly well. It uses natural stemming, and distance algorithms to return a relevance score.

User.search('Malaysia Car Food',{username:1},{},  function(err, u){
   console.log('Search Results: '+JSON.stringify(u));
}); 
Chong Lip Phang
  • 8,755
  • 5
  • 65
  • 100