【问题标题】:Elasticsearch: search score puzzle me. Same score for different match levelsElasticsearch:搜索分数让我困惑。不同匹配级别的相同分数
【发布时间】:2023-04-07 18:12:01
【问题描述】:

为了简化:

PUT /test/vendors/1
{
  "type": "doctor",
  "name": "Ron",
  "place": "Boston"  
}

PUT /test/vendors/2
{
  "type": "doctor",
  "name": "Tom",
  "place": "Boston"  

}

PUT /test/vendors/3
{
  "type": "doctor",
  "name": "Jack",
  "place": "San Fran"  

}

然后搜索:

GET /test/_search
{
  "query": {
    "multi_match" : {
      "query":    "doctor in Boston", 
      "fields": [ "type", "place" ] 
    }
  }
}

我明白为什么我会找到在旧金山工作的Jack——因为他也是doctor。但是,我不明白为什么比赛得分对他来说是一样的。另外两个也与place 匹配,不是吗?为什么RonTom 得分不高?

{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.9245277,
    "hits": [
      {
        "_index": "test",
        "_type": "vendors",
        "_id": "2",
        "_score": 0.9245277,
        "_source": {
          "type": "doctor",
          "name": "Tom",
          "place": "Boston"
        }
      },
      {
        "_index": "test",
        "_type": "vendors",
        "_id": "1",
        "_score": 0.9245277,
        "_source": {
          "type": "doctor",
          "name": "Ron",
          "place": "Boston"
        }
      },
      {
        "_index": "test",
        "_type": "vendors",
        "_id": "3",
        "_score": 0.9245277,
        "_source": {
          "type": "doctor",
          "name": "Jack",
          "place": "San Fran"
        }
      }
    ]
  }
}

当找到较少的搜索关键字时,有没有办法强制它得分较低?另外,如果我在这种搜索方面走错了路,并且有更好的模式/方式来做这件事——我很高兴能指出正确的方向。

【问题讨论】:

    标签: elasticsearch kibana


    【解决方案1】:

    您的搜索结构不正确。上面的搜索查询忽略了place 属性,这就是为什么所有文档都获得相同分数的原因(仅考虑type 属性)。原因是works_at是嵌套映射,在搜索时应该区别对待。

    首先,您应该将works_at 定义为嵌套映射(阅读更多here)。然后,您必须调整查询以使用该嵌套映射,请参阅示例 here

    【讨论】:

    • 有趣。没想到。我试试看谢谢!
    • 我尝试移除嵌套。仍然 - 我在所有方面都得到相同的分数(编辑了帖子)。我肯定会听取您关于嵌套的建议,但问题仍然存在,为什么我的搜索结果得分相似,尽管最后一个文档中缺少“波士顿”。
    【解决方案2】:
    GET /test/_search
    {
      "query": {
        "multi_match" : {
          "query":    "doctor in Boston", 
          "fields": [ "type", "place" ],
          "type": "most_fields" .   <---- I WAS MISSING THIS
        }
      }
    }
    

    一旦进入,就会给出正确的结果,其中“San Fran”家伙的得分较低。

    {
      "took": 8,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 3,
        "max_score": 1.2122098,
        "hits": [
          {
            "_index": "test",
            "_type": "vendors",
            "_id": "2",
            "_score": 1.2122098,
            "_source": {
              "type": "doctor",
              "name": "Tom",
              "place": "Boston"
            }
          },
          {
            "_index": "test",
            "_type": "vendors",
            "_id": "1",
            "_score": 1.2122098,
            "_source": {
              "type": "doctor",
              "name": "Ron",
              "place": "Boston"
            }
          },
          {
            "_index": "test",
            "_type": "vendors",
            "_id": "3",
            "_score": 0.9245277,
            "_source": {
              "type": "doctor",
              "name": "Jack",
              "place": "San Fran"
            }
          }
        ]
      }
    }
    

    【讨论】:

    • 你确定这解决了吗?对于所有结果,我仍然得到相同的分数。指向地点字段仍然是错误的,不是吗?
    猜你喜欢
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 2021-12-13
    相关资源
    最近更新 更多