【问题标题】:Elasticsearch - Applying multi level filter on nested aggregation bucket?Elasticsearch - 在嵌套聚合桶上应用多级过滤器?
【发布时间】:2019-02-19 21:40:18
【问题描述】:

我正在尝试通过应用多个过滤器来获得不同的嵌套对象。

基本上在 Elasticsearch 中,我将城市作为顶级文档,在内部我有嵌套的公民文档,其中还有另一个嵌套的宠物文档。

我正在尝试让所有满足特定条件的所有公民都适用于所有这 3 个级别(城市、公民和宠物):

Give me all distinct citizens 
that have age:"40", 
that have pets "name":"Casper",
from cities with office_type="secondary" 

我知道要过滤第一级我可以使用查询条件,然后如果我需要过滤嵌套的公民,我可以在聚合级别添加一个过滤器。

我以这篇文章为例:https://iridakos.com/tutorials/2018/10/22/elasticsearch-bucket-aggregations.html

查询目前有效:

GET city_offices/_search
{
  "size" : 10,
   "query": {
    "term" : { "office_type" : "secondary" } 
  },
  "aggs": {
      "citizens": {
        "nested": {
          "path": "citizens"
        },
        "aggs": {
          "inner_agg": {
            "filter": {
                "term": { "citizens.age": "40" }  
              } ,
              "aggs": {
                  "occupations": {
                    "terms": {
                      "field": "citizens.occupation"
                    }
                  }
              }
          }
        }
      }
    }
}

但是:如何添加“宠物”嵌套过滤条件?

映射:

PUT city_offices
{
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "doc": {
      "properties": {
        "city": {
          "type": "keyword"
        },
        "office_type": {
          "type": "keyword"
        },
        "citizens": {
          "type": "nested",
          "properties": {
            "occupation": {
              "type": "keyword"
            },
            "age": {
              "type": "integer"
            },
            "pets": {
              "type": "nested",
              "properties": {
                "kind": {
                  "type": "keyword"
                  },
                "name": {
                  "type": "keyword"
                },
                "age": {
                  "type": "integer"
                }
              }
            }
          }
        }
      }
    }
  }
}

索引数据:

PUT /city_offices/doc/1
{
   "city":"Athens",
   "office_type":"secondary",
   "citizens":[      
      {
         "occupation":"Statistician",
         "age":30,
         "pets":[
            {
               "kind":"Cat",
               "name":"Phoebe",
               "age":14
            }
         ]
      },
      {
         "occupation":"Librarian",
         "age":30,
         "pets":[
            {
               "kind":"Rabbit",
               "name":"Nino",
               "age":13
            }
         ]
      },   
      {
         "occupation":"Librarian",
         "age":40,
         "pets":[
            {
               "kind":"Rabbit",
               "name":"Nino",
               "age":13
            }
         ]
      },      
      {
         "occupation":"Statistician",
         "age":40,
         "pets":[
            {
               "kind":"Rabbit",
               "name":"Casper",
               "age":2
            },
            {
               "kind":"Rabbit",
               "name":"Nino",
               "age":13
            },
            {
               "kind":"Dog",
               "name":"Nino",
               "age":15
            }
         ]
      }   
   ]
}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    所以我找到了解决方案。 基本上我在查询部分应用顶级过滤器,然后在聚合中应用其余条件。

    首先我应用公民级别过滤器聚合,然后进入嵌套宠物内部并应用过滤器,然后我需要回到公民级别(使用 reverse_nested:公民),然后设置将生成最终存储桶的术语。

    查询如下所示:

    GET city_offices/_search
    {
      "size" : 10,
       "query": {
        "term" : { "office_type" : "secondary" } 
      },
      "aggs": {
          "citizens": {
            "nested": {
              "path": "citizens"
            },
            "aggs": {
              "inner": {
                "filter": {
                    "term": { "citizens.age": "40" }  
                  } ,
                  "aggs": {
                      "occupations": {
                        "nested": {
                          "path": "citizens.pets"
                        },
                        "aggs": {
                          "inner_pets": {
                            "filter": {
                                "term": { "citizens.pets.name": "Casper" }  
                              } ,
                               "aggs": {
                                 "lll": {
                                   "reverse_nested": {
                                      "path": "citizens"
                                    },
                                    "aggs": {
                                       "xxx": {
                                          "terms": {
                                            "field": "citizens.occupation",
                                            "size": 10
                                          }
                                      }
                                    }
                                  }
                               }
                          }
                        }
                      }
                  }
              }
            }
          }
        }
    }
    

    响应桶如下所示:

                  "xxx": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [
                      {
                        "key": "Librarian",
                        "doc_count": 1
                      },
                      {
                        "key": "Statistician",
                        "doc_count": 1
                      }
                    ]
                  }
    

    还有其他建议吗?

    【讨论】:

      猜你喜欢
      • 2017-04-15
      • 2018-01-30
      • 2014-12-31
      • 2015-11-14
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      • 2016-12-04
      • 1970-01-01
      相关资源
      最近更新 更多