Elasticsearch function_score查询field_value_factor分配相同的分数,即使字段值不同

时间:2019-04-12 17:49:16

标签: elasticsearch

  • 使用Elasticsearch v6.5

嗨, 我在日期字段上对 field_value_factor 使用 function_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.为什么分数将实际字段值修整为较小的值,然后再乘以10000000。所以使用字段值因子的目的不是要对具有较高字段值的那些文档评分更高。

1 个答案:

答案 0 :(得分:0)

如果添加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,因此   无法准确表示,因此您的舍入   看到。