# Filter nested field in aggregation if value isn't indexed or is null

**URL:** https://discuss.elastic.co/t/filter-nested-field-in-aggregation-if-value-isnt-indexed-or-is-null/345949
**Category:** Elasticsearch
**Created:** [October 27, 2023, 6:55pm UTC](https://discuss.elastic.co/t/filter-nested-field-in-aggregation-if-value-isnt-indexed-or-is-null/345949 "2023-10-27T18:55:44Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Raphael\_Fidelis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raphael_fidelis/32/119076_2.png) [@Raphael\_Fidelis](https://discuss.elastic.co/u/Raphael_Fidelis)
#### Post date: [October 27, 2023, 6:55pm UTC](https://discuss.elastic.co/t/filter-nested-field-in-aggregation-if-value-isnt-indexed-or-is-null/345949/1 "2023-10-27T18:55:44Z")

</div>

Hello,

I have been stuck on an issue regarding my aggregation.

I have a mapping similar to as the following:

```auto
{
   "product":{
      "aliases":{},
      "mappings":{
         "properties":{
            "characteristics":{
               "type":"nested",
               "properties":{
                  "key":{
                     "type":"text",
                     "fields":{
                        "keyword":{
                           "type":"keyword",
                           "ignore_above":256
                        }
                     },
                     "norms":false,
                     "analyzer":"accent_filter"
                  },
                  "active":{
                     "type":"boolean"
                  },
                  "order":{
                     "type":"long"
                  },
                  "ranking":{
                     "type":"float"
                  },
                  "value":{
                     "type":"text",
                     "fields":{
                        "keyword":{
                           "type":"keyword"
                        }
                     },
                     "copy_to":[
                        "char_values"
                     ],
                     "norms":false,
                     "analyzer":"accent_filter"
                  }
               }
            },
            "keywords":{
               "type":"text",
               "fields":{
                  "keyword":{
                     "type":"keyword",
                     "ignore_above":256
                  }
               },
               "norms":false,
               "analyzer":"accent_filter"
            },
            "id":{
               "type":"text",
               "fields":{
                  "keyword":{
                     "type":"keyword",
                     "ignore_above":256
                  }
               }
            }
         }
      }
   }
}

```

The field **ranking** may not be present on every object, so as I need to make an aggregation based on this field using "max", I get multiple results with **null**. So I needed to filter/remove every object that doesnt have the field ranking from my aggregation, however, I can't seem to be able to do it.

Example aggregation query:

```auto
{
   "aggs":{
      "keywords":{
         "terms":{
            "field":"keywords.keyword",
            "size":"1",
            "order":{
               "max_score_keyword.value":"desc"
            }
         },
         "aggs":{
            "characteristics":{
               "nested":{
                  "path":"characteristics"
               },
               "aggs":{
                  "key":{
                     "terms":{
                        "field":"characteristics.key.keyword",
                        "size":"1",
                        "order":{
                           "max_score":"desc"
                        }
                     },
                     "aggs":{
                        "max_score":{
                           "max":{
                              "field":"characteristics.score"
                           }
                        },
                        "filter_score":{
                           "filter":{
                              "exists":{
                                 "field":"characteristics.score"
                              }
                           }
                        },
                        "values":{
                           "terms":{
                              "field":"characteristics.value.keyword",
                              "size":"2"
                           }
                        }
                     }
                  }
               }
            },
            "max_score_keyword":{
               "max":{
                  "script":{
                     "source":"return _score"
                  }
               }
            }
         }
      }
   }
}

```

It returns the following:

```auto
{
   "aggregations":{
      "keywords":{
         "doc_count_error_upper_bound":-1,
         "sum_other_doc_count":3690,
         "buckets":[
            {
               "key":"Chuveiro",
               "doc_count":8,
               "max_score_keyword":{
                  "value":581.73291015625
               },
               "characteristics":{
                  "doc_count":110,
                  "chave":{
                     "doc_count_error_upper_bound":-1,
                     "sum_other_doc_count":109,
                     "buckets":[
                        {
                           "key":"Acompanha Cano",
                           "doc_count":1,
                           "filter_score":{
                              "doc_count":0
                           },
                           "max_score":{
                              "value":null
                           },
                           "values":{
                              "doc_count_error_upper_bound":0,
                              "sum_other_doc_count":0,
                              "buckets":[
                                 {
                                    "key":"Sim",
                                    "doc_count":1
                                 }
                              ]
                           }
                        }
                     ]
                  }
               }
            }
         ]
      }
   }
}

```

As you can see, inside each bucket in characteristics I get the doc\_count: 0 and max\_value: null, meaning it doesn't have a value. However, I can't actually filter out the value.

Is there a way I can filter out from the aggregation every characteristic that does not have **ranking** indexed? I went through every possibility I could find online but nothing helped.

Thanks in advance.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [November 24, 2023, 6:55pm UTC](https://discuss.elastic.co/t/filter-nested-field-in-aggregation-if-value-isnt-indexed-or-is-null/345949/2 "2023-11-24T18:55:45Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
