elasticsearch - ElasticSearch聚合,对数组项进行过滤

标签 elasticsearch elasticsearch-aggregation

我想使用一些其他逻辑来执行聚合请求,但是我不确定是否可行以及如何执行。当我拥有以下文档时,如何请求数组中未定义“类型”的位置?我尝试使用类型发送子聚合过滤器,但是位置“20”在该聚合上也获得了doc_count 1。
在这种情况下,如何对匹配的聚合项目执行一些逻辑?
文件:

    //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
                    }
                }
            ]
        }
    }
}

最佳答案

为了防止locations array flattening,您需要将索引映射设置为nested:

PUT ind
{
  "mappings": {
    "properties": {
      "locations": {
        "type": "nested"
      }
    }
  }
}
提取文档后,此查询将为您获取所需的结果:
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 - ElasticSearch聚合,对数组项进行过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64535700/

相关文章:

Elasticsearch 通过另一个文档查找文档

elasticsearch - 设置大小对性能的影响

elasticsearch - ElasticSearch:指标聚合是否支持min_doc_count

elasticsearch - 具有许多元素的术语查询的性能

elasticsearch - ElasticSearch将索引分配给服务器

elasticsearch - 如何在Elasticsearch中获取重复的字段值及其计数

elasticsearch - 在es中按嵌套和非嵌套字段分组

elasticsearch - elasticsearch-获得 'function_score'内的中级分数

elasticsearch - 基于 Elasticsearch 中两个字段的聚合导致 SearchParseExcepetion 为 "cannot find aggregator type"

elasticsearch - 需要帮助将ElasticSearch过滤器 bool 查询从1.7转换为6.7