# Terms query return distinct field results

**URL:** <https://discuss.elastic.co/t/terms-query-return-distinct-field-results/209950>\
**Category:** Elasticsearch\
**Created:** [November 29, 2019, 9:56am UTC](https://discuss.elastic.co/t/terms-query-return-distinct-field-results/209950 "2019-11-29T09:56:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![hackerwin7](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hackerwin7/32/43223_2.png) [@hackerwin7](https://discuss.elastic.co/u/hackerwin7)\
**Post date:** [November 29, 2019, 9:56am UTC](https://discuss.elastic.co/t/terms-query-return-distinct-field-results/209950/1 "2019-11-29T09:56:19Z")

</div>

Hi,  
In terms query:  
like this

```auto
{
    "query": {
        "terms" : { "user" : ["u1", "u2","u3"]}
    }
}

```

this would return any document's user match u1 or u2 or u3 and maybe duplicate  
such as

```auto
result-1: u1
result-2: u2
result-3: u1
result-4: u3
result-5: u3
...

```

I want to the results hits count is same to query terms count.  
such as:

```auto
result-1: u1
result-2: u2
result-3: u3

```

So, we want to query this

```auto
{
    "query": {
        "terms" : { "user" : ["u1", "u2","u3", ...... "u10000"]}
    }
}

```

and we want the search results like this:

```auto
result-1: u1
result-2: u2
......
result-10000: u-10000

```

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [November 29, 2019, 10:00am UTC](https://discuss.elastic.co/t/terms-query-return-distinct-field-results/209950/2 "2019-11-29T10:00:52Z")

</div>

What you're after are aggregations rather than individual documents. As your example shows - a user could produce many events that appear as documents

```
GET test/_search
{
  "query": {
	"terms": {
	  "user": [1, 5, 3]
	}
  },
  "size": 0,
  "aggs": {
	"topMatchingUsers": {
	  "terms": {
		"field": "user"
	  }
	}
  }
}
```

---

<div class="post-metadata">

**Author:** ![hackerwin7](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hackerwin7/32/43223_2.png) [@hackerwin7](https://discuss.elastic.co/u/hackerwin7)\
**Post date:** [November 29, 2019, 10:07am UTC](https://discuss.elastic.co/t/terms-query-return-distinct-field-results/209950/3 "2019-11-29T10:07:58Z")

</div>

@Mark_Harwood  
Thanks for your reply  
but the aggregation is just buckets result, like this:

```auto
hits: {
duplicate hits result
}
aggs: {
 "buckets" : [
        {
          "key" : u1,
          "doc_count" : 29
        },
        {
          "key" : u2,
          "doc_count" : 9
        },
 ....
      ]
}

```

how to let the buckets return the total document hits, not just `key`, `count` pair value

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [November 29, 2019, 10:12am UTC](https://discuss.elastic.co/t/terms-query-return-distinct-field-results/209950/4 "2019-11-29T10:12:08Z")

</div>

Not sure I understand. Each bucket's `doc_count` _is_ the total number of documents referencing that user?  
If your question is about how to avoid the JSON formatting of results I'm afraid that's unavoidable.

---

<div class="post-metadata">

**Author:** ![hackerwin7](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hackerwin7/32/43223_2.png) [@hackerwin7](https://discuss.elastic.co/u/hackerwin7)\
**Post date:** [November 29, 2019, 10:18am UTC](https://discuss.elastic.co/t/terms-query-return-distinct-field-results/209950/5 "2019-11-29T10:18:36Z")

</div>

@Mark_Harwood  
I'll elaborate this case, I want the result is document result not document count:  
such as  
terms query user: [u1, u2]

```auto
{
"user": "u1",
"uid": "1000"
},
{
"user": "u1",
"uid": "1001"
},
{
"user": "u1",
"uid": "1002"
},
{
"user": "u2",
"uid": "2002"
},
{
"user": "u2",
"uid": "2003"
},

```

However I just want the distinct by user of the document results:

```auto
{
"user": "u1",
"uid": "1000"
},
{
"user": "u2",
"uid": "2003"
}

```

is okay,

```auto
{
"user": "u1",
"uid": "1001"
},
{
"user": "u2",
"uid": "2002"
}

```

is also okay for us.  
we just want the distinct of terms query document results, not aggregation document counts

---

<div class="post-metadata">

**Author:** ![hackerwin7](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hackerwin7/32/43223_2.png) [@hackerwin7](https://discuss.elastic.co/u/hackerwin7)\
**Post date:** [November 29, 2019, 10:32am UTC](https://discuss.elastic.co/t/terms-query-return-distinct-field-results/209950/6 "2019-11-29T10:32:46Z")

</div>

> [@How Can I Perform a Distinct Query?](https://discuss.elastic.co/t/how-can-i-perform-a-distinct-query/12539):
>
> Hi Guys, I'm a new ElasticSearch user, and need some help selecting only documents with a distinct field. I've got an index of documents which look something like the following: { "last\_name": "Degges", "first\_name": "Randall", "state": "CA", } I'm trying to return a list of de-duplicated documents which contain the same last name. For instance -- if I had three documents, two with a "last\_name" field of "Degges" and one with a "last\_name" field of "Perez", I'd want to return only tw…

similar to this topic, we just want to return a unique list of documents in terms query

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [November 29, 2019, 10:36am UTC](https://discuss.elastic.co/t/terms-query-return-distinct-field-results/209950/7 "2019-11-29T10:36:12Z")

</div>

Maybe [field collapsing](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-collapse) is what you're looking for?

---

<div class="post-metadata">

**Author:** ![hackerwin7](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hackerwin7/32/43223_2.png) [@hackerwin7](https://discuss.elastic.co/u/hackerwin7)\
**Post date:** [November 29, 2019, 10:42am UTC](https://discuss.elastic.co/t/terms-query-return-distinct-field-results/209950/8 "2019-11-29T10:42:49Z")

</div>

Yes, I find collapse the field will return the distinct results, thanks!

---

<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 27, 2019, 10:42am UTC](https://discuss.elastic.co/t/terms-query-return-distinct-field-results/209950/9 "2019-12-27T10:42:50Z")

</div>

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