功能分数返回所有文档的相同分数

时间:2016-04-04 03:22:52

标签: elasticsearch lucene

我正在使用带有采样器聚合的函数得分来匹配最近访问过的文档。

ES查询

    {
  "query": {
    "function_score": {
      "boost_mode": "replace", // we need to replace document score with the result of the functions,
      "query": {
      },
      "functions": [
        {
          "field_value_factor": { // return `lastvisited` value as score
            "field": "visited_time"
          }
          ,"weight":1
        }
      ]
    }
  },
  "size": 10000
}

响应

    {
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "hits" : {
    "total" : 5,
    "max_score" : 1.45973969E12,
    "hits" : [ {
      "_index" : "idx0",
      "_type" : "8001",
      "_id" : "null-100-1459739724631",
      "_score" : 1.45973969E12,
      "_routing" : "100",
      "_source" : {
        "visited_time" : 1459739724636
      }
    }, {
      "_index" : "idx0",
      "_type" : "8001",
      "_id" : "null-101-1459708570522",
      "_score" : 1.45970862E12,
      "_routing" : "101",
      "_source" : {
        "visited_time" : 1459708570525
      }
    }, {
      "_index" : "idx0",
      "_type" : "8001",
      "_id" : "null-101-1459708599619",
      "_score" : 1.45970862E12,
      "_routing" : "101",
      "_source" : {
        "visited_time" : 1459708599620
      }
    }, {
      "_index" : "idx0",
      "_type" : "8001",
      "_id" : "null-100-1459708476386",
      "_score" : 1.45970849E12,
      "_routing" : "100",
      "_source" : {
        "visited_time" : 1459708476387
      }
    }, {
      "_index" : "idx0",
      "_type" : "8001",
      "_id" : "null-100-1459708421417",
      "_score" : 1.45970836E12,
      "_routing" : "100",
      "_source" : {
        "visited_time" : 1459708421492
      }
    } ]
  }
}

我不知道为什么它会返回相同的文件分数?

1 个答案:

答案 0 :(得分:0)

您的查询很好,但我猜您正在处理的数字的精确度非常高。文档得分为double精度值,而不是long。因此,在将long值转换为double时,精度会有一些损失,因此您会发现某些结果不正常。请注意,只有第二个和第三个结果出现故障。我想除非你处理的是低精度值,否则没有简单的方法可以解决这个问题。

但是,您正在尝试解决的具体问题有一个简单的解决方案。您可以使用sorting而不必处理上述问题。使用以下查询:

{
  "query": {
    // query goes here
  },
  "sort": [
    {
      "visited_time": {
        "order": "desc"
      }
    }
  ],
  "size": 10000
}