【问题标题】:Multiple documents having equal search score in MongoDB Atlas Search在 MongoDB Atlas Search 中具有相同搜索分数的多个文档
【发布时间】:2022-06-13 10:42:01
【问题描述】:

有没有办法提高 Atlas 搜索中完全匹配的分数?

我在将“hi”从英语翻译成法语时遇到问题。经过一些调试后,我发现从我的聚合返回的前三 (3) 个文档的分数相同,均为“2.362138271331787”。

我预计 'hi' 得分更高,因为它与相同的搜索查询完全匹配,但 'it's his' 和 'his' 似乎与 'hi' 得分相同。

这是我的搜索查询:

const searchOption= [
  {
    $search: {
      text: {
        query: 'hi',
        path: 'english',
      },
    },
  },
  { $project: {  _id: 0, french: 1, english: 1, score: { $meta: "searchScore" } } },
  { $limit: 5 },
];

const result = await Greetings.aggregate(searchOption, { cursor: { batchSize: 5 } }).toArray();

这是退回的文件。该列表按搜索分数排序:

[
  {
    "english": "it’s his",
    "french": "c'est le sien",
    "score": 2.362138271331787
  },
  {
    "english": "hi",
    "french": "salut",
    "score": 2.362138271331787
  },
  {
    "english": "his",
    "french": "le sien",
    "score": 2.362138271331787
  },
  {
    "english": "it’s his failure to arrange his",
    "french": "c'est son incapacité à organiser son",
    "score": 2.2482824325561523
  },
  {
    "english": "it’s his failure to arrange his time",
    "french": "c'est son incapacité à organiser son temps",
    "score": 2.0995540618896484
  }
]

【问题讨论】:

    标签: javascript database mongodb non-relational-database mongodb-atlas-search


    【解决方案1】:

    这是 Atlas Search 的一个已知限制,此处提到了解决方案: https://www.mongodb.com/docs/atlas/atlas-search/autocomplete/#limitations

    string 类型上的 lucene.keyword 分析器非常有助于在分数保真度至关重要的场景中进行精确匹配。

    基本上,路径english应该在索引定义中定义为自动完成和字符串,如:

    [
      {"type": "string"},
      {"type": "autocomplete"}
    ]
    

    以上假设您没有使用自动完成或字符串的语言分析器,这可能不是字符串的理想选择。

    然后,在查询方面,您需要一个compound 查询,其中两个选项都是should 子句。您应该增加 text 子句而不是自动完成子句。

    【讨论】:

      【解决方案2】:

      分数是 Mongo 内部实现的“相关性分数”,我会说令我惊讶的是,即使是“文本”操作数,字段长度也不包含在分数中,我个人希望添加它在不久的将来以某种形式出现。

      现在您可以使用解决方法来构建您想要的分数,例如,您可以使用 should(或)表达式与 phrase 运算符结合提升分数函数,如下所示:

      const searchOption= [
          {
              $search: {
                  "compound": {
                      "should" : [
                          {
                              "phrase":{
                                  "query": "hi",
                                  "path": "english",
                                  "score": {"boost":{"value":5}} 
                              }
                          },
                          {
                              text: {
                                  query: 'hi',
                                  path: 'english',
                              },
                          },
                      ]
                  }
              }
          },
          { $project: {  _id: 0, french: 1, english: 1, score: { $meta: "searchScore" } } },
          { $limit: 5 },
      ];
      
      const result = await Greetings.aggregate(searchOption, { cursor: { batchSize: 5 } }).toArray();
      

      否则,您也可以只按english 长度和分数进行排序(这是假设分数将被捆绑),显然这不是真正的排序,因为它假设前 5 个结果是实际的前 5 个结果你期望得到。

      const searchOption= [
          {
              $search: {
                  text: {
                      query: 'hi',
                      path: 'english',
                  },
              },
          },
          { $project: {  _id: 0, french: 1, english: 1, score: { $meta: "searchScore" }, len: {$strLenCP: "$english"} } },
          { $sort : { score: -1, len: -1 } },
          { $limit: 5 },
      ];
      
      const result = await Greetings.aggregate(searchOption, { cursor: { batchSize: 5 } }).toArray();
      

      【讨论】:

        猜你喜欢
        • 2021-06-01
        • 2021-12-26
        • 1970-01-01
        • 2020-10-22
        • 1970-01-01
        • 2023-03-28
        • 2022-09-23
        • 2021-02-02
        • 2021-12-10
        相关资源
        最近更新 更多