es 嵌套字段(nested)聚合统计/查询

背景:person是嵌套字段,类型type=nested,person.id 是他一个子属性,即: [{person.id}, {person.id}, {person.id}]

目的:统计每个person.id 出现的次数

POST /search_lib/_search?scroll=2m
{
  "aggregations": {
      "test": {
          "nested": {
              "path": "person"
          },
          "aggregations": {
              "tag_bucket": {
                  "terms": {
                      "field": "person.id"
                  }
              }
          }
      }
  }
}

 结果:

…… 
 "aggregations" : {
    "test" : {
      "doc_count" : 3,
      "tag_bucket" : {
        "doc_count_error_upper_bound" : 0,
        "sum_other_doc_count" : 0,
        "buckets" : [
          {
            "key" : "4641944492668096513",
            "doc_count" : 8
          },
          {
            "key" : "4641944814140526593",
            "doc_count" : 1
          },
          {
            "key" : "4641944814186663937",
            "doc_count" : 1
          }
        ]
      }
    }
  }
}

目的:查询person.id包含1或2的数据:

POST /search_lib/_search?scroll=2m
{
  "query": {
    "bool": {
      "must": [
      {"nested": {
        "path": "person",
        "query": {
          "terms": {
            "person.id": [
              "1",
              "2",
            ]
          }
        }
      }}
    ]}
  }
}

 

posted @ 2022-07-07 16:27  牛郎  阅读(3000)  评论(0编辑  收藏  举报