1

elasticsearch 5.2

I want to filter my items by the nested object state.id = 101 and an aggregation count for all the items by state.id. There are items with the state ids 101, 102 and 103.

The aggregation count is now limited to state.id 101. I want the only get the items with state.id = 101 and the aggregation count for all state ids.

How can i do this?

GET index/items/_search
{
  "query": {
        "nested" : {
            "path" : "state",
            "query": {
              "bool": {
                "filter": {
                  "term": { "state.id": 101 }
                }
              }
            }
        }
  },
     "aggs" : {
        "state" : {
            "nested" : {
                "path" : "state"
            },
            "aggs" : {
                "count" : { "terms" : { "field" : "state.id" } }
            }
        }
    }
}

1 Answer 1

2

You need to use post_filter instead of query: (Oh and use POST instead of GET when sending a payload)

POST index/items/_search
{
   "post_filter": {                         <--- change this
      "nested": {
         "path": "state",
         "query": {
            "bool": {
               "filter": {
                  "term": {
                     "state.id": 101
                  }
               }
            }
         }
      }
   },
   "aggs": {
      "state": {
         "nested": {
            "path": "state"
         },
         "aggs": {
            "count": {
               "terms": {
                  "field": "state.id"
               }
            }
         }
      }
   }
}

If you want to further narrow the nested aggregation, you can also add a filter:

POST index/items/_search
{
   "post_filter": {
      "nested": {
         "path": "state",
         "query": {
            "bool": {
               "filter": {
                  "term": {
                     "state.id": 101
                  }
               }
            }
         }
      }
   },
   "aggs": {
      "narrow": {
         "filter": {
            "nested": {
               "path": "state",
               "query": {
                  "bool": {
                     "filter": {
                        "term": {
                           "state.id": 101
                        }
                     }
                  }
               }
            }
         },
         "aggs": {
            "state": {
               "nested": {
                  "path": "state"
               },
               "aggs": {
                  "count": {
                     "terms": {
                        "field": "state.id"
                     }
                  }
               }
            }
         }
      }
   }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. Exactly what I needed. One more question. I want to narrow down the current aggregation context to a specific set of documents. How can i use filter aggregation in an nested an aggregation. link
Thx. But, I want to filter the aggregation on the nested object. ``` "filter": { "terms": { "state.id": [101,102] } }, ``` And only get the documents from "state.id": 101 like above.
You simply need to replace the filter with the nested one. I've updated my answer
Exactly what I needed. Through your answers, I understand aggregations much better now. Thx.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.