# Use facets to group documents by index and type

**URL:** https://discuss.elastic.co/t/use-facets-to-group-documents-by-index-and-type/17169
**Category:** Elasticsearch
**Created:** [April 24, 2014, 8:28am UTC](https://discuss.elastic.co/t/use-facets-to-group-documents-by-index-and-type/17169 "2014-04-24T08:28:45Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Sviatoslav\_Abakumov](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@Sviatoslav\_Abakumov](https://discuss.elastic.co/u/Sviatoslav_Abakumov)
#### Post date: [April 24, 2014, 8:28am UTC](https://discuss.elastic.co/t/use-facets-to-group-documents-by-index-and-type/17169/1 "2014-04-24T08:28:45Z")

</div>

Hello,

I'd like to get how much documents of particular index AND type, like this:

{'f4': {'\_type': 'terms',  
'missing': 0,  
'other': 0,  
'terms': [{'count': 99, 'term': 'light\_beer/comment'},  
{'count': 99, 'term': 'dark\_beer/comment'}],  
'total': 198}}

To do this I added fields ` __index` and `__ type` and filled them when I was  
indexing documents.  
I use term facet with `"script": "_source. __index + '/' + _source.__ type"`

Is there a way to get index and type names from `_index` (IndexLookup) or  
`_source` (SourceLookup)?

Thank you.

--  
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/80604448-0bd5-499f-9c13-de096a732981%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/80604448-0bd5-499f-9c13-de096a732981%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)
#### Post date: [April 24, 2014, 11:30am UTC](https://discuss.elastic.co/t/use-facets-to-group-documents-by-index-and-type/17169/2 "2014-04-24T11:30:11Z")

</div>

Hello,

I'm not sure if you're already aware of the predefined \_index and \_type  
fields:

