A level ahead with text search in mongoDb

11 / Jul / 2014 by Sakshi Tyagi 1 comments

In our previous post, we saw how simply we could use the free text search feature from MongoDB. In continuation to that, in this post we would be looking at how can we create multiple text indexes and understand how actually the things work.

Document Scoring
MongoDB assigns a score to each of the document that contains the search term passed in $text field. The higher the score, the more relevant that document would be to that search term.

Stemming
MongoDb full text search only matches the complete stemmed words. For those who donโ€™t know what stemming is, it is a process of reducing a word to its stem/root. For example:walked, walking, walks.

All of the above words have a stem word walk. Stemming is language dependent and that is why MongoDb supports quite a bit of languages which have their own set of stop words and stemmers defined.

Create Compound Text Indexes
MongoDb allows us to create compound text indexes, lets see how we can do that:

db.article.ensureIndex({ 'title' : 'text', 'content': 'text' });

In the above command, we have created text index on more then one fields, i.e., title and text fields of the article collection.

When creating compound indexes, we can assign individual weights to the fields to increase their ranking in the search results. Weights can be assigned as follows while creating indexes:

db.article.ensureIndex({ 'title' : 'text', 'content': 'text' }, {weights: {title: 5}});

In the above statement, we have assigned the title a weight of 5, while the content field gets the default weight of 1. So, if a search match is found in title and content fields, the documents with title match will have higher ranking due to the assigned weight.

We can also apply projection to the $text queries and get only the required fields just like normal projections.

Multiple Language Support
MongoDb supports a handful of text search languages, we can define the default language for a text index so that MongoDB uses relevant stemmers and stop words. This can be done as follows:

db.article.ensureIndex({'title': 'text'}, {'default_language': 'french'});

You can find the list of languages supported as of now here.

I hope this helps you in understanding the Free text search from MongoDB. We should see even more features related to search in the upcoming releases of the database. ๐Ÿ™‚

FOUND THIS USEFUL? SHARE IT

comments (1 “A level ahead with text search in mongoDb”)

Leave a Reply

Your email address will not be published. Required fields are marked *