1

I store the a field of value like 'AA', but when using terms aggregation in search, it returns the value in lowercase like 'aa'.

when search with aggs like this:

POST /113/_search
{
  "query": {
    "query_string": {
      "query": "Jack"
    }
  }, 
    "aggs" : {
      "gender" : {
        "terms" : {
        "field" : "gender"
        }
      },
      "grade" : {
        "terms" : {
            "field" : "grade",
            "order" : {"_count" : "asc"}
        }

      }
    }
}

outputs like this:

{
  "hits": {
    "total": 4,
    "max_score": 1.3862944,
    "hits": [
      {
        "_index": "113",
        "_type": "default_type",
        "_id": "1",
        "_score": 0.07419574,
        "_source": {
          "name": "Tome Jack",
          "grade": "AA",
          "gender": 1
        }
      }
    ]
  },
  "aggregations": {
    "grade": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "b",
          "doc_count": 1
        },
        {
          "key": "c",
          "doc_count": 1
        },
        {
          "key": "aa",
          "doc_count": 2
        }
      ]
    }
  }
}

Why the key become lowercase like 'aa' instead of 'AA'? How to get the key as its original value.

property setting like this:

       "grade": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        },
        "analyzer": "ik_max_word",
        "fielddata": true
      },
Allen_Tsang
  • 555
  • 1
  • 6
  • 14

1 Answers1

0

This is caused because you are using lowercase filter in your ik_max_word analyzer, Elastic search would use index data for aggregation purposes the best approach would be to remove lowercase filter or have another mapping for the field that are not Analyzed for aggregation purposes, for more information check ES aggregation documentation here.

Kaveh
  • 1,158
  • 6
  • 16