Elasticsearch – Apply Nested Filter on Nested (Inner) Aggregation

           The following query can be used to apply filter on nested (inner) aggregation. In our example aggregationName2 is the nested aggregation. The filter (bold content) in the input JSON will be applied only for the aggregation aggregationName2. aggregationName2 takes in the output of aggregationName1, then applies the filter which is then processed by aggregationName2 for the final results.


GET /index/type/_search
    "aggs": {
        "aggregationName1": {
            "terms": {
                "field": "fieldName1"
             "aggs": {
                 "filtered": {
                     "filter": {
                         "bool": {
                             "must": [{
                                 "term": {
                                     "filterFieldName": filterFieldValue
                     "aggs": {
                         "aggregationName2": {
                             "terms": {
                                 "field": "fieldName2"
    "size": 0

Related Links: 

Web Application for Elasticsearch :
  1. ElasticTab – Elasticsearch to Excel Report (Web Application)
Elasticsearch Plugin:
  1. Elasticsearch Plugin To Generate (Save and E-Mail) Excel Reports
  1. Execute Multiple Search Query in Elasticsearch
  2. Monitor Elasticsearch Servers with Shell Script - E-Mail Notification
  3. Execute Raw Elasticsearch Query using Transport Client – Java API
  4. Elasticsearch – Apply Nested Filter on Nested (Inner) Aggregation
  5. Execute Multiple Search Query in Elasticsearch
  6. Enable CORS to Send Cross Domain Request to Elasticsearch using AJAX
  7. Elasticsearch Java API – Get Index List
  8. Elasticsearch Java API – Get Alias List
  9. Elasticsearch Java API - Get Type List from given Index
  10. Elasticsearch Java API – Get Field List for a given Index and Type
  11. Elasticsearch Java API – Get Index Type List Mapping
  12. Elasticsearch – Use Script Filter/Conditon in Aggregation/Sub-Aggreagtion
  13. Elasticsearch – Compare/ScriptFilter/Condition on Two Fields using Script Filter – REST Query + Java API
  14. Elasticsearch - Date/Time(String)  Add/Subtract Duration - Days,Months,Years,Hours,Minutes,Seconds
  15. Elasticsearch Geo-Shape Slow Indexing Performance - Solved
  16. Chrome Elasticsearch Sense Not Working – Solved
  1. Logstash – Process Log File Once and Exit/Stop Logstash After Reading Log File Once
  2. Measure Logstash Performance using Metrics Filter – Issue/Error in Syntax (Unknown setting ‘message’ for stdout)
  3. Logstash – Process Same Log File (File Input) from Beginning/Start
  4. Create Custom Filter/Plugin to Emit New Events Manually in Logstash
Logstash and Elasticsearch:
  1. Query Elasticsearch Cluster in Filter Section when using Logstash
  2. Custom Elasticsearch Index Name/Type Name based on Events in Logstash
MongoDB and Elasticsearch:
  1. Import Data from Mongo DB to Elasticsearch using Elasticsearch River

2 thoughts on “Elasticsearch – Apply Nested Filter on Nested (Inner) Aggregation

  1. Anonymous

    I’m using your elasticsearch code based php mysql. It’s working fine. I have to implement the multisearch (mean msearch) concept on project.I have tried more steps for developing the msearch but i could not complete.

    Multisearch concept:

    single query can search the data on two index type. One type have no conditions and to listed data.another type have to search the data with conditions.finally combine the results and show on page.

    1.common index with two types like(my_index / type1,type2)

    2. type1 :- To search the title data with no conditions

    3. type2 :- To search the title data with conditions

    shows the result with pagination
    Note : Already i tried bool query but doesn’t work


Leave a Reply