# Aggregation question

**URL:** <https://discuss.elastic.co/t/aggregation-question/15841>\
**Category:** Elasticsearch\
**Created:** [February 17, 2014, 10:06am UTC](https://discuss.elastic.co/t/aggregation-question/15841 "2014-02-17T10:06:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mooky](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mooky](https://discuss.elastic.co/u/mooky)\
**Post date:** [February 17, 2014, 10:06am UTC](https://discuss.elastic.co/t/aggregation-question/15841/1 "2014-02-17T10:06:09Z")

</div>

I am using aggregation to give me some reports of items (count) aggregated  
by various properties (location or market (or both)).

I am using a term aggregator.

A simplified example of my data looks like this:

{  
"intentLocationCode": "SHANG",  
"intentLocationDescription": "Shanghai area",  
"intentMarketDescription": "China Stainless Steel Exchange",  
"intentMarketCode": "CSSX",  
}

Lets say I want to aggregate by Intent Location:

My aggregation looks like this  
{  
"aggregations" : {  
"intentLocations" : {  
"terms" : { "field" : "intentLocationCode" }  
}  
}  
}

And the result looks something like this:  
{  
"aggregations": {  
"intentLocations": {  
"buckets": [  
{  
"key": "shang",  
"doc\_count": 12  
},  
{  
"key": "anotherlocation",  
"doc\_count": 8760  
},  
{  
"key": "loc42",  
"doc\_count": 4773  
},  
{  
"key": "area51",  
"doc\_count": 821  
}  
]  
}  
}  
}

However, in the results I would like something like:  
{  
"aggregations": {  
"intentLocations": {  
"buckets": [  
{  
"key": "Shanghai area",  
"doc\_count": 12  
},  
{  
"key": "Another Location Where Copper Is Stored",  
"doc\_count": 8760  
},  
{  
"key": "The 42nd Stainless Steel Storage Company",  
"doc\_count": 4773  
},  
{  
"key": "Area 51",  
"doc\_count": 821  
}  
]  
}  
}  
}

ie I really want to the value of the intentLocationDescription field as the  
key rather than the code. But obviously, doing a term aggregation on  
description is going to give me very different results (unless I index  
description with not\_analyzed)  
However, I do want to analyse intentLocationDescription for decent search  
behaviour.

Is there a trick to achieve this with aggregations?  
Or do I have to index intentLocationDescriptionTwice (analysed and not  
analysed)?  
(I don't really want to be doing any post-processing to match code and  
description - because that would involve a reference data lookup that will  
kill performance)

Cheers.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/965c337e-3b12-4603-880d-acf9a1860ed7%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/965c337e-3b12-4603-880d-acf9a1860ed7%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@Binh\_Ly](https://discuss.elastic.co/u/Binh_Ly)\
**Post date:** [February 18, 2014, 3:28pm UTC](https://discuss.elastic.co/t/aggregation-question/15841/2 "2014-02-18T15:28:32Z")

</div>

Yes, the correct way would be to index intentLocationDescription as a  
multi-field. You don't have to introduce it as multiple fields in your  
source document. All you need to do is on the ES mapping, you set that  
field to a multi-field, once as whatever analyzed you want, and the other  
as not\_analyzed. You can see an example here:

[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-core-types.html#\_multi\_fields\_3](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-core-types.html#_multi_fields_3)

Wherein you have 2 fields in the index derived from 1 single field in your  
JSON source. The "name" field is analyzed. And then the "name.raw" field is  
not\_analyzed which is what you want to aggregate on.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/3b0b8f26-0775-4d6c-9376-faab0e03b106%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/3b0b8f26-0775-4d6c-9376-faab0e03b106%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![mooky](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mooky](https://discuss.elastic.co/u/mooky)\
**Post date:** [February 21, 2014, 1:27pm UTC](https://discuss.elastic.co/t/aggregation-question/15841/3 "2014-02-21T13:27:34Z")

</div>

Excellent. Thanks!

On Tuesday, 18 February 2014 15:28:32 UTC, Binh Ly wrote:

> Yes, the correct way would be to index intentLocationDescription as a  
> multi-field. You don't have to introduce it as multiple fields in your  
> source document. All you need to do is on the ES mapping, you set that  
> field to a multi-field, once as whatever analyzed you want, and the other  
> as not\_analyzed. You can see an example here:
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-core-types.html#_multi_fields_3)
> 
> Wherein you have 2 fields in the index derived from 1 single field in your  
> JSON source. The "name" field is analyzed. And then the "name.raw" field is  
> not\_analyzed which is what you want to aggregate on.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/70acc449-62d8-4230-8da8-6aabf206d5cd%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/70acc449-62d8-4230-8da8-6aabf206d5cd%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 1:48am UTC](https://discuss.elastic.co/t/aggregation-question/15841/4 "2017-07-06T01:48:22Z")

</div>

Closed on July 6, 2017, 1:48am UTC


