# Elasticsearch aggregation issue with "Filter"

**URL:** https://discuss.elastic.co/t/elasticsearch-aggregation-issue-with-filter/101568
**Category:** Elasticsearch
**Created:** [September 23, 2017, 2:03pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-issue-with-filter/101568 "2017-09-23T14:03:33Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![yesu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yesu/32/48857_2.png) [@yesu](https://discuss.elastic.co/u/yesu)
#### Post date: [September 23, 2017, 2:03pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-issue-with-filter/101568/1 "2017-09-23T14:03:33Z")

</div>

Elasticsearch aggregation issue with "Filter", With "Filter" missing field and alphabetical sorting with lower and uppercase not working

Below is the sol for sorting.

```auto
PUT my_index1
{
   "settings": {
      "analysis": {
         "analyzer": {
            "analyzer_keyword": {
               "filter": "lowercase",
               "tokenizer": "keyword"
            }
         }
      }
   },
   "mappings": {
      "type": {
         "properties": {
            "name": {
               "type": "string",
               "fields": {
                  "sort": {
                     "type": "string",
                     "analyzer": "analyzer_keyword",
                     "fielddata": true
                  },
                  "not_ana": {
                     "type": "string",
                     "index": "not_analyzed"
                  }
               }
            }
         }
      }
   }
}

PUT /my_index1/type/_bulk
{"index":{"_id":1}}
{"name":"Boffey"}
{"index":{"_id":2}}
{"name":"BROWN"}
{"index":{"_id":3}}
{"name":"bailey"}
{"index":{"_id":4}}
{"name":"Böhm"}
{"index":{"_id":5}}
{"name":"bp"}
{"index":{"_id":6}}
{"name":"animal"}
{"index":{"_id":7}}
{"name":"Ample"}
{"index":{"_id":8}}
{"name":"category"}
{"index":{"_id":9}}
{"name":null}

POST /my_index1/type/_search
{
   "size" : 0,
   "aggs": {
      "result": {
         "terms": {
            "field": "name.sort",
            "missing" : "Not Available",
            "order": {
               "_term": "asc"
            }
         },
         "aggs": { 
            "sub_result": { 
               "terms": {
                  "field": "name.not_ana" 
               }
            }
         }
      }
   }
}

Response:
{
   "took": 0,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 12,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "result": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "Not Available",
               "doc_count": 1,
               "sub_result": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": []
               }
            },
            {
               "key": "ample",
               "doc_count": 1,
               "sub_result": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": "Ample",
                        "doc_count": 1
                     }
                  ]
               }
            },
            {
               "key": "animal",
               "doc_count": 1,
               "sub_result": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": "animal",
                        "doc_count": 1
                     }
                  ]
               }
            },
            {
               "key": "bailey",
               "doc_count": 2,
               "sub_result": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": "bailey",
                        "doc_count": 2
                     }
                  ]
               }
            },
            {
               "key": "boffey",
               "doc_count": 2,
               "sub_result": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": "Boffey",
                        "doc_count": 2
                     }
                  ]
               }
            },
            {
               "key": "bp",
               "doc_count": 1,
               "sub_result": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": "bp",
                        "doc_count": 1
                     }
                  ]
               }
            },
            {
               "key": "brown",
               "doc_count": 2,
               "sub_result": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": "BROWN",
                        "doc_count": 2
                     }
                  ]
               }
            },
            {
               "key": "böhm",
               "doc_count": 1,
               "sub_result": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": "Böhm",
                        "doc_count": 1
                     }
                  ]
               }
            },
            {
               "key": "category",
               "doc_count": 1,
               "sub_result": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                     {
                        "key": "category",
                        "doc_count": 1
                     }
                  ]
               }
            }
         ]
      }
   }
}

```

But when i add "filter" its not working as expected, missing field not working. Below is the changed query.

```auto
POST /my_index1/type/_search
{
   "size": 0,
   "aggs": {
      "result": {
         "filter": {
            "bool": {
               "must": [
                  {
                     "range": {
                        "name.sort": {
                           "from": "ample",
                           "to": null,
                           "include_lower": false,
                           "include_upper": true,
                           "boost": 1
                        }
                     }
                  }
               ],
               "disable_coord": false,
               "adjust_pure_negative": true,
               "boost": 1
            }
         },
         "aggs": {
            "result": {
               "terms": {
                  "field": "name.sort",
                  "order": {
                     "_term": "asc"
                  }
               },
               "aggs": {
                  "sub_result": {
                     "terms": {
                        "field": "name.not_ana"
                     }
                  }
               }
            }
         }
      }
   }
}

```

Can somebody help me am I doing anything wrong in query while adding filter?

---

<div class="post-metadata">

### Author: ![yesu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yesu/32/48857_2.png) [@yesu](https://discuss.elastic.co/u/yesu)
#### Post date: [September 23, 2017, 2:08pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-issue-with-filter/101568/2 "2017-09-23T14:08:33Z")

</div>

Could not post entire thing in single topic so adding response here for last query in above post. Filtering is working but "missing" field count is missing.

```auto
{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 12,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "result": {
         "doc_count": 10,
         "result": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
               {
                  "key": "animal",
                  "doc_count": 1,
                  "sub_result": {
                     "doc_count_error_upper_bound": 0,
                     "sum_other_doc_count": 0,
                     "buckets": [
                        {
                           "key": "animal",
                           "doc_count": 1
                        }
                     ]
                  }
               },
               {
                  "key": "bailey",
                  "doc_count": 2,
                  "sub_result": {
                     "doc_count_error_upper_bound": 0,
                     "sum_other_doc_count": 0,
                     "buckets": [
                        {
                           "key": "bailey",
                           "doc_count": 2
                        }
                     ]
                  }
               },
               {
                  "key": "boffey",
                  "doc_count": 2,
                  "sub_result": {
                     "doc_count_error_upper_bound": 0,
                     "sum_other_doc_count": 0,
                     "buckets": [
                        {
                           "key": "Boffey",
                           "doc_count": 2
                        }
                     ]
                  }
               },
               {
                  "key": "bp",
                  "doc_count": 1,
                  "sub_result": {
                     "doc_count_error_upper_bound": 0,
                     "sum_other_doc_count": 0,
                     "buckets": [
                        {
                           "key": "bp",
                           "doc_count": 1
                        }
                     ]
                  }
               },
               {
                  "key": "brown",
                  "doc_count": 2,
                  "sub_result": {
                     "doc_count_error_upper_bound": 0,
                     "sum_other_doc_count": 0,
                     "buckets": [
                        {
                           "key": "BROWN",
                           "doc_count": 2
                        }
                     ]
                  }
               },
               {
                  "key": "böhm",
                  "doc_count": 1,
                  "sub_result": {
                     "doc_count_error_upper_bound": 0,
                     "sum_other_doc_count": 0,
                     "buckets": [
                        {
                           "key": "Böhm",
                           "doc_count": 1
                        }
                     ]
                  }
               },
               {
                  "key": "category",
                  "doc_count": 1,
                  "sub_result": {
                     "doc_count_error_upper_bound": 0,
                     "sum_other_doc_count": 0,
                     "buckets": [
                        {
                           "key": "category",
                           "doc_count": 1
                        }
                     ]
                  }
               }
            ]
         }
      }
   }
}

```

---

<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: [October 21, 2017, 2:08pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-issue-with-filter/101568/3 "2017-10-21T14:08:44Z")

</div>

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