# Facets - How to do propper Terms aggregations

**URL:** <https://discuss.elastic.co/t/facets-how-to-do-propper-terms-aggregations/73371>\
**Category:** Elasticsearch\
**Created:** [January 31, 2017, 1:43pm UTC](https://discuss.elastic.co/t/facets-how-to-do-propper-terms-aggregations/73371 "2017-01-31T13:43:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ros](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ros/32/11720_2.png) [@ros](https://discuss.elastic.co/u/ros)\
**Post date:** [January 31, 2017, 1:43pm UTC](https://discuss.elastic.co/t/facets-how-to-do-propper-terms-aggregations/73371/1 "2017-01-31T13:43:24Z")

</div>

Given an index with documents that have a brand property, we need to create a term aggregation that is case insensitive.

**Data size**

- 28 indices
- each with 10.000 documents

**Index definition**

_Please note that the use of fielddata_

```
PUT demo_products
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_custom_analyzer": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  },
  "mappings": {
    "product": {
      "properties": {
        "brand": {
          "type": "text",
          "analyzer": "my_custom_analyzer",
          "fielddata": true,
        }
      }
    }
  }
}

```

**Data**

```
POST demo_products/product
{
  "brand": "New York Jets"
}

POST demo_products/product
{
  "brand": "new york jets"
}

POST demo_products/product
{
  "brand": "Washington Redskins"
}

```

**Query**

```
GET demo_products/product/_search
{
  "size": 0,
  "aggs": {
    "brand_facet": {
      "terms": {
        "field": "brand"
      }
    }
  }
}

```

**Query result**

```
"aggregations": {
    "brand_facet": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "new york jets",
          "doc_count": 2
        },
        {
          "key": "washington redskins",
          "doc_count": 1
        }
      ]
    }
  }

```

**This is great -** but lowercased ☹  
We could use a top\_hits sub aggregation to the propper cased version of the field

**What to do?**  
If we use keyword instead of text we end up the 2 buckets for New York Jets because of the differences in casing.

We're concerned about the performance implications by using fielddata. However if fielddata is disabled we get the dreaded "Fielddata is disabled on text fields by default."

Any other tips to resolve this - or should we not be so concerned about fielddata?

---

<div class="post-metadata">

**Author:** ![ros](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ros/32/11720_2.png) [@ros](https://discuss.elastic.co/u/ros)\
**Post date:** [February 3, 2017, 1:30pm UTC](https://discuss.elastic.co/t/facets-how-to-do-propper-terms-aggregations/73371/2 "2017-02-03T13:30:57Z")

</div>

To answer my own question. As of ES 5.2 keyword normalizers is the way to go.

```
PUT demo_products
{
  "settings": {
    "analysis": {
      "normalizer": {
        "my_normalizer": {
          "type": "custom",
          "filter": ["lowercase"]
        }
      }
    }
  },
  "mappings": {
    "product": {
      "properties": {
        "brand": {
          "type": "keyword",
          "normalizer": "my_normalizer"
        }
      }
    }
  }
}

```

Response

```
  "aggregations" : {
    "brand_facet" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "new york jets",
          "doc_count" : 2
        },
        {
          "key" : "washington redskins",
          "doc_count" : 1
        }
      ]
    }
  }
```

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [February 3, 2017, 10:24pm UTC](https://discuss.elastic.co/t/facets-how-to-do-propper-terms-aggregations/73371/3 "2017-02-03T22:24:23Z")

</div>

Yep! Confirming that 5.2 normalizers is exactly the right situation in this case 🙂

---

<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:** [March 3, 2017, 10:24pm UTC](https://discuss.elastic.co/t/facets-how-to-do-propper-terms-aggregations/73371/4 "2017-03-03T22:24:33Z")

</div>

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