【问题标题】:Elasticsearch 5 | filter nested object and nested aggregation count弹性搜索 5 |过滤嵌套对象和嵌套聚合计数
【发布时间】:2017-02-11 23:38:18
【问题描述】:

弹性搜索 5.2

我想按嵌套对象 state.id = 101 过滤我的项目,并按 state.id 对所有项目进行聚合计数。有状态 ID 为 101、102 和 103 的项目。

聚合计数现在限制为 state.id 101。我希望只获取 state.id = 101 的项目和所有状态 ID 的聚合计数。

我该怎么做?

GET index/items/_search
{
  "query": {
        "nested" : {
            "path" : "state",
            "query": {
              "bool": {
                "filter": {
                  "term": { "state.id": 101 }
                }
              }
            }
        }
  },
     "aggs" : {
        "state" : {
            "nested" : {
                "path" : "state"
            },
            "aggs" : {
                "count" : { "terms" : { "field" : "state.id" } }
            }
        }
    }
}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    你需要使用post_filter而不是查询:(哦,发送有效载荷时使用POST而不是GET)

    POST index/items/_search
    {
       "post_filter": {                         <--- change this
          "nested": {
             "path": "state",
             "query": {
                "bool": {
                   "filter": {
                      "term": {
                         "state.id": 101
                      }
                   }
                }
             }
          }
       },
       "aggs": {
          "state": {
             "nested": {
                "path": "state"
             },
             "aggs": {
                "count": {
                   "terms": {
                      "field": "state.id"
                   }
                }
             }
          }
       }
    }
    

    如果想进一步缩小嵌套聚合,也可以加个过滤器:

    POST index/items/_search
    {
       "post_filter": {
          "nested": {
             "path": "state",
             "query": {
                "bool": {
                   "filter": {
                      "term": {
                         "state.id": 101
                      }
                   }
                }
             }
          }
       },
       "aggs": {
          "narrow": {
             "filter": {
                "nested": {
                   "path": "state",
                   "query": {
                      "bool": {
                         "filter": {
                            "term": {
                               "state.id": 101
                            }
                         }
                      }
                   }
                }
             },
             "aggs": {
                "state": {
                   "nested": {
                      "path": "state"
                   },
                   "aggs": {
                      "count": {
                         "terms": {
                            "field": "state.id"
                         }
                      }
                   }
                }
             }
          }
       }
    }
    

    【讨论】:

    • 谢谢。正是我需要的。还有一个问题。我想将当前聚合上下文缩小到一组特定的文档。如何在嵌套聚合中使用过滤器聚合。 link
    • 谢谢。但是,我想过滤嵌套对象的聚合。 ``` "filter": { "terms": { "state.id": [101,102] } }, ``` 并且只从 "state.id": 101 中获取文档。
    • 您只需将过滤器替换为嵌套过滤器即可。我已经更新了答案
    • 正是我需要的。通过您的回答,我现在更好地理解了聚合。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 2020-02-07
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多