Full Text Search using MongoDB

Apr 08, 2017

If you build a web-service around content, then it is a matter of time when you need to introduce a search functionality.

Depending on the shape of your data you may go the easiest way: search using a regular expression against some field (title, name, etc.). But what if you have more than one field? What if the data is spread across several collections? You would probably also want to have a ‘search rank,’ i.e. to know how close the match is.

This way the simple solution will not work anymore. Or it won’t be simple.

‘Classic’ solution for this problem is to pick something like Elastic Search or Apache Lucene. But if you are running on MongoDB, then there is no need to introduce another dependency into your stack.
You can use the MongoDB Full-Text Search and MongoDB Aggregation Framework to build powerful search functionality.

Getting Started

I highly recommend to create a database locally, import sample data and follow my actions step by step.
I crafted small data set. The data is normalized on purpose. I chose the “worst” case to show a real-world application.

Just connect to some sandbox database locally (you can pick any other name):

However, this approach might be slow, especially if we add few more fields. Also, it probably makes sense to order results based on some criteria. E.g., matches by title go first, then go matches by genres.

This way we would need to do few searches, for each field, and then combine results manually.

Fortunately, there is a much better approach.

Full-Text Search

MongoDB supports text indexes that enable Full Text Search.
The great thing about text indexes: they provide textScore, which is controlled via weights.
Here is an example: