【问题标题】:MongoDB Full-Text Search Score "What does Score means?"MongoDB全文搜索评分“Score是什么意思?”
【发布时间】:2017-08-19 20:53:47
【问题描述】:

我正在为我的学校开发一个 MongoDB 项目。我有一个句子集合,我做一个普通的文本搜索来找到集合中最相似的句子,这是基于评分的。

我运行这个查询

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

当我查询句子时看看这些结果,

"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* 

什么是分值?这是什么意思? 如果我想显示只有 70% 及以上相似度的结果。

如何解释得分结果以便显示相似度百分比,我正在使用 C# 来执行此操作,但不必担心实现。我不介意伪代码解决方案!

【问题讨论】:

  • 70% 的相似度是什么意思?你想用什么样的分数来衡量相似度?
  • 我实际上正在尝试制作一个抄袭软件,您可以在其中上传文档,然后将每个句子与句子池进行比较。所以,当最高分的句子相似度达到 70% 或更多时,就有抄袭的可能性。
  • @NasriYatim 你找到方法了吗?
  • 嗨 Nasri,我也是 MongoDB 的新手,对我来说,我需要从名称字段中搜索名称“Raja Sekar”,我已将其编入索引。但我的条件是搜索词应该匹配 75% 的相似记录。你能帮我解决这个问题吗

标签: mongodb algorithm full-text-search


【解决方案1】:

当您使用 MongoDB 文本索引时,它会为每个匹配的文档生成一个分数。此分数表明您的搜索字符串与文档的匹配程度。分数越高,与搜索文本相似的机会就越大。分数计算方式:

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)

如上图所示,分数受以下因素影响:

  1. 与实际搜索文本匹配的词条数,匹配越多,得分越高
  2. 文档字段中的标记数
  3. 搜索到的文本是否与文档字段完全匹配

以下是您的一份文件的推导:

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 

同样的方法,你可以推导出另一个。

如需更详细的 MongoDB 分析,请查看: Mongo Scoring Algorithm Blog

【讨论】:

    【解决方案2】:

    文本搜索会为索引字段中包含搜索词的每个文档分配一个分数。分数决定了文档与给定搜索查询的相关性。

    对于文档中的每个索引字段,MongoDB 将匹配数乘以权重并将结果相加。使用这个总和,MongoDB 然后计算文档的分数。

    索引字段的默认权重为 1。

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

    【讨论】:

    • 与其抄袭,不如用例子来解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-09
    • 2011-03-29
    • 2020-12-20
    • 2018-05-04
    • 1970-01-01
    • 2018-11-23
    • 2014-07-20
    相关资源
    最近更新 更多