简体   繁体   English

弹性搜索中的聚合属性

[英]aggregating properties in elastic search

I have an indexed entry that has optional properties. 我有一个具有可选属性的索引条目。 So, for example, I have entries like this 因此,例如,我有这样的条目

{
  "id":1
  "field1":"XYZ"
},
{
  "id":2
  "field2":"XYZ"
},
{
  "id":3
  "field1":"XYZ"
}

I would like to make an aggregation that will tell me how many entries I have with field1 and field2 populated. 我想进行汇总,以告诉我填充了field1和field2的条目有多少。

The expected result should be: 预期结果应为:

{
"field1":2
"field2":1
}

Is this even possible with elasticsaerch? 使用Elasticsaerch甚至可能吗?

Yes, you can do it like this: 是的,您可以这样做:

POST myindex/_search
{
  "size": 0,
  "aggs": {
    "field_exists": {
      "filters": {
        "filters": {
          "field1": {
            "exists": {
              "field": "field1"
            }
          },
          "field2": {
            "exists": {
              "field": "field2"
            }
          }
        }
      }
    }
  }
}

You'll get an answer like this one: 您会得到这样的答案:

"aggregations" : {
  "field_exists" : {
    "buckets" : {
      "field1" : {
        "doc_count" : 2
      },
      "field2" : {
        "doc_count" : 1
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM