Wednesday, March 31, 2021

Implementing Search feature using Text Index in MongoDb

Let's say we have 1,000,000 documents with below data schema:

[{

name : "Simon",

comment: "This is awesome article. Max also pretty good on this.",

score: 29.52711235568646,

age: 40

}, 

{

name : "Max"

comment: "You are an awesome person.",

score:47.148964115304345,

age: 72

}]


We want to implement a search feature where user can search the name and comment. The search weight of the name is higher than the comment. For example, when the user searches for Max on the above 2 documents, the search result will return both documents. However, the document with Max name will show first.


MongoDb has "Text Search" index that performs a text search of string content. The index can easily create using below command:


db.ratings.createIndex( 

{ name: "text", comment: "text" },

{ weights: { name: 10, comment: 5 }} 

)


When creating the index, the collection will be locked, you can't find, update, etc on the collection. You can create the index on the background without lock the collection using the below command. Creating an index background is slower.


db.ratings.createIndex( 

{ name: "text", comment: "text" },

{ weights: { name: 10, comment: 5 }},

{ background: true }

)


Let search Max using the below query:


db.ratings.find( { $text: { $search: "Max" } } )


To display the search score and sort based on the search score, you can use below query:


db.ratings.find( 

{ $text: { $search: "Max" } }, 

{ searchScore: { $meta: "textScore" }} 

).sort( 

{ searchScore: { $meta: "textScore" }} 

).pretty()


Here is the result:

[{

name : "Max"

comment: "You are an awesome person.",

score:47.148964115304345,

age: 72,

searchScore: 11

},{

name : "Simon",

comment: "This is awesome article. Max also pretty good on this.",

score: 29.52711235568646,

age: 40,

searchScore: 5.5

}]


References:

No comments:

Post a Comment