# Aggregations negate filter (or aggs excepts filter on same column)

**URL:** https://discuss.elastic.co/t/aggregations-negate-filter-or-aggs-excepts-filter-on-same-column/38052
**Category:** Elasticsearch
**Created:** [December 28, 2015, 3:12pm UTC](https://discuss.elastic.co/t/aggregations-negate-filter-or-aggs-excepts-filter-on-same-column/38052 "2015-12-28T15:12:03Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![JamesRadar](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@JamesRadar](https://discuss.elastic.co/u/JamesRadar)
#### Post date: [December 28, 2015, 3:12pm UTC](https://discuss.elastic.co/t/aggregations-negate-filter-or-aggs-excepts-filter-on-same-column/38052/1 "2015-12-28T15:12:03Z")

</div>

Hi,

I have query like this.

```
{
  "query" : {
      "bool": {
        "must" : [
            {
                "term" : {
                    "active": 1
                }
            },
            {
                "term" : {
                    "categories.e_category_id": 3
                }
            },
            {
                "has_child" : {
                    "type" : "e_params",
                    "query" : {
                        "bool" : {
                            "must" : [
                                {
                                    "term" : {
                                        "param_type_id": 191
                                    }
                                },
                                {
                                    "term" : {
                                        "param_value": "2,5"
                                    }
                                }
                                ]
                        }
                    }
                }
            }
        ]
      }
  },
  "aggs" : {
      "ks" : {
          "children": { 
            "type": "e_params"
          },
          "aggs": {
              "ks" : {
                  "terms" : {
                              "field" : "param_type_url",
                              "size" : 0
                          },
                  "aggs": {
                      "ks" : {
                          "terms" : {
                              "field" : "param_value",
                              "size" : 0
                          }
                      }
                  }
              }
          }
      }
  }
}

```

Is there any way to excepts filter in aggregations? I have aggs on param\_type\_url from type e\_params. And I have filter on that. So the param\_type\_url in this case velikost-baleni returns only document which meets the filter. Like in response bellow.

```
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "ks": {
      "doc_count": 11,
      "ks": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": "barva",
            "doc_count": 3,
            "ks": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                {
                  "key": "tmavě šedá",
                  "doc_count": 3
                }
              ]
            }
          },
          {
            "key": "typ-produktu",
            "doc_count": 3,
            "ks": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                {
                  "key": "lazura",
                  "doc_count": 3
                }
              ]
            }
          },
          {
            "key": "velikost-baleni",
            "doc_count": 3,
            "ks": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                {
                  "key": "2,5",
                  "doc_count": 3
                }
              ]
            }
          },
          {
            "key": "odstin",
            "doc_count": 2,
            "ks": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                {
                  "key": "Stříbrná Grafit",
                  "doc_count": 1
                },
                {
                  "key": "Stříbrná Onyx",
                  "doc_count": 1
                }
              ]
            }
          }
        ]
      }
    }
  }
}

```

And I want to return all values from that params. But I need to apply filter on others param\_type\_url.

Can anyone help me please? I am dealing with that problem whole day and I can solve it only with multiple queries. That is something i hope it can be solve with only one query. Is there any way?

Thanks you!

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [December 28, 2015, 3:30pm UTC](https://discuss.elastic.co/t/aggregations-negate-filter-or-aggs-excepts-filter-on-same-column/38052/2 "2015-12-28T15:30:02Z")

</div>

There are the post filter and aggregation filters that could help here but your current approach mitght be faster. Why is it a problem for you to run several requests? If you're just worrying about the http overhead, you could use the msearch api.

---

<div class="post-metadata">

### Author: ![JamesRadar](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@JamesRadar](https://discuss.elastic.co/u/JamesRadar)
#### Post date: [December 29, 2015, 5:15am UTC](https://discuss.elastic.co/t/aggregations-negate-filter-or-aggs-excepts-filter-on-same-column/38052/3 "2015-12-29T05:15:51Z")

</div>

> [@jpountz](#):
>
> There are the post filter and aggregation filters that could help here but your current approach mitght be faster. Why is it a problem for you to run several requests? If you're just worrying about the http overhead, you could use the msearch api.

I am not using msearch api. I think that many request at once can slow my queries rapidly. But from testing it is look like it isn't.

We want to use one elastic cluster for many projects, so i want to do small number of requests.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 5, 2017, 11:28pm UTC](https://discuss.elastic.co/t/aggregations-negate-filter-or-aggs-excepts-filter-on-same-column/38052/4 "2017-07-05T23:28:11Z")

</div>


