# Unique count (cardinality agg query) from two fields

**URL:** <https://discuss.elastic.co/t/unique-count-cardinality-agg-query-from-two-fields/153626>\
**Category:** Elasticsearch\
**Created:** [October 23, 2018, 2:32pm UTC](https://discuss.elastic.co/t/unique-count-cardinality-agg-query-from-two-fields/153626 "2018-10-23T14:32:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![birko](https://avatars.discourse-cdn.com/v4/letter/b/dbc845/32.png) [@birko](https://discuss.elastic.co/u/birko)\
**Post date:** [October 23, 2018, 2:32pm UTC](https://discuss.elastic.co/t/unique-count-cardinality-agg-query-from-two-fields/153626/1 "2018-10-23T14:32:09Z")

</div>

Hello,

how can I do unique count based on two fields with cardinality aggregation query? Let’s imagine from\_user and to\_user are telephone numbers and I need unique count of tel.numbers.

Example:  
**1st document**  
`"_index": "index1",`  
`...`  
`"_source": {`  
`"creation_date": "2018-08-10T00:20:05",`  
`"from_user": "+441234",`  
`"to_user": "+445678"`  
`}`

**2nd document**  
`"_index": "index1",`  
`...`  
`"_source": {`  
`"creation_date": "2018-08-10T00:21:47",`  
`"from_user": "+447890",`  
`"to_user": "+441234"`  
`}`

I need to execute cardinality aggregation query on two fields (from\_user and to\_user). Because I need to get number 3 - in my two doucuments are 3 unique tel. numbers.

**_Request:_**  
GET /index1/\_search  
{  
"aggs" : {  
"unique\_tel\_numbers" : {  
"cardinality" : {  
"field" : "from\_user"  
}  
}  
}  
}

**_Response:_**  
...  
"aggregations": {  
"unique\_tel\_numbers": {  
"value": 2  
}  
}  
...

---

<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:** [October 24, 2018, 8:54am UTC](https://discuss.elastic.co/t/unique-count-cardinality-agg-query-from-two-fields/153626/2 "2018-10-24T08:54:19Z")

</div>

Two options:

1. **Index time** : Use [copy\_to](https://www.elastic.co/guide/en/elasticsearch/reference/current/copy-to.html) to create an "all phone numbers" physical field
2. **query time** : use a script to combine the fields (see below)

Query-time example:

```
DELETE test
POST test/_doc/1
{
  "from":1,
  "to":2
}
POST test/_doc/2
{
  "from":2,
  "to":3
}
GET test/_doc/_search
{
  "size":0,
  "aggs":{
	"phones":{
	  "cardinality":{
		"script":{
		  "source":"return [doc['from'], doc['to']]"
		}
	  }
	}

  }
}
```

---

<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:** [November 21, 2018, 8:54am UTC](https://discuss.elastic.co/t/unique-count-cardinality-agg-query-from-two-fields/153626/3 "2018-11-21T08:54:19Z")

</div>

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