Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB Full-Text Search Score "What does Score means?"

I'm working on a MongoDB project for my school. I have a Collection of sentences, and I do a normal Text search to find the most similar sentence in the collection, this is based on the scoring.

I run this Query

db.sentences.find({$text: {$search: "any text"}}, {score: {$meta: "textScore"}}).sort({score:{$meta:"textScore"}})

Take a look at these results when i query sentences,

"that kicking a dog causes it pain"
----Matched With
"that kicking a dog causes it pain – is not very controversial."
----Give a Result of:
*score: 2.4*


"This sentence have nothing to do with any other"
----Matched With
"Who is the “He” in this sentence?"
----Give a result of:
*Score: 1.0* 

What is the score value? what does it mean? What if I want to show the results that only have similarity of 70% and above.

How can I interpret the score result so I can display a similarity percentage, I'm using C# to do this, but don't worry about the implementation. I don't mind a Pseudo-code solution!

like image 908
Nasri Yatim Avatar asked Mar 27 '17 08:03

Nasri Yatim


People also ask

How does text search work in MongoDB?

MongoDB text search uses the Snowball stemming library to reduce words to an expected root form (or stem) based on common language rules. Algorithmic stemming provides a quick reduction, but languages have exceptions (such as irregular or contradicting verb conjugation patterns) that can affect accuracy.

Can MongoDB do full-text search?

Implement full-text search in MongoDB Atlas Go to any cluster and select the “Search” tab to do so. From there, you can click on “Create Search Index” to launch the process. Once the index is created, you can use the $search operator to perform full-text searches.

How do I search for text in a field in MongoDB?

In MongoDB, we can perform text search using text index and $text operator. Text index: MongoDB proved text indexes that are used to find the specified text from the string content. Text indexes should be either a string or an array of string elements.

How many full text indexes can be created against each collection within a MongoDB database?

You can only create one text index for any given MongoDB collection, but the index can be created using more than one field. In our example collection, there is useful text stored in both the name and description fields of each document. It could be useful to create a text index for both fields.

What is a full text search in MongoDB?

Full text searches are based on indexes which itself come with cost. Indexes rearrange when we new document is added. Full text searches do not cache most recent searches, i.e. in Redis it cache the search and provide better performance and availability of data. In this article we have seen the scenario and the need of MongoDB search.

What happens if there is no text index defined in MongoDB?

Note: If you try to execute any full-text search query on a collection for which there is no text index defined, MongoDB will return an error message instead: In this step, you learned how to use one or multiple words as a text search query, how MongoDB joins multiple words with a logical OR operation, and how MongoDB performs stemming.

What are the features of MongoDB latest version?

As MongoDB latest version provide text search featured by searches on text content. Support case insensitive search on text content. Provide stop-word searches i.e. and, or. Also provide language based stemming rule. i.e. argue, argued, argues, arguing, and argus to the stem argu.

Does MongoDB have search engine optimization?

This search is heavily rely on indexing and SQL like search. This search do not use search engine optimization or web crawling etc features. See the Full text search Part 2 for Redis search. As MongoDB latest version provide text search featured by searches on text content.


2 Answers

Text search assigns a score to each document that contains the search term in the indexed fields. The score determines the relevance of a document to a given search query.

For each indexed field in the document, MongoDB multiplies the number of matches by the weight and sums the results. Using this sum, MongoDB then calculates the score for the document.

The default weight is 1 for the indexed fields.

https://docs.mongodb.com/manual/tutorial/control-results-of-text-search/

like image 155
Danilo Calzetta Avatar answered Nov 15 '22 05:11

Danilo Calzetta


When you use a MongoDB text index, it generates a score for every matching document. This score indicates how strongly your search string matches the document. The higher the score more is the chances of resemblance to the searched text. The score is calculated by:

Step 1: Let the search text = S
Step 2: Break S into tokens (If you are not doing a Phrase search). Let's say T1, T2..Tn. Apply Stemming to each token
Step 3: For every search token, calculate score per index field of text index as follows:
       
score = (weight * data.freq * coeff * adjustment);
       
Where :
weight = user Defined Weight for any field. Default is 1 when no weight is specified
data.freq = how frequently the search token appeared in the text
coeff = ​(0.5 * data.count / numTokens) + 0.5
data.count = Number of matching token
numTokens = Total number of tokens in the text
adjustment = 1 (By default).If the search token is exactly equal to the document field then adjustment = 1.1
Step 4: Final score of document is calculated by adding all tokens scores per text index field
Total Score = score(T1) + score(T2) + .....score(Tn)

So as we can see above a score is influenced by the following factors:

  1. Number of Terms matching with the actual searched text, more the match more will be the score
  2. Number of tokens in the document field
  3. Whether the searched text exactly matches the document field or not

Following is the derivation for one of your document:

Search String = This sentence have nothing to do with any other
Document = Who is the “He” in this sentence?

Score Calculation:
Step 1: Tokenize search string.Apply Stemming and remove stop words.
    Token 1: "sentence"
    Token 2: "nothing"
Step 2: For every search token obtained in Step 1, do steps 3-11:
        
      Step 3: Take Sample Document and Remove Stop Words
            Input Document:  Who is the “He” in this sentence?
            Document after stop word removal: "sentence"
      Step 4: Apply Stemming 
        Document in Step 3: "sentence"
        After Stemming : "sentence"
      Step 5: Calculate data.count per search token 
              data.count(sentence)= 1
              data.count(nothing)= 1
      Step 6: Calculate total number of token in document
              numTokens = 1
      Step 7: Calculate coefficient per search token
              coeff = ​(0.5 * data.count / numTokens) + 0.5
              coeff(sentence) =​ 0.5*(1/1) + 0.5 = 1.0
              coeff(nothing) =​ 0.5*(1/1) + 0.5 = 1.0    
      Step 8: Calculate adjustment per search token (Adjustment is 1 by default. If the search text match exactly with the raw document only then adjustment = 1.1)
              adjustment(sentence) = 1
              adjustment(nothing) =​ 1
      Step 9: weight of field (1 is default weight)
              weight = 1
      Step 10: Calculate frequency of search token in document (data.freq)
           For ever ith occurrence, the data frequency = 1/(2^i). All occurrences are summed.
            a. Data.freq(sentence)= 1/(2^0) = 1
            b. Data.freq(nothing)= 0
      Step 11: Calculate score per search token per field:
         score = (weight * data.freq * coeff * adjustment);
         score(sentence) = (1 * 1 * 1.0 * 1.0) = 1.0
         score(nothing) = (1 * 0 * 1.0 * 1.0) = 0
Step 12: Add individual score for every token of search string to get total score
Total score = score(sentence) + score(nothing) = 1.0 + 0.0 = 1.0 

In the same way, you can derive the other one.

For more detailed MongoDB analysis, check: Mongo Scoring Algorithm Blog

like image 45
Ananya Avatar answered Nov 15 '22 03:11

Ananya