Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude terms in nested composite aggregation

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

like image 317
AbhiArora Avatar asked Nov 24 '25 07:11

AbhiArora


1 Answers

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"
                  }
                }
              }
            ]
          }
        }
      }
    }
  }
}
like image 177
Val Avatar answered Nov 26 '25 17:11

Val