How to exclude terms in nested composite aggregation

╄→гoц情女王★ 提交于 2020-04-30 09:47:11

问题


I am using composite aggregation on nested fields in elasticsearch but I want to exclude some terms from the result.

This aggregation is working:

{
  "size": 0,
  "geo": {
    "communication": {
      "nested": {
        "path": "geo"
      },
      "aggs": {
        "table": {
          "composite": {
            "size": 1000,
            "sources": [
              {"stk1": {"terms": {"field": "geo.src"}}},
              {"stk2": {"terms": {"field": "geo.dest"}}}
            ]
          }
        }
      }
    }
  }
}

But I want to exclude some terms from stk2,

{
  "size": 0,
  "aggs": {
    "geo": {
      "nested": {
        "path": "geo"
      },
      "aggs": {
        "table": {
          "composite": {
            "size": 1000,
            "sources": [
              {"stk1": {"terms": {"field": "geo.src"}}},
              {"stk2": {"terms": {"field": "geo.dest", "exclude":"cancel"}}}
            ]
          }
        }
      }
    }
  }
}

The above query is not working.

UPDATE 1: The result should omit only the array elements and not the entire document containing "cancel".

I am using elastic v6.7


回答1:


I'd suggest using a query to exclude those documents:

{
  "size": 0,
  "aggs": {
    "geo": {
      "nested": {
        "path": "geo"
      }
    },
    "aggs": {
      "filter": {
        "bool": {
          "must_not": {
            "prefix": {
              "geo.dest": "cancel"
            }
          }
        }
      },
      "aggs": {
        "table": {
          "composite": {
            "size": 10,
            "sources": [
              {
                "stk1": {
                  "terms": {
                    "field": "geo.src"
                  }
                }
              },
              {
                "stk2": {
                  "terms": {
                    "field": "geo.dest"
                  }
                }
              }
            ]
          }
        }
      }
    }
  }
}


来源:https://stackoverflow.com/questions/56059780/how-to-exclude-terms-in-nested-composite-aggregation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!