我如何聚合_score

时间:2016-08-03 08:05:30

标签: elasticsearch aggregate

我尝试在Elastic Search上的_score字段上运行聚合查询,但没有结果。似乎无法使用_score字段,可能是因为它不是文档的字段。我怎样才能在_score上聚合?

这是我的疑问:

{
  "_source": false, "explain": false, "from": 0, "size": 0,
  "aggs" : {
    "score_ranges" : {
       "range" : {
          "field" : "_score",
          "ranges" : [
            { "to"   : 50 },
            { "from" : 50, "to" : 75 },
            { "from" : 75 }
          ]
        }
      }
  },
  "query": {
    "function_score": {
      "query": {
        "match_all": { }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

  "aggs": {
    "scores_histogram": {
      "histogram": {
        "script": "return _score.doubleValue() * 10",
        "interval": 3
      }
    }
  }

或,ranges

  "aggs": {
    "score_ranges": {
      "range": {
        "script": "_score",
        "ranges": [
          {
            "to": 50
          },
          {
            "from": 50,
            "to": 75
          },
          {
            "from": 75
          }
        ]
      }
    }
  }

您需要启用动态脚本。