> **[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.

> **[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.

You can enable them so you can search and retrieve values like you would  
with any other field.

Best regards,  
Radu

On Thu, Apr 24, 2014 at 11:28 AM, Sviatoslav Abakumov \<  
[abakumov.sviatoslav@progforce.com](mailto:abakumov.sviatoslav@progforce.com)\> wrote:

> Hello,
> 
> I'd like to get how much documents of particular index AND type, like this:
> 
> {'f4': {'\_type': 'terms',  
> 'missing': 0,  
> 'other': 0,  
> 'terms': [{'count': 99, 'term': 'light\_beer/comment'},  
> {'count': 99, 'term': 'dark\_beer/comment'}],  
> 'total': 198}}
> 
> To do this I added fields ` __index` and `__ type` and filled them when I  
> was indexing documents.  
> I use term facet with `"script": "_source. __index + '/' + _source.__ type"`
> 
> Is there a way to get index and type names from `_index` (IndexLookup) or  
> `_source` (SourceLookup)?
> 
> Thank you.
> 
> --  
> 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/80604448-0bd5-499f-9c13-de096a732981%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/80604448-0bd5-499f-9c13-de096a732981%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/80604448-0bd5-499f-9c13-de096a732981%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/80604448-0bd5-499f-9c13-de096a732981%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Performance Monitoring \* Log Analytics \* Search Analytics  
Solr & Elasticsearch Support \* [http://sematext.com/](http://sematext.com/)

--  
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/CAHXA0\_0z5jD8cZfkca8FNOmcg7FnEeXL\_S%3DZmJVcDm7PArixLQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHXA0_0z5jD8cZfkca8FNOmcg7FnEeXL_S%3DZmJVcDm7PArixLQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Sviatoslav\_Abakumov](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@Sviatoslav\_Abakumov](https://discuss.elastic.co/u/Sviatoslav_Abakumov)
#### Post date: [April 24, 2014, 11:54am UTC](https://discuss.elastic.co/t/use-facets-to-group-documents-by-index-and-type/17169/3 "2014-04-24T11:54:37Z")

</div>

Oh, yes, I've enabled `_index` so I can do term facet requests like:

```
{'field': '_index'}
{'field': '_type'}

```

And even:

```
{'fields': ['_index', '_type']}

```

There was a hope that this request does what I want, but it merely unites  
facets:

```
{'_type': 'terms',
 'missing': 0,
 'other': 0,
 'terms': [{'count': 198, 'term': 'comment'},
           {'count': 99, 'term': 'light_beer'},
           {'count': 99, 'term': 'dark_beer'}]}

```

Well, I want to concatenate index and type names and pass it to ES facets.  
The only way I see is to use `script`, but I don't know how to access  
`_index` and `_type` from there.

Thank you for understanding.

On Thu, Apr 24, 2014 at 3:30 PM, Radu Gheorghe  
[radu.gheorghe@sematext.com](mailto:radu.gheorghe@sematext.com)wrote:

> Hello,
> 
> I'm not sure if you're already aware of the predefined \_index and \_type  
> fields:
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-index-field.html)
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-type-field.html)
> 
> You can enable them so you can search and retrieve values like you would  
> with any other field.
> 
> Best regards,  
> Radu
> 
> On Thu, Apr 24, 2014 at 11:28 AM, Sviatoslav Abakumov \<  
> [abakumov.sviatoslav@progforce.com](mailto:abakumov.sviatoslav@progforce.com)\> wrote:
> 
> > Hello,
> > 
> > I'd like to get how much documents of particular index AND type, like  
> > this:
> > 
> > {'f4': {'\_type': 'terms',  
> > 'missing': 0,  
> > 'other': 0,  
> > 'terms': [{'count': 99, 'term': 'light\_beer/comment'},  
> > {'count': 99, 'term': 'dark\_beer/comment'}],  
> > 'total': 198}}
> > 
> > To do this I added fields ` __index` and `__ type` and filled them when I  
> > was indexing documents.  
> > I use term facet with `"script": "_source. __index + '/' + _source.__ type"`
> > 
> > Is there a way to get index and type names from `_index` (IndexLookup)or `_source` (SourceLookup)?
> > 
> > Thank you.
> > 
> > --  
> > 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/80604448-0bd5-499f-9c13-de096a732981%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/80604448-0bd5-499f-9c13-de096a732981%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/80604448-0bd5-499f-9c13-de096a732981%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/80604448-0bd5-499f-9c13-de096a732981%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> Performance Monitoring \* Log Analytics \* Search Analytics  
> Solr & Elasticsearch Support \* [http://sematext.com/](http://sematext.com/)
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/uSUHj5LcGdw/unsubscribe](https://groups.google.com/d/topic/elasticsearch/uSUHj5LcGdw/unsubscribe).  
> To unsubscribe from this group and all its topics, 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/CAHXA0\_0z5jD8cZfkca8FNOmcg7FnEeXL\_S%3DZmJVcDm7PArixLQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHXA0_0z5jD8cZfkca8FNOmcg7FnEeXL_S%3DZmJVcDm7PArixLQ%40mail.gmail.com)[https://groups.google.com/d/msgid/elasticsearch/CAHXA0\_0z5jD8cZfkca8FNOmcg7FnEeXL\_S%3DZmJVcDm7PArixLQ%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAHXA0_0z5jD8cZfkca8FNOmcg7FnEeXL_S%3DZmJVcDm7PArixLQ%40mail.gmail.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/CAKmtHP-VZJ\_gUG1Z7yESL6yihQCn8zZJ4kyZm%2B6FeAFJsvnMqA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKmtHP-VZJ_gUG1Z7yESL6yihQCn8zZJ4kyZm%2B6FeAFJsvnMqA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)
#### Post date: [April 24, 2014, 12:23pm UTC](https://discuss.elastic.co/t/use-facets-to-group-documents-by-index-and-type/17169/4 "2014-04-24T12:23:12Z")

</div>

Ah, I see now. Sorry for misreading your initial post. You can do that  
using scripts. This works for me:

curl localhost:9200/\_search?pretty -d '{  
"facets": {  
"idxtype": {  
"terms": {  
"script\_field": "doc['"'\_index'].value + '/' + doc['\_type'"'].value"  
}  
}  
}}'

Using doc['field\_name'].value is usually preferred to \_source or \_fields  
because with doc you're accessing values loaded in memory:

> **[Elastic — The Search AI Company](https://www.elastic.co)**
>
> Power insights and outcomes with The Elastic Search AI Platform. See into your data and find answers that matter with enterprise solutions designed to help you accelerate time to insight. Try Elastic ...

## Best regards, Radu

Performance Monitoring \* Log Analytics \* Search Analytics  
Solr & Elasticsearch Support \* [http://sematext.com/](http://sematext.com/)

On Thu, Apr 24, 2014 at 2:54 PM, Sviatoslav Abakumov \<  
[abakumov.sviatoslav@progforce.com](mailto:abakumov.sviatoslav@progforce.com)\> wrote:

> Oh, yes, I've enabled `_index` so I can do term facet requests like:
> 
> ```
> {'field': '_index'}
> {'field': '_type'}
> 
> ```
> 
> And even:
> 
> ```
> {'fields': ['_index', '_type']}
> 
> ```
> 
> There was a hope that this request does what I want, but it merely unites  
> facets:
> 
> ```
> {'_type': 'terms',
> 'missing': 0,
> 'other': 0,
> 'terms': [{'count': 198, 'term': 'comment'},
> {'count': 99, 'term': 'light_beer'},
> {'count': 99, 'term': 'dark_beer'}]}
> 
> ```
> 
> Well, I want to concatenate index and type names and pass it to ES facets.  
> The only way I see is to use `script`, but I don't know how to access  
> `_index` and `_type` from there.
> 
> Thank you for understanding.
> 
> On Thu, Apr 24, 2014 at 3:30 PM, Radu Gheorghe \<[radu.gheorghe@sematext.com](mailto:radu.gheorghe@sematext.com)
> 
> > wrote:
> 
> > Hello,
> > 
> > I'm not sure if you're already aware of the predefined \_index and \_type  
> > fields:
> > 
> > [Elastic — The Search AI Company | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-index-field.html)
> > 
> > [Elastic — The Search AI Company | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-type-field.html)
> > 
> > You can enable them so you can search and retrieve values like you would  
> > with any other field.
> > 
> > Best regards,  
> > Radu
> > 
> > On Thu, Apr 24, 2014 at 11:28 AM, Sviatoslav Abakumov \<  
> > [abakumov.sviatoslav@progforce.com](mailto:abakumov.sviatoslav@progforce.com)\> wrote:
> > 
> > > Hello,
> > > 
> > > I'd like to get how much documents of particular index AND type, like  
> > > this:
> > > 
> > > {'f4': {'\_type': 'terms',  
> > > 'missing': 0,  
> > > 'other': 0,  
> > > 'terms': [{'count': 99, 'term': 'light\_beer/comment'},  
> > > {'count': 99, 'term': 'dark\_beer/comment'}],  
> > > 'total': 198}}
> > > 
> > > To do this I added fields ` __index` and `__ type` and filled them when I  
> > > was indexing documents.  
> > > I use term facet with `"script": "_source. __index + '/' + _source.__ type"`
> > > 
> > > Is there a way to get index and type names from `_index` (IndexLookup)or `_source` (SourceLookup)?
> > > 
> > > Thank you.
> > > 
> > > --  
> > > 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/80604448-0bd5-499f-9c13-de096a732981%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/80604448-0bd5-499f-9c13-de096a732981%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/80604448-0bd5-499f-9c13-de096a732981%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/80604448-0bd5-499f-9c13-de096a732981%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > .  
> > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > 
> > --  
> > Performance Monitoring \* Log Analytics \* Search Analytics  
> > Solr & Elasticsearch Support \* [http://sematext.com/](http://sematext.com/)
> > 
> > --  
> > You received this message because you are subscribed to a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/uSUHj5LcGdw/unsubscribe](https://groups.google.com/d/topic/elasticsearch/uSUHj5LcGdw/unsubscribe).  
> > To unsubscribe from this group and all its topics, 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/CAHXA0\_0z5jD8cZfkca8FNOmcg7FnEeXL\_S%3DZmJVcDm7PArixLQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHXA0_0z5jD8cZfkca8FNOmcg7FnEeXL_S%3DZmJVcDm7PArixLQ%40mail.gmail.com)[https://groups.google.com/d/msgid/elasticsearch/CAHXA0\_0z5jD8cZfkca8FNOmcg7FnEeXL\_S%3DZmJVcDm7PArixLQ%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAHXA0_0z5jD8cZfkca8FNOmcg7FnEeXL_S%3DZmJVcDm7PArixLQ%40mail.gmail.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/CAKmtHP-VZJ\_gUG1Z7yESL6yihQCn8zZJ4kyZm%2B6FeAFJsvnMqA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKmtHP-VZJ_gUG1Z7yESL6yihQCn8zZJ4kyZm%2B6FeAFJsvnMqA%40mail.gmail.com)[https://groups.google.com/d/msgid/elasticsearch/CAKmtHP-VZJ\_gUG1Z7yESL6yihQCn8zZJ4kyZm%2B6FeAFJsvnMqA%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAKmtHP-VZJ_gUG1Z7yESL6yihQCn8zZJ4kyZm%2B6FeAFJsvnMqA%40mail.gmail.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/CAHXA0\_2UpvcgQn9n4aifmfOEztP6AaK3hdHOD7TMrgU0HkhbvA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHXA0_2UpvcgQn9n4aifmfOEztP6AaK3hdHOD7TMrgU0HkhbvA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Sviatoslav\_Abakumov](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@Sviatoslav\_Abakumov](https://discuss.elastic.co/u/Sviatoslav_Abakumov)
#### Post date: [April 24, 2014, 12:26pm UTC](https://discuss.elastic.co/t/use-facets-to-group-documents-by-index-and-type/17169/5 "2014-04-24T12:26:16Z")

</div>

Magnificent! That's what I like to see.

Thank you so much!

On Thu, Apr 24, 2014 at 4:23 PM, Radu Gheorghe  
[radu.gheorghe@sematext.com](mailto:radu.gheorghe@sematext.com)wrote:

> Ah, I see now. Sorry for misreading your initial post. You can do that  
> using scripts. This works for me:
> 
> curl localhost:9200/\_search?pretty -d '{  
> "facets": {  
> "idxtype": {  
> "terms": {  
> "script\_field": "doc['"'\_index'].value + '/' + doc['\_type'"'].value"  
> }  
> }  
> }}'
> 
> Using doc['field\_name'].value is usually preferred to \_source or \_fields  
> because with doc you're accessing values loaded in memory:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html#_document_fields)
> 
> ## Best regards, Radu
> 
> Performance Monitoring \* Log Analytics \* Search Analytics  
> Solr & Elasticsearch Support \* [http://sematext.com/](http://sematext.com/)
> 
> On Thu, Apr 24, 2014 at 2:54 PM, Sviatoslav Abakumov \<  
> [abakumov.sviatoslav@progforce.com](mailto:abakumov.sviatoslav@progforce.com)\> wrote:
> 
> > Oh, yes, I've enabled `_index` so I can do term facet requests like:
> > 
> > ```
> > {'field': '_index'}
> > {'field': '_type'}
> > 
> > ```
> > 
> > And even:
> > 
> > ```
> > {'fields': ['_index', '_type']}
> > 
> > ```
> > 
> > There was a hope that this request does what I want, but it merely unites  
> > facets:
> > 
> > ```
> > {'_type': 'terms',
> > 'missing': 0,
> > 'other': 0,
> > 'terms': [{'count': 198, 'term': 'comment'},
> > {'count': 99, 'term': 'light_beer'},
> > {'count': 99, 'term': 'dark_beer'}]}
> > 
> > ```
> > 
> > Well, I want to concatenate index and type names and pass it to ES  
> > facets. The only way I see is to use `script`, but I don't know how to  
> > access `_index` and `_type` from there.
> > 
> > Thank you for understanding.
> > 
> > On Thu, Apr 24, 2014 at 3:30 PM, Radu Gheorghe \<  
> > [radu.gheorghe@sematext.com](mailto:radu.gheorghe@sematext.com)\> wrote:
> > 
> > > Hello,
> > > 
> > > I'm not sure if you're already aware of the predefined \_index and \_type  
> > > fields:
> > > 
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-index-field.html)
> > > 
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-type-field.html)
> > > 
> > > You can enable them so you can search and retrieve values like you would  
> > > with any other field.
> > > 
> > > Best regards,  
> > > Radu
> > > 
> > > On Thu, Apr 24, 2014 at 11:28 AM, Sviatoslav Abakumov \<  
> > > [abakumov.sviatoslav@progforce.com](mailto:abakumov.sviatoslav@progforce.com)\> wrote:
> > > 
> > > > Hello,
> > > > 
> > > > I'd like to get how much documents of particular index AND type, like  
> > > > this:
> > > > 
> > > > {'f4': {'\_type': 'terms',  
> > > > 'missing': 0,  
> > > > 'other': 0,  
> > > > 'terms': [{'count': 99, 'term': 'light\_beer/comment'},  
> > > > {'count': 99, 'term': 'dark\_beer/comment'}],  
> > > > 'total': 198}}
> > > > 
> > > > To do this I added fields ` __index` and `__ type` and filled them when I  
> > > > was indexing documents.  
> > > > I use term facet with `"script": "_source. __index + '/' + _source.__ type"`
> > > > 
> > > > Is there a way to get index and type names from `_index` (IndexLookup)or `_source` (SourceLookup)?
> > > > 
> > > > Thank you.
> > > > 
> > > > --  
> > > > 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/80604448-0bd5-499f-9c13-de096a732981%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/80604448-0bd5-499f-9c13-de096a732981%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/80604448-0bd5-499f-9c13-de096a732981%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/80604448-0bd5-499f-9c13-de096a732981%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > > .  
> > > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > > 
> > > --  
> > > Performance Monitoring \* Log Analytics \* Search Analytics  
> > > Solr & Elasticsearch Support \* [http://sematext.com/](http://sematext.com/)
> > > 
> > > --  
> > > You received this message because you are subscribed to a topic in the  
> > > Google Groups "elasticsearch" group.  
> > > To unsubscribe from this topic, visit  
> > > [https://groups.google.com/d/topic/elasticsearch/uSUHj5LcGdw/unsubscribe](https://groups.google.com/d/topic/elasticsearch/uSUHj5LcGdw/unsubscribe).  
> > > To unsubscribe from this group and all its topics, 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/CAHXA0\_0z5jD8cZfkca8FNOmcg7FnEeXL\_S%3DZmJVcDm7PArixLQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHXA0_0z5jD8cZfkca8FNOmcg7FnEeXL_S%3DZmJVcDm7PArixLQ%40mail.gmail.com)[https://groups.google.com/d/msgid/elasticsearch/CAHXA0\_0z5jD8cZfkca8FNOmcg7FnEeXL\_S%3DZmJVcDm7PArixLQ%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAHXA0_0z5jD8cZfkca8FNOmcg7FnEeXL_S%3DZmJVcDm7PArixLQ%40mail.gmail.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/CAKmtHP-VZJ\_gUG1Z7yESL6yihQCn8zZJ4kyZm%2B6FeAFJsvnMqA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKmtHP-VZJ_gUG1Z7yESL6yihQCn8zZJ4kyZm%2B6FeAFJsvnMqA%40mail.gmail.com)[https://groups.google.com/d/msgid/elasticsearch/CAKmtHP-VZJ\_gUG1Z7yESL6yihQCn8zZJ4kyZm%2B6FeAFJsvnMqA%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAKmtHP-VZJ_gUG1Z7yESL6yihQCn8zZJ4kyZm%2B6FeAFJsvnMqA%40mail.gmail.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 a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/uSUHj5LcGdw/unsubscribe](https://groups.google.com/d/topic/elasticsearch/uSUHj5LcGdw/unsubscribe).  
> To unsubscribe from this group and all its topics, 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/CAHXA0\_2UpvcgQn9n4aifmfOEztP6AaK3hdHOD7TMrgU0HkhbvA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHXA0_2UpvcgQn9n4aifmfOEztP6AaK3hdHOD7TMrgU0HkhbvA%40mail.gmail.com)[https://groups.google.com/d/msgid/elasticsearch/CAHXA0\_2UpvcgQn9n4aifmfOEztP6AaK3hdHOD7TMrgU0HkhbvA%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAHXA0_2UpvcgQn9n4aifmfOEztP6AaK3hdHOD7TMrgU0HkhbvA%40mail.gmail.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/CAKmtHP8EvCWmOynZEqhdQ%2Bs2DQS-bs62Qj%3DNw9H76ktL%3DTvKKg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKmtHP8EvCWmOynZEqhdQ%2Bs2DQS-bs62Qj%3DNw9H76ktL%3DTvKKg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)
#### Post date: [April 24, 2014, 12:53pm UTC](https://discuss.elastic.co/t/use-facets-to-group-documents-by-index-and-type/17169/6 "2014-04-24T12:53:01Z")

</div>

Great! You're welcome!

On Thu, Apr 24, 2014 at 3:26 PM, Sviatoslav Abakumov \<  
[abakumov.sviatoslav@progforce.com](mailto:abakumov.sviatoslav@progforce.com)\> wrote:

> Magnificent! That's what I like to see.
> 
> Thank you so much!
> 
> On Thu, Apr 24, 2014 at 4:23 PM, Radu Gheorghe \<[radu.gheorghe@sematext.com](mailto:radu.gheorghe@sematext.com)
> 
> > wrote:
> 
> > Ah, I see now. Sorry for misreading your initial post. You can do that  
> > using scripts. This works for me:
> > 
> > curl localhost:9200/\_search?pretty -d '{  
> > "facets": {  
> > "idxtype": {  
> > "terms": {  
> > "script\_field": "doc['"'\_index'].value + '/' + doc['\_type'"'].value"  
> > }  
> > }  
> > }}'
> > 
> > Using doc['field\_name'].value is usually preferred to \_source or \_fields  
> > because with doc you're accessing values loaded in memory:  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html#_document_fields)
> > 
> > ## Best regards, Radu
> > 
> > Performance Monitoring \* Log Analytics \* Search Analytics  
> > Solr & Elasticsearch Support \* [http://sematext.com/](http://sematext.com/)
> > 
> > On Thu, Apr 24, 2014 at 2:54 PM, Sviatoslav Abakumov \<  
> > [abakumov.sviatoslav@progforce.com](mailto:abakumov.sviatoslav@progforce.com)\> wrote:
> > 
> > > Oh, yes, I've enabled `_index` so I can do term facet requests like:
> > > 
> > > ```
> > > {'field': '_index'}
> > > {'field': '_type'}
> > > 
> > > ```
> > > 
> > > And even:
> > > 
> > > ```
> > > {'fields': ['_index', '_type']}
> > > 
> > > ```
> > > 
> > > There was a hope that this request does what I want, but it merely  
> > > unites facets:
> > > 
> > > ```
> > > {'_type': 'terms',
> > > 'missing': 0,
> > > 'other': 0,
> > > 'terms': [{'count': 198, 'term': 'comment'},
> > > {'count': 99, 'term': 'light_beer'},
> > > {'count': 99, 'term': 'dark_beer'}]}
> > > 
> > > ```
> > > 
> > > Well, I want to concatenate index and type names and pass it to ES  
> > > facets. The only way I see is to use `script`, but I don't know how to  
> > > access `_index` and `_type` from there.
> > > 
> > > Thank you for understanding.
> > > 
> > > On Thu, Apr 24, 2014 at 3:30 PM, Radu Gheorghe \<  
> > > [radu.gheorghe@sematext.com](mailto:radu.gheorghe@sematext.com)\> wrote:
> > > 
> > > > Hello,
> > > > 
> > > > I'm not sure if you're already aware of the predefined \_index and \_type  
> > > > fields:
> > > > 
> > > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-index-field.html)
> > > > 
> > > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-type-field.html)
> > > > 
> > > > You can enable them so you can search and retrieve values like you  
> > > > would with any other field.
> > > > 
> > > > Best regards,  
> > > > Radu
> > > > 
> > > > On Thu, Apr 24, 2014 at 11:28 AM, Sviatoslav Abakumov \<  
> > > > [abakumov.sviatoslav@progforce.com](mailto:abakumov.sviatoslav@progforce.com)\> wrote:
> > > > 
> > > > > Hello,
> > > > > 
> > > > > I'd like to get how much documents of particular index AND type, like  
> > > > > this:
> > > > > 
> > > > > {'f4': {'\_type': 'terms',  
> > > > > 'missing': 0,  
> > > > > 'other': 0,  
> > > > > 'terms': [{'count': 99, 'term': 'light\_beer/comment'},  
> > > > > {'count': 99, 'term': 'dark\_beer/comment'}],  
> > > > > 'total': 198}}
> > > > > 
> > > > > To do this I added fields ` __index` and `__ type` and filled them when  
> > > > > I was indexing documents.  
> > > > > I use term facet with `"script": "_source. __index + '/' + _source.__ type"`
> > > > > 
> > > > > Is there a way to get index and type names from `_index` (IndexLookup)or `_source` (SourceLookup)?
> > > > > 
> > > > > Thank you.
> > > > > 
> > > > > --  
> > > > > 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/80604448-0bd5-499f-9c13-de096a732981%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/80604448-0bd5-499f-9c13-de096a732981%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/80604448-0bd5-499f-9c13-de096a732981%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/80604448-0bd5-499f-9c13-de096a732981%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > > > .  
> > > > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > > > 
> > > > --  
> > > > Performance Monitoring \* Log Analytics \* Search Analytics  
> > > > Solr & Elasticsearch Support \* [http://sematext.com/](http://sematext.com/)
> > > > 
> > > > --  
> > > > You received this message because you are subscribed to a topic in the  
> > > > Google Groups "elasticsearch" group.  
> > > > To unsubscribe from this topic, visit  
> > > > [https://groups.google.com/d/topic/elasticsearch/uSUHj5LcGdw/unsubscribe](https://groups.google.com/d/topic/elasticsearch/uSUHj5LcGdw/unsubscribe)  
> > > > .  
> > > > To unsubscribe from this group and all its topics, 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/CAHXA0\_0z5jD8cZfkca8FNOmcg7FnEeXL\_S%3DZmJVcDm7PArixLQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHXA0_0z5jD8cZfkca8FNOmcg7FnEeXL_S%3DZmJVcDm7PArixLQ%40mail.gmail.com)[https://groups.google.com/d/msgid/elasticsearch/CAHXA0\_0z5jD8cZfkca8FNOmcg7FnEeXL\_S%3DZmJVcDm7PArixLQ%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAHXA0_0z5jD8cZfkca8FNOmcg7FnEeXL_S%3DZmJVcDm7PArixLQ%40mail.gmail.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/CAKmtHP-VZJ\_gUG1Z7yESL6yihQCn8zZJ4kyZm%2B6FeAFJsvnMqA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKmtHP-VZJ_gUG1Z7yESL6yihQCn8zZJ4kyZm%2B6FeAFJsvnMqA%40mail.gmail.com)[https://groups.google.com/d/msgid/elasticsearch/CAKmtHP-VZJ\_gUG1Z7yESL6yihQCn8zZJ4kyZm%2B6FeAFJsvnMqA%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAKmtHP-VZJ_gUG1Z7yESL6yihQCn8zZJ4kyZm%2B6FeAFJsvnMqA%40mail.gmail.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 a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/uSUHj5LcGdw/unsubscribe](https://groups.google.com/d/topic/elasticsearch/uSUHj5LcGdw/unsubscribe).  
> > To unsubscribe from this group and all its topics, 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/CAHXA0\_2UpvcgQn9n4aifmfOEztP6AaK3hdHOD7TMrgU0HkhbvA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHXA0_2UpvcgQn9n4aifmfOEztP6AaK3hdHOD7TMrgU0HkhbvA%40mail.gmail.com)[https://groups.google.com/d/msgid/elasticsearch/CAHXA0\_2UpvcgQn9n4aifmfOEztP6AaK3hdHOD7TMrgU0HkhbvA%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAHXA0_2UpvcgQn9n4aifmfOEztP6AaK3hdHOD7TMrgU0HkhbvA%40mail.gmail.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/CAKmtHP8EvCWmOynZEqhdQ%2Bs2DQS-bs62Qj%3DNw9H76ktL%3DTvKKg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKmtHP8EvCWmOynZEqhdQ%2Bs2DQS-bs62Qj%3DNw9H76ktL%3DTvKKg%40mail.gmail.com)[https://groups.google.com/d/msgid/elasticsearch/CAKmtHP8EvCWmOynZEqhdQ%2Bs2DQS-bs62Qj%3DNw9H76ktL%3DTvKKg%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAKmtHP8EvCWmOynZEqhdQ%2Bs2DQS-bs62Qj%3DNw9H76ktL%3DTvKKg%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Performance Monitoring \* Log Analytics \* Search Analytics  
Solr & Elasticsearch Support \* [http://sematext.com/](http://sematext.com/)

--  
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/CAHXA0\_3dzcjyNUyUaSr0fqV078%2BRvXDL6tR4cKQr90ZYTeN%3DdA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHXA0_3dzcjyNUyUaSr0fqV078%2BRvXDL6tR4cKQr90ZYTeN%3DdA%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:33am UTC](https://discuss.elastic.co/t/use-facets-to-group-documents-by-index-and-type/17169/7 "2017-07-06T01:33:47Z")

</div>


