# Terms aggregation on field with types in different mapping

**URL:** <https://discuss.elastic.co/t/terms-aggregation-on-field-with-types-in-different-mapping/56478>\
**Category:** Elasticsearch\
**Created:** [July 27, 2016, 8:03am UTC](https://discuss.elastic.co/t/terms-aggregation-on-field-with-types-in-different-mapping/56478 "2016-07-27T08:03:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![elssar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elssar/32/9216_2.png) [@elssar](https://discuss.elastic.co/u/elssar)\
**Post date:** [July 27, 2016, 8:03am UTC](https://discuss.elastic.co/t/terms-aggregation-on-field-with-types-in-different-mapping/56478/1 "2016-07-27T08:03:11Z")

</div>

So I have a bunch of indices, rotated monthly, with two mappings - server, client. They both have a field user\_id. In server, the user\_id is a string, and in client, it is mostly a string, but in one months index, it got indexed as an integer.

When I try to do a terms aggregation on the user\_id field, I get an error - `ClassCastException[org.elasticsearch.search.aggregations.bucket.terms.StringTerms$Bucket cannot be cast to org.elasticsearch.search.aggregations.bucket.terms.LongTerms$Bucket]`

When I query the monthly indices which have the correct mapping, I don't get the error.

This is fine, and expected.

But what I don't understand is why do I get this error even when I specify a mapping?

That is, `/events-*/server/_search`, and then do a terms aggregation on the `user_id` field.

---

<div class="post-metadata">

**Author:** ![1222kkk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/1222kkk/32/23635_2.png) [@1222kkk](https://discuss.elastic.co/u/1222kkk)\
**Post date:** [July 27, 2016, 8:10am UTC](https://discuss.elastic.co/t/terms-aggregation-on-field-with-types-in-different-mapping/56478/2 "2016-07-27T08:10:29Z")

</div>

> [@elssar](#):
>
> StringTerms

StringTerms ,LongTerms ,field type different and confilts?

---

<div class="post-metadata">

**Author:** ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)\
**Post date:** [July 27, 2016, 8:46am UTC](https://discuss.elastic.co/t/terms-aggregation-on-field-with-types-in-different-mapping/56478/3 "2016-07-27T08:46:00Z")

</div>

Hi @elssar,

> [@elssar](#):
>
> But what I don't understand is why do I get this error even when I specify a mapping?

Any chance you allow [dynamic field mapping](https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-field-mapping.html), the user id was not in the mapping and encountered for the first time?

Anyway, you can correct the problem by creating a new index with the correct mapping and using the [reindex API](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html) to reindex the data. See [index aliases and zero downtime](https://www.elastic.co/guide/en/elasticsearch/guide/master/index-aliases.html) on how to achieve this without affecting your users.

Daniel

---

<div class="post-metadata">

**Author:** ![elssar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elssar/32/9216_2.png) [@elssar](https://discuss.elastic.co/u/elssar)\
**Post date:** [July 27, 2016, 10:23am UTC](https://discuss.elastic.co/t/terms-aggregation-on-field-with-types-in-different-mapping/56478/4 "2016-07-27T10:23:58Z")

</div>

Hi @danielmitterdorfer,

I understand that I have to reindex (I've reindexed more times than I'd like to admit 😅).

I think my last post didn't explain my query well enough.

What I don't understand is why would a field having conflicting types in two different mappings cause problems when only aggregating over one mapping.

That is,

```auto
{
  "events": {
    "mappings": {
      "server": {
        "user_id": {
          "full_name": "user_id",
          "mapping": {
            "user_id": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        }
      },
      "client": {
        "user_id": {
          "full_name": "user_id",
          "mapping": {
            "user_id": {
              "type": "long"
            }
          }
        }
      }
    }
  }
}

```

Now when I run a termsn aggregation on the `user_id` field, in `/events/server`, why does the mapping for `user_id` in `client` matter? Shouldn't they be independent of each other. I under that I'd get an error if I sent an query to `/events`.

---

<div class="post-metadata">

**Author:** ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)\
**Post date:** [July 27, 2016, 10:35am UTC](https://discuss.elastic.co/t/terms-aggregation-on-field-with-types-in-different-mapping/56478/5 "2016-07-27T10:35:19Z")

</div>

Hi @elssar,

oh, yes. I totally misunderstood.

> when I run a termsn aggregation on the user\_id field, in /events/server, why does the mapping for user\_id in client matter? Shouldn't they be independent of each other.

Your assumption is not correct. Fields with the same name must have the same mapping (if they are in the same index). For more details see:

- [Conflicts between fields in different types](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html#merging-conflicts) in the reference documentation
- The blog post [index vs type](https://www.elastic.co/blog/index-vs-type)

Daniel

---

<div class="post-metadata">

**Author:** ![elssar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elssar/32/9216_2.png) [@elssar](https://discuss.elastic.co/u/elssar)\
**Post date:** [July 27, 2016, 10:41am UTC](https://discuss.elastic.co/t/terms-aggregation-on-field-with-types-in-different-mapping/56478/6 "2016-07-27T10:41:20Z")

</div>

@danielmitterdorfer ah, that explains it then. Thank you 🙂

---

<div class="post-metadata">

**Author:** ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)\
**Post date:** [July 27, 2016, 10:44am UTC](https://discuss.elastic.co/t/terms-aggregation-on-field-with-types-in-different-mapping/56478/7 "2016-07-27T10:44:57Z")

</div>

Sure, you're welcome. 🙂

---

<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:** [July 5, 2017, 10:32pm UTC](https://discuss.elastic.co/t/terms-aggregation-on-field-with-types-in-different-mapping/56478/8 "2017-07-05T22:32:36Z")

</div>


