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


(Jakub Vrchota) #1

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!


(Adrien Grand) #2

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.


(Jakub Vrchota) #3

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.


(system) #4