ElasticSearch聚合,对数组项进行过滤 [英] ElasticSearch aggregation with filter on array item

查看:143
本文介绍了ElasticSearch聚合,对数组项进行过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用其他一些逻辑来执行聚合请求,但是我不确定是否可行以及如何执行.当我拥有以下文档时,如何请求数组中没有类型"的位置?定义的?我尝试发送关于类型的子聚合过滤器,但是还发送了位置"20".在该聚合上获取doc_count 1.

I want to do an aggregation request with some additional logic, but I'm not sure if it's possible and how to do it. When I have the following documents, how can I requests the locations in the array that do not have a "type" defined? I tried with sending a sub aggregation filter on type, but then also location "20" get doc_count 1 on that aggregation.

在这种情况下,如何对匹配的聚合项进行一些逻辑处理?

How can I do some logic on the matching aggregation item in this case?

文档:

    //document1
{
        "locations": [{
                "code": "20",
                "names": [{
                        "languageCode": "en-GB",
                        "value": "Amsterdam"
                    }
                ]
            }, {
                "type": {
                    "id": 25,
                    "names": [{
                            "languageCode": "en-GB",
                            "value": "area"
                        }
                    ]
                },
                "code": "21",
                "names": [{
                        "languageCode": "en-GB",
                        "value": "Amsterdam-South"
                    }
                ]
            }
        ]
    }
//Document 2
    {
        "locations": [{
                "code": "22",
                "names": [{
                        "languageCode": "en-GB",
                        "value": "DenHague"
                    }, {
                        "languageCode": "nl-NL",
                        "value": "DenHaag"
                    }
                ]
            }
        ]
    }

请求:

{
    "aggs": {
        "Filter_Location": {
            "aggs": {
                "SubType": {
                    "filter": {
                        "exists": {
                            "field": "locations.type"
                        }
                    }
                }           },
            "terms": {
                "field": "locations.code.keyword"
            }
        }
    },
    "size": 0
}

结果:

{
    "aggregations": {
        "Filter_Location": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [{
                    "key": "20",
                    "doc_count": 1,
                    "SubType": {
                        "doc_count": 1
                    },
                    "groupByAccoId": {
                        "value": 1
                    }
                }, {
                    "key": "21",
                    "doc_count": 1,
                    "SubType": {
                        "doc_count": 1
                    },
                    "groupByAccoId": {
                        "value": 1
                    }
                }, {
                    "key": "22",
                    "doc_count": 1,
                    "SubType": {
                        "doc_count": 0
                    },
                    "groupByAccoId": {
                        "value": 1
                    }
                }
            ]
        }
    }
}

预期结果:

{
    "aggregations": {
        "Filter_Location": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [{
                    "key": "20",
                    "doc_count": 1,
                    "SubType": {
                        "doc_count": 0
                    }
                }, {
                    "key": "21",
                    "doc_count": 1,
                    "SubType": {
                        "doc_count": 1
                    }
                }, {
                    "key": "22",
                    "doc_count": 1,
                    "SubType": {
                        "doc_count": 0
                    }
                }
            ]
        }
    }
}

推荐答案

为了防止位置

In order to prevent the locations array flattening you'll need to set your index mapping as nested:

PUT ind
{
  "mappings": {
    "properties": {
      "locations": {
        "type": "nested"
      }
    }
  }
}

在提取文档后,此查询将为您获取所需的结果:

After ingesting the docs this query will fetch you the desired results:

GET ind/_search
{
  "size": 0,
  "aggs": {
    "Filter_Location_parent": {
      "nested": {
        "path": "locations"
      },
      "aggs": {
        "Filter_Location": {
          "terms": {
            "field": "locations.code.keyword"
          },
          "aggs": {
            "SubType": {
              "filter": {
                "exists": {
                  "field": "locations.type"
                }
              }
            }
          }
        }
      }
    }
  }
}

这篇关于ElasticSearch聚合,对数组项进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