Text index for search in MongoDB

Text Indexes for search in MongoDB

Text index stores a text as a separate words after removing all the stop words to provide you with text search queries on a string content.

  • A collection can have at most one text index.
  • A text index can only be applied to string or array of strings.

Create Text Index

To create a text index, we'll use the createIndex method. To index a string field that contains strings or array of strings, we'll use "text" in the type of index instead of the default values of the simple indexes (1 or -1).

db.collection.createIndex({field: "text"})

Example of search in text index

Inserting data

Consider a collection named articles that holds documents that resemble the following document:

{name: "Mongodb using text index for searching"}
{name: "Mongodb indexes"}
{name: "MongoDB vs relational database"}

Create text index

Single text index
db.articles.createIndex({name: "text"})
Compound text index
db.articles.createIndex({name: "text", description: "text"})

Querying text index field

Searching for a specific separate words on text index

The following search will search for every document that its name contains MongoDB or database.

db.articles.find({$text: {$search: "MongoDB database"}})

Result

{ "_id" : ObjectId("5e40896fdfa432477f12280c"), "name" : "MongoDB vs relational database" }
{ "_id" : ObjectId("5e40896fdfa432477f12280b"), "name" : "Mongodb indexes" }
{ "_id" : ObjectId("5e40896fdfa432477f12280a"), "name" : "Mongodb using text index for searching" }

Searching with mandatory words on text index

The following search will search for every documents that its name contains "text index" mandatory and the other ones are not. To specify a required text we put the text inside: \".

db.articles.find({$text: {$search: "\"text index\" mongodb"}})

Result

{ "_id" : ObjectId("5e40896fdfa432477f12280a"), "name" : "Mongodb using text index for searching" }

Excluding words on text index

The following search will search for every documents that its name contains MongoDB and not containing "relational" to do so we add the "-" before the word.

db.articles.find({$text: {$search: "mongodb -relational"}})

Result

{ "_id" : ObjectId("5e40896fdfa432477f12280b"), "name" : "Mongodb indexes" }
{ "_id" : ObjectId("5e40896fdfa432477f12280a"), "name" : "Mongodb using text index for searching" }

Sorting text index field

As every other search in the world, the user would like to have the documents that contains all the words he specifies first or the documents that its name contains the words first before the description. To do so we use the weights of fields and here is the syntax:

Creating indexes with weights

db.articles.createIndex(
	{name: "text", description: "text"},
	{
		"weights" :
		{
			name: 20,
			description: 5
		}
	}
)

Sorting based on weights

db.articles.find({
	$text: {$search: "mongodb index"}},
   {score: { $meta: "textScore" }}
).sort({score: { $meta: "textScore" }})