'ElasticSearch query with nested filter and nested aggregation

Hi I'm building a elastic search query to do a keyword search between both the root documents and the nested documents, and I need to provide Filter and Aggregation functionality for both nested and root. Here is the query I'm using. I'm running into trouble:

  1. Cannot apply multi filters for nested type. (I can apply multi filter for the root documents).
  2. Cannot convert the multi filter for nested type into Java Code.. (Not sure which class to use for the "query" section - MatchQueryBuilder, or NestedQueryBuilder?
            "bool":  {
              "filter": {
                "nested": {
                  "path": "items",
                  "query": [
                    {
                      "match": {
                        "items.attributes.color.color.colorDescription": "Gray"
                      }
                    }
                  ]
               }
            }
          }

Elastic Search expert, please HELP ME! Thank you!

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "query_string": {
                  "query": "cuce",
                  "default_field": "*",
                  "fields": [],
                  "type": "best_fields",
                  "default_operator": "and"
                }
              },
              {
                "bool": {
                  "must": [
                    {
                      "query_string": {
                        "query": "cuce",
                        "default_field": "*",
                        "fields": [],
                        "type": "best_fields",
                        "default_operator": "and"
                      }
                    },
                    {
                      "nested": {
                        "query": {
                          "bool": {
                            "should": [
                              {
                                "multi_match": {
                                  "query": "cuce",
                                  "fields": [
                                    "items.attributes.color.color.colorDescription^1.0",
                                    "items.attributes.color.colorFamily^1.0"
                                  ],
                                  "type": "best_fields",
                                  "operator": "OR"
                                }
                              }
                            ]
                          }
                        },
                        "path": "items",
                        "score_mode": "max",
                        "inner_hits": {}
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ],
      "filter": [
        {
          "term": {
            "attributes.ageGroup.keyword": "ADULT"
           }
          },
          {
          "term": {
            "attributes.gender.keyword": "FEMALE"
           }
          },
          {
            "bool":  {
              "filter": {
                "nested": {
                  "path": "items",
                  "query": [
                    {
                      "match": {
                        "items.attributes.color.color.colorDescription": "Gray"
                      }
                    }
                  ]
               }
            }
          }
        }
      ]
    }
  },
   "aggregations": {
            "attributes.ageGroup": {
                "terms": {
                    "field": "attributes.ageGroup.keyword",
                    "size": 10,
                    "min_doc_count": 1,
                    "shard_min_doc_count": 0,
                    "show_term_doc_count_error": false,
                    "order": [
                        {
                            "_count": "desc"
                        },
                        {
                            "_key": "asc"
                        }
                    ]
                }
            },
            "attributes.gender": {
                "terms": {
                    "field": "attributes.gender.keyword",
                    "size": 10,
                    "min_doc_count": 1,
                    "shard_min_doc_count": 0,
                    "show_term_doc_count_error": false,
                    "order": [
                        {
                            "_count": "desc"
                        },
                        {
                            "_key": "asc"
                        }
                    ]
                }
            },
             "items.attributes.nationalRetailFederationAttributes.nrfColorCode": {
                "nested": {
                    "path": "items"
                },
                "aggregations": {
                    "items.attributes.nationalRetailFederationAttributes.nrfColorCode": {
                        "terms": {
                            "field": "items.attributes.nationalRetailFederationAttributes.nrfColorCode.keyword",
                            "size": 10,
                            "min_doc_count": 1,
                            "shard_min_doc_count": 0,
                            "show_term_doc_count_error": false,
                            "order": [
                                {
                                    "_count": "desc"
                                },
                                {
                                    "_key": "asc"
                                }
                            ]
                        }
                    }
                }
            },
            "items.attributes.nationalRetailFederationAttributes.nrfSizeLabel": {
                "nested": {
                    "path": "items"
                },
                "aggregations": {
                    "items.attributes.nationalRetailFederationAttributes.nrfSizeLabel": {
                        "terms": {
                            "field": "items.attributes.nationalRetailFederationAttributes.nrfSizeLabel.keyword",
                            "size": 10,
                            "min_doc_count": 1,
                            "shard_min_doc_count": 0,
                            "show_term_doc_count_error": false,
                            "order": [
                                {
                                    "_count": "desc"
                                },
                                {
                                    "_key": "asc"
                                }
                            ]
                        }
                    }
                }
            }
        }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source