One of the leading NoSQL databases, MongoDB is well known for its fast performance, versatile schema, scalability and great capabilities for indexing. Let us look at some context before we get into some details. Full-text search is an essential feature when we talk about finding content on the internet. A google search is the best example for this when we see the content using the phrases or keywords. In this article, we will learn about full-text search capabilities in MongoDB based on text index. Create a Sample DatabaseBefore we begin, we will create a sample database that will be used during the tutorial. We will create a database with the name myDB and create a collection with the name books. For this, the statement would be as follows.
Let's insert some documents by using the following statement.
Creating a Text IndexWe need to create a text index on the fields to perform the text search. We can create this on single or multiple fields. The following statement will create a text index on a single field.
We will create a text index on the description and subtitle fields for this tutorial. We can create only one text index per collection in MongoDB. So We will create a compound text index using the following statement.
$searchNow we will try to search documents that have the keywords 'ECMAScript' in the description and subtitle fields. For this, we can use the below statement.
Example
PhrasesYou can search for phrases using the text index. By default, text search performs an OR search for all words in the phrase. If you want to search 'modern design patterns', it will search for documents with the keywords either modern, design, or patterns. Example
If you want to search for exact phrases like documents with 'modern design patterns' together, you can do so by specifying double quotes in the search text. Example
NegationsIf you want to exclude the documents containing a particular word, you can use a negation search. For example if you're going to search all documents with the 'JavaScript' but not 'HTML5' or 'ECMAScript', you can search as the below example. Example
Text Search ScoreThe text search provides a score to each document representing the relevancy of the document with the search query. This score can be used to sort all the records returned in the search result. A higher score will indicate a most relevant match. Example
Stop WordsThe $text operator filters out the language-specific stop words, such as a, an, the and in English. The below search will not return any document in the result. Example
Stemmed WordsThe $text operator matches on the complete stemmed word. So if some document field contains the word learning or learn, a search on the term learning or learn would result in the same. Example
ConclusionI hope you learned something new today. Here is an interesting article on Self-Hosted MongoDB. I also invite you to try stuff on your own and share your experience in the comment section. Furthermore, if you face any problems with any of the above definitions, please feel free to ask me in the comments section below. |