# How to aggregate by metadata (types/field names)?

**URL:** <https://discuss.elastic.co/t/how-to-aggregate-by-metadata-types-field-names/17248>\
**Category:** Elasticsearch\
**Created:** [April 29, 2014, 1:43am UTC](https://discuss.elastic.co/t/how-to-aggregate-by-metadata-types-field-names/17248 "2014-04-29T01:43:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ElasticSearch\_Users\_](https://avatars.discourse-cdn.com/v4/letter/e/b4bc9f/32.png) [@ElasticSearch\_Users\_](https://discuss.elastic.co/u/ElasticSearch_Users_)\
**Post date:** [April 29, 2014, 1:43am UTC](https://discuss.elastic.co/t/how-to-aggregate-by-metadata-types-field-names/17248/1 "2014-04-29T01:43:58Z")

</div>

Hi, I'm trying to create a query that will return count of hits per doc  
type and field name.  
For example if I have the following docs:  
type X  
[  
{f1: ..., f2: "a b c"},  
{f1: ..., f2: "b c d"},  
{f1: ..., f2: "c d e"},  
]

type Y  
[  
{f3: ..., f4: "d e f"},  
{f3: ..., f4: "e f g"},  
{f3: ..., f4: "f g h"},  
]

I want to query for types and field names containing hits for "d":  
[  
{Type: X, Field: f2, Count: 2}  
{Type: Y, Field: f4, Count: 1}  
]

I don't want to run a query that will pull all docs matching "d" and then  
aggregate metadata on the client as the number of docs can be really big...  
I've searched this group and found couple posts suggesting to read mappings  
to figure out types and field names, but I don't want to run separate query  
per typeXfieldname as metadata in my case can be really big (number of  
typeXfieldName can be in hundreds).

Well, I can actually push doc type into each document as a field, but I'm  
still left with aggregation by field name hits...

--  
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/583d45e9-ea2b-4567-9f97-93f4c545b99d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/583d45e9-ea2b-4567-9f97-93f4c545b99d%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![ElasticSearch\_Users\_](https://avatars.discourse-cdn.com/v4/letter/e/b4bc9f/32.png) [@ElasticSearch\_Users\_](https://discuss.elastic.co/u/ElasticSearch_Users_)\
**Post date:** [May 1, 2014, 2:04am UTC](https://discuss.elastic.co/t/how-to-aggregate-by-metadata-types-field-names/17248/2 "2014-05-01T02:04:57Z")

</div>

> bump

I'm new to elastic, considering to move from a proprietary system...  
I'm blocked on the fact that I can't get list of field hits per document as  
part of search results... Any help any clue?

> 

--  
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/19e12a97-9db5-4d54-b8dd-91662c82a22a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/19e12a97-9db5-4d54-b8dd-91662c82a22a%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [May 5, 2014, 10:41am UTC](https://discuss.elastic.co/t/how-to-aggregate-by-metadata-types-field-names/17248/3 "2014-05-05T10:41:26Z")

</div>

Hey,

you could take a look at the terms aggregation using the \_type field, which  
contains the type information. See

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

--Alex

On Thu, May 1, 2014 at 4:04 AM, 'almineev .' via elasticsearch \<  
[elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com)\> wrote:

> bump
> 
> > 
> 
> I'm new to elastic, considering to move from a proprietary system...  
> I'm blocked on the fact that I can't get list of field hits per document  
> as part of search results... Any help any clue?
> 
> > 
> 
> --  
> 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/19e12a97-9db5-4d54-b8dd-91662c82a22a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/19e12a97-9db5-4d54-b8dd-91662c82a22a%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/19e12a97-9db5-4d54-b8dd-91662c82a22a%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/19e12a97-9db5-4d54-b8dd-91662c82a22a%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/CAGCwEM8Nyg2tS\_fJ%3D\_PX8HXtMeutXx7cpebpzvaFLZP5JOuOzg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGCwEM8Nyg2tS_fJ%3D_PX8HXtMeutXx7cpebpzvaFLZP5JOuOzg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:31am UTC](https://discuss.elastic.co/t/how-to-aggregate-by-metadata-types-field-names/17248/4 "2017-07-06T01:31:42Z")

</div>


