MongoDB Text Search – Indexing Single & Multiple Fields
Job-ready Online Courses: Knowledge Awaits – Click to Access!
MongoDB Text Search
We can perform MongoDB text search of string content with the help of query operations. $text operator is used to perform this kind of text search in MongoDB.
{ "subject":"Joe", "content":"best friend", "likes": 60, "year":2015, "language":"english" }
Let’s add some sample documents in the collection.
db.examples.insert({"subject":"Joe", "content":"best friend", "likes": 60, "year":2015, "language":"english"}) db.examples.insert({"subject":"Dogs", "content":"Cats", "likes": 30, "year":2015, "language":"english"}) db.examples.insert({"subject":"Cats", "content":"Rats", "likes": 55, "year":2014, "language":"english"}) db.examples.insert({"subject":"Rats", "content":"Joe", "likes": 75, "year":2014, "language":"english"})
i. Indexing a Single Field
db.examples.createIndex({"subject":"text"})
Now, if you want to test this newly created text index on the subject field, we will have to find documents using $text operator. We will look for all documents that have keyword dog in their subject field.
db.examples.find({$text: {$search: "dogs"}}, {score: {$meta: "toextScore"}}).sort({score:{$meta:"textScore"}})
Here, we are using {$meta: “textScore” } to get some statistics about how relevant our resultant document is. After that, we will sort all documents by their textScore using sort command. Higher the textScore, more relevant the match is.
ii. Indexing Multiple Fields
db.examples.dropIndex("subject_text") db.examples.createIndex({"subject":"text","content":"text"})
After writing this query now write another query to find out all documents with keyword cat.
db.messages.find({$text: {$search: "cat"}}, {score: {$meta: "textScore"}}).sort({score:{$meta:"textScore"}})
This query will give us the following output:
iii. Indexing the Entire Document
db.messages.createIndex({"$**":"text"})
This query will set up text indexes on any string fields in our documents. For this insert a new document with a new field location in it.
db.examples.insert({"subject":"Birds", "content":"Rats", "likes": 12, "year":2013, location: "Switzerland", "language":"english"})
Now if we try to search with Switzerland keyword then it will show up the current document we inserted.
db.examples.find({$text: {$search: "switzerland"}}, {score: {$meta: "textScore"}}).sort({score:{$meta:"textScore"}})
iv. Languages Supported by Text Search by MongoDB
A new version is introduced by MongoDB, version 2 for the text search features. Version 1 only supports the long form of each language name. But in version, we can use two-letter language codes defined in ISO 639-1.
Language Name | Two-Letter Code |
Danish | da |
English | en |
Dutch | nl |
Finnish | fi |
French | fr |
German | de |
Hungarian | hu |
Italian | it |
Norwegian | nb |
Portuguese | pt |
Romanian | ro |
Russian | ru |
Spanish | es |
Swedish | sv |
Turkish | tr |
So, this was all about MongoDB Text Search Tutorial. Hope, you like our explanation.
Summary
We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google
Typo toextScore