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

Hello,

I have been stuck on an issue regarding my aggregation.

I have a mapping similar to as the following:

{
   "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:

{
   "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:

{
   "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.

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