# How to get distinct terms from multiple fields

**URL:** <https://discuss.elastic.co/t/how-to-get-distinct-terms-from-multiple-fields/166955>\
**Category:** Elasticsearch\
**Created:** [February 4, 2019, 12:30pm UTC](https://discuss.elastic.co/t/how-to-get-distinct-terms-from-multiple-fields/166955 "2019-02-04T12:30:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sesha](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@sesha](https://discuss.elastic.co/u/sesha)\
**Post date:** [February 4, 2019, 12:30pm UTC](https://discuss.elastic.co/t/how-to-get-distinct-terms-from-multiple-fields/166955/1 "2019-02-04T12:30:25Z")

</div>

I need to get distinct cities from multiple fields using aggregations.

```
{
    "originCity": "Moscow"
    "destCity": "Stockholm"
}

```

Thanks

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 4, 2019, 12:55pm UTC](https://discuss.elastic.co/t/how-to-get-distinct-terms-from-multiple-fields/166955/2 "2019-02-04T12:55:23Z")

</div>

I'd define a `copy_to` parameter for each of those fields in the mapping, so at index time it will copy the content of `originCity` and `destCity` to a `keyword` field you can name `city`. Then run a Terms agg on `city` is straightforward.

---

<div class="post-metadata">

**Author:** ![sesha](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@sesha](https://discuss.elastic.co/u/sesha)\
**Post date:** [February 4, 2019, 7:22pm UTC](https://discuss.elastic.co/t/how-to-get-distinct-terms-from-multiple-fields/166955/3 "2019-02-04T19:22:12Z")

</div>

Thanks @dadoonet

I have tried copy\_to example from the documentation as below.

```
PUT my_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "first_name": {
          "type": "text",
          "copy_to": "full_name" 
        },
        "last_name": {
          "type": "text",
          "copy_to": "full_name" 
        },
        "full_name": {
          "type": "text"
        }
      }
    }
  }
}

PUT my_index/_doc/1
{
  "first_name": "John",
  "last_name": "Smith"
} 

```

Now tried terms aggregation, but didn't get the result as expected.

```
GET /my_index/_doc/_search
{ "size": 0,
  "aggs": {
    "NAME": {
      "terms": {
        "field": "full_name.keyword",
        "size": 10
      }
    }
  }
}

Ouput:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "NAME": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": []
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 4, 2019, 7:35pm UTC](https://discuss.elastic.co/t/how-to-get-distinct-terms-from-multiple-fields/166955/4 "2019-02-04T19:35:28Z")

</div>

There is no `full_name.keyword` in your mapping. I don't see how this can even run.

It should be something like:

```auto
PUT my_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "first_name": {
          "type": "text",
          "copy_to": "full_name" 
        },
        "last_name": {
          "type": "text",
          "copy_to": "full_name" 
        },
        "full_name": {
          "type": "keyword"
        }
      }
    }
  }
}

PUT my_index/_doc/1
{
  "first_name": "John",
  "last_name": "Smith"
} 

GET /my_index/_doc/_search
{
 "size": 0,
  "aggs": {
    "NAME": {
      "terms": {
        "field": "full_name"
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![sesha](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@sesha](https://discuss.elastic.co/u/sesha)\
**Post date:** [February 4, 2019, 7:39pm UTC](https://discuss.elastic.co/t/how-to-get-distinct-terms-from-multiple-fields/166955/5 "2019-02-04T19:39:05Z")

</div>

Thanks very much @dadoonet 😀

---

<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 4, 2019, 7:39pm UTC](https://discuss.elastic.co/t/how-to-get-distinct-terms-from-multiple-fields/166955/6 "2019-03-04T19:39:07Z")

</div>

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