在与文档相同的属性上对Elasticsearch存储桶进行排序

时间:2017-06-16 17:03:46

标签: sorting elasticsearch aggregation

有一种方法可以按多个属性对文档进行排序。 我想通过相同的属性以相同的方式对聚合桶进行排序。这可能吗?

以下是我的示例文档:

    curl -XPOST 'localhost:9200/_bulk?pretty' -H 'Content-Type: 
application/json' -d'
    { "index" : { "_index" : "cars", "_type" : "cars", "_id" : "1" } }
    { "make":"BMW", "series":"3er", "doorCount": 4}
    { "index" : { "_index" : "cars", "_type" : "cars", "_id" : "2" } }
    { "make":"BMW", "series":"3er", "doorCount": 5}
    { "index" : { "_index" : "cars", "_type" : "cars", "_id" : "3" } }
    { "make":"Opel", "series":"Astra", "doorCount": 2}
    { "index" : { "_index" : "cars", "_type" : "cars", "_id" : "4" } }
    { "make":"Opel", "series":"Omega", "doorCount": 2}
    '

这里我将fielddata设置为true以便能够聚合:

curl -XPUT 'localhost:9200/cars/_mapping/cars?pretty' -H 'Content-Type: application/json' -d'
{
  "properties": {
    "make": { 
      "type":     "text",
      "fielddata": true
    },
    "series": { 
      "type":     "text",
      "fielddata": true
    }
  }
}
'

以下是查询汇总存储区:

curl -XGET 'localhost:9200/cars/cars/_search?pretty' -d'
{
  "size": 0,
  "aggregations" : {
    "CARS" : {
      "terms" : {
        "script" : {
          "inline" : "doc[\"make\"].value + \"_\" + doc[\"series\"].value",
          "lang" : "painless"
        }      
      },
      "aggregations" : {
        "make" : {
          "terms" : { "field" : "make"}
        },
        "series" : {
          "terms" : { "field" : "series" }
        }
      }
    }
  }
}
'

知道如何按make和系列对存储桶进行排序吗?

1 个答案:

答案 0 :(得分:0)

首先,您可能希望将makeseries指定为keyword类型而不是text,因为它不需要进行分析(您需要这些领域的完全匹配)。

你的输出是什么样的?当我尝试它时,它似乎正确排序。

  [
    {
      "key": "bmw_3er",
      "doc_count": 2,
      "series": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": "3er",
            "doc_count": 2
          }
        ]
      },
      "make": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": "bmw",
            "doc_count": 2
          }
        ]
      }
    },
    {
      "key": "opel_astra",
      "doc_count": 1,
      "series": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": "astra",
            "doc_count": 1
          }
        ]
      },
      "make": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": "opel",
            "doc_count": 1
          }
        ]
      }
    },
    {
      "key": "opel_omega",
      "doc_count": 1,
      "series": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": "omega",
            "doc_count": 1
          }
        ]
      },
      "make": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": "opel",
            "doc_count": 1
          }
        ]
      }
    }
  ]