【问题标题】:Elasticsearch - Aggregation on nested documents, using filter with has_childElasticsearch - 对嵌套文档进行聚合,使用带有 has_child 的过滤器
【发布时间】:2015-07-29 13:21:08
【问题描述】:

我的索引文档看起来有点像这样;

- a - Parent doc
 \- b - Nested doc (0 to many relation to a)
 \- c - Child doc (0 to many relation to a)
 \- d - Child doc (0 to many relation to a)

a 和 b 中的信息“从不”更改。 c 和 d 中的信息经常更改,父子关系似乎完美匹配。

在 b 上执行聚合就可以了。使用 c/d 的过滤器/查询也可以正常工作。当我尝试组合“has_child”过滤器 c/d 和 b 的嵌套聚合时,不会返回聚合。在 _hits 正确数量的父项下返回一个文档(总数)。

有没有办法执行这种类型的聚合,或者这是一个限制?

/someindex/a/_search
{
   "size": 0,
   "filter": {
     "has_child": {
       "type": "c",
       "filter": {
          "term": { "someFieldInC": 123}
         }
       }
     }
   }, 
  "aggregations" : {
     "details": {
        "nested": {
           "path": "b" 
         },
         "aggregations" : {
           "details-filter" : {
              "filter" : {
                 "query" : {
                    "term": {"b.someFieldInB": 123}
                 }
              },
              "aggregations" : {
                "some_count" : { 
                   "terms" : { 
                      "field" : "b.someCountFieldInB"
                   } 
                }
             }
           }
        }
     }
   }
}

给予

{
  "took":22,
  "timed_out":false,
  "_shards": {
     "total":5,
     "successful":5,
     "failed":0
  },
  "hits":{
     "total":20134,
     "max_score":0.0,
     "hits":[]
  }
}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您需要告诉您的过滤器它也在导航嵌套文档。修改您的请求如下:

      "aggregations" : {
         "details": {
            "nested": {
               "path": "b" 
             },
             "aggregations" : {
               "details-filter" : {
                  "filter" : {
                     "nested": {
                        "path": "b",
                         "filter": {
                             "query" : {
                                "term": {"b.someFieldInB": 123}
                             }
                         }
                     }
                  },
                  "aggregations" : {
                    "some_count" : { 
                       "terms" : { 
                          "field" : "b.someCountFieldInB"
                       } 
                    }
                 }
               }
            }
         }
    

    【讨论】:

    • 谢谢 Eric,我试过了,但没有效果。我找到了一个解决方案,只需将我的子过滤器移动到聚合的顶部。
    【解决方案2】:

    经过一番挖掘,我找到了答案。

    在我的嵌套聚合之前将子过滤器移动到顶部的聚合中;

    "aggregations" : {
       "childfilter" : { 
          "filter": {
             "has_child": {
                "type": "c",
                "query": {
                   "match": {"someFieldInC": 123}
                 }
             }
        },
        "aggregations" : {
           "details": {
              "nested": {
       . . . .
    

    【讨论】:

      猜你喜欢
      • 2018-11-19
      • 2020-09-05
      • 2020-07-26
      • 2018-01-30
      • 2014-12-31
      • 2015-05-06
      • 2021-04-19
      • 1970-01-01
      • 2019-10-20
      相关资源
      最近更新 更多