# Getting unique results

**URL:** <https://discuss.elastic.co/t/getting-unique-results/7573>\
**Category:** Elasticsearch\
**Created:** [May 6, 2012, 9:56am UTC](https://discuss.elastic.co/t/getting-unique-results/7573 "2012-05-06T09:56:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anjesh](https://avatars.discourse-cdn.com/v4/letter/a/a4c791/32.png) [@anjesh](https://discuss.elastic.co/u/anjesh)\
**Post date:** [May 6, 2012, 9:56am UTC](https://discuss.elastic.co/t/getting-unique-results/7573/1 "2012-05-06T09:56:43Z")

</div>

Hi,  
I have following mapping and data

curl -XPUT 'localhost:9200/testcompany/activity1/\_mapping' -d '{  
"activity" : {  
"properties" : {  
"organization" : {  
"properties": {  
"id": {"type": "string"},  
"name": {"analyzer": "keyword", "type": "string"}  
}  
}  
}  
}  
}'  
curl -XPUT localhost:9200/testcompany/activity1/1 -d '{  
"title": "title1 of project",  
"organization": {"name": "ABC company", "id": "11a"}  
}'  
curl -XPUT localhost:9200/testcompany/activity1/2 -d '{  
"title": "title2 of project",  
"organization": {"name": "ABC company", "id": "11a"}  
}'  
curl -XPUT localhost:9200/testcompany/activity1/3 -d '{  
"title": "title3 of project",  
"organization": {"name": "XYZ company", "id": "12c"}  
}'

I need to present the list of organizations (name and ids) with their  
numbers like  
ABC company (11a): 2  
XYX company (12c): 1

If i do facet for id,  
curl -X POST localhost:9200/testcompany/activity1/\_search?pretty=true -d '{  
"query" : {"match\_all":{}},  
"facets" : {"organization" : {"terms" : {"field": "organization.id"}}}  
}'

I only get count by "ids" and using name, i get count by "names". What i  
could do is get the count by ids and names and combine results, which is  
definitely not a good way. Is there any possiblity of getting the unique  
json results for organizations so that i could easily combine the facets  
results with the sort of key-value unique results.

Thanks  
Anjesh.

---

<div class="post-metadata">

**Author:** ![sujoysett](https://avatars.discourse-cdn.com/v4/letter/s/2acd7d/32.png) [@sujoysett](https://discuss.elastic.co/u/sujoysett)\
**Post date:** [May 7, 2012, 5:17am UTC](https://discuss.elastic.co/t/getting-unique-results/7573/2 "2012-05-07T05:17:58Z")

</div>

Hi,

Try using scripts in facets to run facets on multiple fields. Something  
like :

{  
"query": {  
"match\_all": {}  
},  
"size" : 0,  
"facets": {  
"organization": {  
"terms": {  
"script": "doc['organization.name'].value + "(" +  
doc['organization.id'].value + ")""  
}  
}  
}  
}

This might not work properly, as I might be missing something on the  
necessary syntax for nested type, as is your data, but it works plain and  
simple in my case.

On Sunday, May 6, 2012 3:26:43 PM UTC+5:30, anjesh wrote:

> Hi,  
> I have following mapping and data
> 
> curl -XPUT 'localhost:9200/testcompany/activity1/\_mapping' -d '{  
> "activity" : {  
> "properties" : {  
> "organization" : {  
> "properties": {  
> "id": {"type": "string"},  
> "name": {"analyzer": "keyword", "type": "string"}  
> }  
> }  
> }  
> }  
> }'  
> curl -XPUT localhost:9200/testcompany/activity1/1 -d '{  
> "title": "title1 of project",  
> "organization": {"name": "ABC company", "id": "11a"}  
> }'  
> curl -XPUT localhost:9200/testcompany/activity1/2 -d '{  
> "title": "title2 of project",  
> "organization": {"name": "ABC company", "id": "11a"}  
> }'  
> curl -XPUT localhost:9200/testcompany/activity1/3 -d '{  
> "title": "title3 of project",  
> "organization": {"name": "XYZ company", "id": "12c"}  
> }'
> 
> I need to present the list of organizations (name and ids) with their  
> numbers like  
> ABC company (11a): 2  
> XYX company (12c): 1
> 
> If i do facet for id,  
> curl -X POST localhost:9200/testcompany/activity1/\_search?pretty=true -d '{  
> "query" : {"match\_all":{}},  
> "facets" : {"organization" : {"terms" : {"field": "organization.id"}}}  
> }'
> 
> I only get count by "ids" and using name, i get count by "names". What i  
> could do is get the count by ids and names and combine results, which is  
> definitely not a good way. Is there any possiblity of getting the unique  
> json results for organizations so that i could easily combine the facets  
> results with the sort of key-value unique results.
> 
> Thanks  
> Anjesh.

---

<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 6, 2017, 3:29am UTC](https://discuss.elastic.co/t/getting-unique-results/7573/3 "2017-07-06T03:29:56Z")

</div>

Closed on July 6, 2017, 3:29am UTC


