Elasticsearch bool should filter with result limit


(tushar) #1

in the below query I want only 1 results from department "a" and two from "b"
but when I run the query the results are empty. Can anyone please tell
me where am I going wrong

{
                  "query": {
                    "range": {
                      "Date": {
                        "gte": "1440364380",
                         "lte": "1440450780"
                      }
                    }
                  },
                  "filter": {
                    "bool": {
                      "should": [
                        {
                          "term": {
                            "Department": "a"
                             size:1

                                 },
                          "term": {
                            "Department": "b"
                             size:2

                                 }
                        }
                      ]
                    }
                  },"sort": [
                    {
                      "Date": {
                        "order": "desc"
                      }
                    }
                  ]
                }

(Adrien Grand) #2

There is no size parameter on the term query. If you want to get 1 result from department A and 2 from department b, you need to run two queries.


(tushar) #3

Is there any other work around, because I have 20 conditions to match with the limit if I run a loop of individual queries the response is very very slow.


(Adrien Grand) #4

Have you tried running them in a multi-search request? https://www.elastic.co/guide/en/elasticsearch/reference/1.7/search-multi-search.html


(system) #5