【问题标题】:Elasticsearch function_score query field_value_factor assigns same score even if field values are differentElasticsearch function_score 查询 field_value_factor 分配相同的分数,即使字段值不同
【发布时间】:2019-09-03 13:06:10
【问题描述】:
  • 使用 Elasticsearch v6.5

嗨, 我在日期字段上使用带有 field_value_factorfunction_score 查询。我的查询如下:

POST /users/_search
{
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },

      "functions": [
        {
          "field_value_factor": {
            "field": "createdAt"
          }
        }
      ]
    }
  }
}

Response
200 OK
{
  "hits": {
    "hits": [
      {
        "_score": 1545536870000000,
        "_type": "_doc",
        "_id": "user1",
        "_source": {
          "createdAt": 1545536877421,
          "firstName": "foo1"
        },
        "_index": "users"
      },
      {
        "_score": 1545536870000000,
        "_type": "_doc",
        "_id": "user2",
        "_source": {
          "createdAt": 1545536877422,
          "firstName": "foo2"
        },
        "_index": "users"
      }
    ],
    "max_score": 1545536870000000
  },
  "took": 17
}

我的问题是: 1.为什么即使字段的值不同,两个文档也返回相同的分数? 2. 为什么 score 将实际字段值修剪成一个较小的值,然后乘以 10000000。使用字段值因子的全部目的不就是对那些具有较高字段值的文档进行评分吗?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    如果您添加explain:true 参数,您将看到您超过了分数的最大值,这就是为什么它们最终都相同的原因。

     "hits": {
    "total": 2,
    "max_score": 1545536930000,
    "hits": [
      {
        "_shard": "[test1][2]",
        "_node": "L0mg3oZdRdSSlah6QPVqjQ",
        "_index": "test1",
        "_type": "doc",
        "_id": "2",
        "_score": 1545536930000,
        "_source": {
          "createdAt": 1545536877422,
          "firstName": "foo2"
        },
        "_explanation": {
          "value": 1545536930000,
          "description": "function score, product of:",
          "details": [
            {
              "value": 1,
              "description": "*:*, product of:",
              "details": [
                {
                  "value": 1,
                  "description": "boost",
                  "details": []
                },
                {
                  "value": 1,
                  "description": "queryNorm",
                  "details": []
                }
              ]
            },
            {
              "value": 1545536930000,
              "description": "min of:",
              "details": [
                {
                  "value": 1545536930000,
                  "description": "field value function: none(doc['createdAt'].value * factor=1.0)",
                  "details": []
                },
                {
                  "value": 3.4028235e+38,
                  "description": "maxBoost",
                  "details": []
                }
              ]
            }
          ]
        }
      },
      {
        "_shard": "[test1][3]",
        "_node": "L0mg3oZdRdSSlah6QPVqjQ",
        "_index": "test1",
        "_type": "doc",
        "_id": "1",
        "_score": 1545536930000,
        "_source": {
          "createdAt": 1545536877421,
          "firstName": "foo1"
        },
        "_explanation": {
          "value": 1545536930000,
          "description": "function score, product of:",
          "details": [
            {
              "value": 1,
              "description": "*:*, product of:",
              "details": [
                {
                  "value": 1,
                  "description": "boost",
                  "details": []
                },
                {
                  "value": 1,
                  "description": "queryNorm",
                  "details": []
                }
              ]
            },
            {
              "value": 1545536930000,
              "description": "min of:",
              "details": [
                {
                  "value": 1545536930000,
                  "description": "field value function: none(doc['createdAt'].value * factor=1.0)",
                  "details": []
                },
                {
                  "value": 3.4028235e+38,
                  "description": "maxBoost",
                  "details": []
                }
              ]
            }
          ]
        }
      }
    ]
    

    }

    引用此github issue link

    最后的_score是一个浮点数,只能表示整数 精确到 2^25。时间戳是 2^40 的数量级,所以 无法准确表示,因此您的四舍五入 看到。

    【讨论】:

      猜你喜欢
      • 2017-09-17
      • 2019-10-11
      • 1970-01-01
      • 1970-01-01
      • 2016-02-01
      • 2017-02-08
      • 1970-01-01
      • 2015-02-01
      • 2015-01-30
      相关资源
      最近更新 更多