# Elasticsearch 6.2.4: how to get distinct values from multiple fields from ordered documents?

**URL:** <https://discuss.elastic.co/t/elasticsearch-6-2-4-how-to-get-distinct-values-from-multiple-fields-from-ordered-documents/155358>\
**Category:** Elasticsearch\
**Created:** [November 5, 2018, 8:32am UTC](https://discuss.elastic.co/t/elasticsearch-6-2-4-how-to-get-distinct-values-from-multiple-fields-from-ordered-documents/155358 "2018-11-05T08:32:21Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![betty](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@betty](https://discuss.elastic.co/u/betty)\
**Post date:** [November 5, 2018, 8:32am UTC](https://discuss.elastic.co/t/elasticsearch-6-2-4-how-to-get-distinct-values-from-multiple-fields-from-ordered-documents/155358/1 "2018-11-05T08:32:21Z")

</div>

Below are the documents like:

```auto
{
    "name": "Micheal",
    "title": "appliaction engeneer",
    "age": 30,
    "interest": "java",
    "manager": "Amily"
},
{
    "name": "Georgiana",
    "title": "appliaction engeneer",
    "age": 34,
    "interest": "java",
    "manager": "Amily"
},
{
    "name": "Benjamin",
    "title": "Product Manager",
    "age": 36,
    "interest": "management",
    "manager": "robinson "
},
{
    "name": "Selina",
    "title": "appliaction engeneer",
    "age": 30,
    "interest": "java",
    "manager": "Grey "
},
{
    "name": "Edison",
    "title": "appliaction engeneer",
    "age": 26,
    "interest": ".net",
    "manager": "Amily "
}

```

Now I need to exact three fields `title, age, interest` from the documents.

The results should be like:

```auto
{
    "title": "appliaction engeneer",
    "age": 30,
    "interest": "java",
},
{
    "title": "appliaction engeneer",
    "age": 34,
    "interest": "java",
},
{
    "title": "Product Manager",
    "age": 36,
    "interest": "management",
},
{
    "title": "appliaction engeneer",
    "age": 26,
    "interest": ".net",
}

```

One of the repeated record like below one should be removed.

```auto
{
    "title": "appliaction engeneer",
    "age": 30,
    "interest": "java"
}

```

**And most importantly, the order of the obtained records should be consistent with the documents.**

How can we achieve this?

---

<div class="post-metadata">

**Author:** ![Jaspreet\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jaspreet_singh/32/58296_2.png) [@Jaspreet\_Singh](https://discuss.elastic.co/u/Jaspreet_Singh)\
**Post date:** [November 6, 2018, 6:24pm UTC](https://discuss.elastic.co/t/elasticsearch-6-2-4-how-to-get-distinct-values-from-multiple-fields-from-ordered-documents/155358/2 "2018-11-06T18:24:33Z")

</div>

You should be able to get this via `_source`. Try this ...

```auto
GET someindex/sometype/_search
{
    "_source":["title","age","interest"]
}
```

---

<div class="post-metadata">

**Author:** ![betty](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@betty](https://discuss.elastic.co/u/betty)\
**Post date:** [November 7, 2018, 1:31am UTC](https://discuss.elastic.co/t/elasticsearch-6-2-4-how-to-get-distinct-values-from-multiple-fields-from-ordered-documents/155358/3 "2018-11-07T01:31:58Z")

</div>

`_source` cannot give the distinct values, it just gives all values. That's not I want.

---

<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:** [November 7, 2018, 6:33am UTC](https://discuss.elastic.co/t/elasticsearch-6-2-4-how-to-get-distinct-values-from-multiple-fields-from-ordered-documents/155358/4 "2018-11-07T06:33:56Z")

</div>

May be use a terms agg with a script that combines all the values (which is better to compute at index time)?

---

<div class="post-metadata">

**Author:** ![betty](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@betty](https://discuss.elastic.co/u/betty)\
**Post date:** [November 7, 2018, 7:02am UTC](https://discuss.elastic.co/t/elasticsearch-6-2-4-how-to-get-distinct-values-from-multiple-fields-from-ordered-documents/155358/5 "2018-11-07T07:02:49Z")

</div>

Yes I've tried this method but found the order of the results is not consistent with the original documents. So it also doesn't meet my requirements (as I emphasized in the bolding part in my question).

---

<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:** [November 7, 2018, 8:17am UTC](https://discuss.elastic.co/t/elasticsearch-6-2-4-how-to-get-distinct-values-from-multiple-fields-from-ordered-documents/155358/6 "2018-11-07T08:17:59Z")

</div>

Well...

> **the order of the obtained records should be consistent with the documents.**

I don't exactly understand what it means.

---

<div class="post-metadata">

**Author:** ![betty](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@betty](https://discuss.elastic.co/u/betty)\
**Post date:** [November 7, 2018, 8:44am UTC](https://discuss.elastic.co/t/elasticsearch-6-2-4-how-to-get-distinct-values-from-multiple-fields-from-ordered-documents/155358/7 "2018-11-07T08:44:25Z")

</div>

As I described firstly in the question, the order of the documents is:  
`{"name": "Micheal",...}`,  
`{"name": "Georgiana",...}`,  
`{"name": "Benjamin",...}`,  
`{"name": "Selina",...}`,  
`{"name": "Edison",...}`,

If I use the term agg with script that combines the 3 values interested (`"title"`, `"age"`, `"interest"` in this case), the obtained results may come from the following documents:  
`{"name": "Benjamin",...}`,  
`{"name": "Edison",...}`,  
`{"name": "Micheal",...}`,  
`{"name": "Selina",...}`,  
`{"name": "Georgiana",...}`,

The order is different than the original one.

---

<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:** [November 7, 2018, 9:10am UTC](https://discuss.elastic.co/t/elasticsearch-6-2-4-how-to-get-distinct-values-from-multiple-fields-from-ordered-documents/155358/8 "2018-11-07T09:10:47Z")

</div>

How is this sorted in the first place?

---

<div class="post-metadata">

**Author:** ![betty](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@betty](https://discuss.elastic.co/u/betty)\
**Post date:** [November 7, 2018, 9:23am UTC](https://discuss.elastic.co/t/elasticsearch-6-2-4-how-to-get-distinct-values-from-multiple-fields-from-ordered-documents/155358/9 "2018-11-07T09:23:29Z")

</div>

It's sorted by `query` part in the DSL (I didn't list the `query` here) and have multiple roles to define the order. That's why we should follow this order and can't change it. Otherwise we can't get the most relevant records in the first part.

It's like: we already have the results by using:  
`GET index_name/_search`  
`{"query": {...}}`

Now we need to extract the 3 fields from these documents for user's interest, but the order of the final results we got at last should be consistent with the query results we got firstly.

In this case, it should be:  
The first record which contains the 3 fields should come from the first document: `{"name": "Micheal",...}`  
The second record which contains the 3 fields should come from the second document: `{"name": "Georgiana",...}`  
...

Meanwhile, we should remove the repeated values/get the distinct values of the 3 fields which the user interests.

---

<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:** [December 5, 2018, 9:23am UTC](https://discuss.elastic.co/t/elasticsearch-6-2-4-how-to-get-distinct-values-from-multiple-fields-from-ordered-documents/155358/10 "2018-12-05T09:23:36Z")

</div>

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