| by Arround The Web | No comments

MongoDB Full-Text Search

Due to its fast responsiveness, advanced version, sustainability, and strong indexing capabilities, MongoDB is one of the most renowned NoSQL databases. In many cases, MongoDB queries that classify data by looking for precise matches, utilizing significantly larger and less than comparisons, or employing regular expressions will suffice. When it comes to screening against fields with rich textual data, however, these strategies fall short. This guide will help us to create a MongoDB text index and use it to search for documents employing common full-text search queries and criteria.

When we see material employing phrases or keywords, the best example is a Google search. With MongoDB full-text search, we can establish a text index on any column in a document using a string or arrays of strings.

Syntax of the full-text search in MongoDB in Ubuntu 20.04 ?

The syntax mentioned is as follows:

db.Collection_Name.find({$text: {$search: “string”}})

The find() function is used with the following parameters in the syntax above:

  • Collection_Name: refers to the name of the existing collection.
  • Find: performed to search using the find.
  • $text: used to do a collection search that satisfies our objectives.
  • $search: used to perform the search.
  • String: refers to a certain string that we want to look for in the collection, and we can use the search function to find exact words.

Working on the $text operator in MongoDB in Ubuntu 20.04:

Text lists are provided by MongoDB to assist text searches on string content. Any field with a string value or a range of string components can be included in text records. You should have a text record in your collection to do text search questions. Even a collection only has one topic search record. A list can span multiple fields.

How the full-text search performs in MongoDB in Ubuntu 20.04

Now, look at certain examples to better comprehend things. First, we have created a database with the name “myDemo”. Inside this database, we have defined a collection as “fruits”. Then, the insertMany query is used to insert the collection document as the screenshot is displaying the fields and values against these fields for the collection “fruits” document below.

The query “db.fruits.find()” results into the attributes and the entities of the collection “fruits” as follows. We have three documents recorded for the collection of “fruits”.

Now, let’s examine how we can perform a full-text search.

Example # 1: Creating text index in MongoDB in Ubuntu 20.04:

We have to establish a text index on a dataset before you can use MongoDB’s full-text search features. Indexes are unique data structures that isolate a limited piece of data from each text in a collection from the documents themselves. Let’s have a look at how to perform a full-text search.

A text index is built in the same way as a conventional index, instead of defining an ascending/descending order, it defines the text keyword.

Above, we have a query for a full-text search. We have used the createIndex() method to create a text index. We set the two fields “subject” and “content” to the index-type text.

By running the createIndex query on the MongoDB shell, the following output confirms the index creation:

Example # 2: Searches for a word or phrase from full-text in MongoDB in Ubuntu 20.04:

Looking up documents comprising one or more single words is perhaps the most widespread search challenge. Users probably expect the web browser to be adaptable to choosing where the particular search phrases should display. When employing text indexes, MongoDB approaches common search queries in the same way. With a few examples, this step explains how MongoDB processes search requests.

Here, we have a query “db.fruits.find()”. The query employs the $text operator, which informs MongoDB here that the query will make use of the text index we previously defined. Also, only one text index is allowed in each collection. Then, inside the $text operator we have another operator called $search which is used to search the value “sweet” from the given documents.

As you can see, we have only one document with the text content “sweet”. By running the above query, the entire details of the document having the text content “sweet” are displayed as follows:

Now, we are searching for two words by using the following query:

We have given the two-word “vitamin C” to the $search operator which is called inside the $text operator. When the query is ran, it shows the document record having the vitamin C listed in the text as follows:

Example # 3: Scoring and sorting the full-text search results in MongoDB in Ubuntu 20.04:

Each document receives a score from the text search that indicates how relevant it is to the search query. This score is used to categorize all of the records in a search result. A higher score means the contest is more meaningful.

We have a $text operator that searches the two words “Mangoes” and “Orange” with the help of a $search operator. Then, we have a projection {score: $meta: “textScore”} which makes use of the $meta operator, which returns specified metadata from retrieved documents. The textScore metadata, which is a built-in component of MongoDB’s full-text search engine and holds the search relevance score, is returned in this case.

As mentioned in the filter document, the resulting documents will add a new field named score after executing the query:

Now, we have used sort function for the projection {score: $meta: “textScore”}. The sorting document uses the same syntax as the projection document.

The text mango has the greatest relevancy score, so it comes first on the output screen.

Conclusion:

We have acquired how to use MongoDB’s full-text search functionalities by following this tutorial. You built a text index and composed text search queries that included one and many words, entire phrases, and exclusions. You’ve also graded the relevance of the returned papers and ordered the search results to display the most relevant items first.

Share Button

Source: linuxhint.com

Leave a Reply