ElasticSearch通过多列对SQL组进行等效查询

时间:2017-05-08 00:22:34

标签: elasticsearch elasticsearch-plugin

我有几百万个文件,其中包含名称和版本(都是类型关键字)作为每个文件的属性。按名称分组的等效弹性查询是什么版本?

我尝试了以下查询:

{
"size":0,
"query": {
    "bool": {
        "filter": {
            "range": {
                "time": {
                    "gte": "2017-01-28",
                    "lte": "2017-02-28"
                }
            }
        }
    }
},
"aggs": {
    "group_by_name": {
        "terms": {
            "field": "name"
        },
        "aggs": {
            "group_by_version": {
                "terms": {
                    "field": "version"
                }   
            }
        }   
    }       
}

}

但是,结果与按名称,版本进行分组的结果不同。 结果按名称分组,在每个组中,它们按版本分组。 如何将上述查询修改为按名称,版本元组分组并按降序返回结果?

非常感谢您的帮助。

更新: 我得到的是:

{
  "took": 1424,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 115,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "group_by_name": {
      "doc_count_error_upper_bound": 2,
      "sum_other_doc_count": 115,
      "buckets": [
        {
          "key": "product1",
          "doc_count": 50,
          "group_by_version": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 50,
            "buckets": [
              {
                "key": "1.0",
                "doc_count": 40
              },
              {
                "key": "2.0",
                "doc_count": 10
              },
            ]
          }
        },
        {
          "key": "product3",
          "doc_count": 35,
          "group_by_version": {
            "doc_count_error_upper_bound": 4,
            "sum_other_doc_count": 35,
            "buckets": [
              {
                "key": "8.0",
                "doc_count": 20
              },
              {
                "key": "9.0",
                "doc_count": 15
              }
            ]
          }
        },
        {
          "key": "product2",
          "doc_count": 30,
          "group_by_version": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 30,
            "buckets": [
              {
                "key": "4.0",
                "doc_count": 25
              },
              {
                "key": "5.0",
                "doc_count": 5
              }
            ]
          }
        }
      ]
    }
  }
}

我想要的是:

name, version   count
product1 1.0    40
product2 4.0    25
product3 8.0    20
product3 9.0    15
product1 2.0    10
product2 5.0     5

0 个答案:

没有答案