MongoDB - text search

What is the best strategy for selecting mongodb records where the string value contains a set of words or phrases? I am thinking of something equivalent to the mysql LIKE function, eg.

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

      

I've seen options that involve tokenizing a string, but does this involve creating unigrams for the whole text, which would be huge?

+3


source to share


4 answers


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

I strongly discourage you from trying to rearrange your text search with a regex or a phrase, etc. yourself. You should focus on your own application functions :)



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

+3


source


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 text search enabled setParameter Then enable the index on the collection Then search with runCommand

+4


source


You can leverage regex support in MongoDB queries. More information is available at the following link

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

Here are two examples where the above link will navigate in the future:

db.collection.find( { field: /acme.*corp/i } );
db.collection.find( { field: { $regex: 'acme.*corp', $options: 'i' } } );

      

+1


source


Somehow MongoDB's embedded text search didn't fit my requirements for an existing database that used a composite index. Now I am using mongoose-search-plugin and it works great. It uses natural stems 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));
}); 

      

0


source







All Articles