# Returning only certain-values in facet search in multi-value field

**URL:** <https://discuss.elastic.co/t/returning-only-certain-values-in-facet-search-in-multi-value-field/8094>\
**Category:** Elasticsearch\
**Created:** [June 13, 2012, 10:39pm UTC](https://discuss.elastic.co/t/returning-only-certain-values-in-facet-search-in-multi-value-field/8094 "2012-06-13T22:39:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Peach](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matt_peach/32/2848_2.png) [@Matt\_Peach](https://discuss.elastic.co/u/Matt_Peach)\
**Post date:** [June 13, 2012, 10:39pm UTC](https://discuss.elastic.co/t/returning-only-certain-values-in-facet-search-in-multi-value-field/8094/1 "2012-06-13T22:39:34Z")

</div>

Hi all,

I'm running a facet query against a multi-valued field, and I'm wondering  
whether it's possible to only return counts for certain values in the  
field. (As opposed to counts for certain documents.)

So, for example, I have three documents as such:

document {  
categories: [  
1,  
2,  
3,  
4  
]  
}

document {  
categories: [  
1,  
99,  
98,  
97  
]  
}

document {  
categories: [  
2  
]  
}

From my testing, if I facet against the categories field with no filtering,  
I will get an array as such:

[  
{ "term" : "1", "count" : 2 },  
{ "term" : "2", "count" : 2 },  
{ "term" : "3", "count" : 1 },  
{ "term" : "4", "count" : 1 },  
{ "term" : "99", "count" : 1 },  
{ "term" : "98", "count" : 1 },  
{ "term" : "97", "count" : 1 },  
{ "term" : "2", "count" : 1 }  
]

I can also use a facet filter to only search documents that contain  
category "1", which eliminates the "2" document:

[  
{ "term" : "1", "count" : 2 },  
{ "term" : "2", "count" : 1 },  
{ "term" : "3", "count" : 1 },  
{ "term" : "4", "count" : 1 },  
{ "term" : "99", "count" : 1 },  
{ "term" : "98", "count" : 1 },  
{ "term" : "97", "count" : 1 }  
]

What I'd like to do is only return counts for the values I care about; for  
example, I want to know the counts for categories "1" and "2":

[  
{ "term" : "1", "count" : 2 },  
{ "term" : "2", "count" : 2 }  
]

The rationale is that I already know exactly which categories my  
application needs counts for, and want to reduce the response size of the  
search. (My real-world example is returning counts for 200+ terms, and I  
only need eight of them.)

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 14, 2012, 5:25am UTC](https://discuss.elastic.co/t/returning-only-certain-values-in-facet-search-in-multi-value-field/8094/2 "2012-06-14T05:25:30Z")

</div>

Heya,

Did you look at [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/index.html) ?  
There's a section about Facet Filter.

Can this help you ?  
HTH  
David

Le 14 juin 2012 à 00:39, Matt Peach [matthew.peach@mightyape.co.nz](mailto:matthew.peach@mightyape.co.nz) a écrit :

> Hi all,
> 
> I'm running a facet query against a multi-valued field, and I'm wondering whether it's possible to only return counts for certain values in the field. (As opposed to counts for certain documents.)
> 
> So, for example, I have three documents as such:
> 
> document {  
> categories: [  
> 1,  
> 2,  
> 3,  
> 4  
> ]  
> }
> 
> document {  
> categories: [  
> 1,  
> 99,  
> 98,  
> 97  
> ]  
> }
> 
> document {  
> categories: [  
> 2  
> ]  
> }
> 
> From my testing, if I facet against the categories field with no filtering, I will get an array as such:
> 
> [  
> { "term" : "1", "count" : 2 },  
> { "term" : "2", "count" : 2 },  
> { "term" : "3", "count" : 1 },  
> { "term" : "4", "count" : 1 },  
> { "term" : "99", "count" : 1 },  
> { "term" : "98", "count" : 1 },  
> { "term" : "97", "count" : 1 },  
> { "term" : "2", "count" : 1 }  
> ]
> 
> I can also use a facet filter to only search documents that contain category "1", which eliminates the "2" document:
> 
> [  
> { "term" : "1", "count" : 2 },  
> { "term" : "2", "count" : 1 },  
> { "term" : "3", "count" : 1 },  
> { "term" : "4", "count" : 1 },  
> { "term" : "99", "count" : 1 },  
> { "term" : "98", "count" : 1 },  
> { "term" : "97", "count" : 1 }  
> ]
> 
> What I'd like to do is only return counts for the values I care about; for example, I want to know the counts for categories "1" and "2":
> 
> [  
> { "term" : "1", "count" : 2 },  
> { "term" : "2", "count" : 2 }  
> ]
> 
> The rationale is that I already know exactly which categories my application needs counts for, and want to reduce the response size of the search. (My real-world example is returning counts for 200+ terms, and I only need eight of them.)

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 14, 2012, 5:28am UTC](https://discuss.elastic.co/t/returning-only-certain-values-in-facet-search-in-multi-value-field/8094/3 "2012-06-14T05:28:07Z")

</div>

Sorry. Forget my answer.

Le 14 juin 2012 à 07:25, David Pilato [david@pilato.fr](mailto:david@pilato.fr) a écrit :

> Heya,
> 
> Did you look at [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/index.html) ?  
> There's a section about Facet Filter.
> 
> Can this help you ?  
> HTH  
> David
> 
> Le 14 juin 2012 à 00:39, Matt Peach [matthew.peach@mightyape.co.nz](mailto:matthew.peach@mightyape.co.nz) a écrit :
> 
> > Hi all,
> > 
> > I'm running a facet query against a multi-valued field, and I'm wondering whether it's possible to only return counts for certain values in the field. (As opposed to counts for certain documents.)
> > 
> > So, for example, I have three documents as such:
> > 
> > document {  
> > categories: [  
> > 1,  
> > 2,  
> > 3,  
> > 4  
> > ]  
> > }
> > 
> > document {  
> > categories: [  
> > 1,  
> > 99,  
> > 98,  
> > 97  
> > ]  
> > }
> > 
> > document {  
> > categories: [  
> > 2  
> > ]  
> > }
> > 
> > From my testing, if I facet against the categories field with no filtering, I will get an array as such:
> > 
> > [  
> > { "term" : "1", "count" : 2 },  
> > { "term" : "2", "count" : 2 },  
> > { "term" : "3", "count" : 1 },  
> > { "term" : "4", "count" : 1 },  
> > { "term" : "99", "count" : 1 },  
> > { "term" : "98", "count" : 1 },  
> > { "term" : "97", "count" : 1 },  
> > { "term" : "2", "count" : 1 }  
> > ]
> > 
> > I can also use a facet filter to only search documents that contain category "1", which eliminates the "2" document:
> > 
> > [  
> > { "term" : "1", "count" : 2 },  
> > { "term" : "2", "count" : 1 },  
> > { "term" : "3", "count" : 1 },  
> > { "term" : "4", "count" : 1 },  
> > { "term" : "99", "count" : 1 },  
> > { "term" : "98", "count" : 1 },  
> > { "term" : "97", "count" : 1 }  
> > ]
> > 
> > What I'd like to do is only return counts for the values I care about; for example, I want to know the counts for categories "1" and "2":
> > 
> > [  
> > { "term" : "1", "count" : 2 },  
> > { "term" : "2", "count" : 2 }  
> > ]
> > 
> > The rationale is that I already know exactly which categories my application needs counts for, and want to reduce the response size of the search. (My real-world example is returning counts for 200+ terms, and I only need eight of them.)

---

<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:24am UTC](https://discuss.elastic.co/t/returning-only-certain-values-in-facet-search-in-multi-value-field/8094/4 "2017-07-06T03:24:18Z")

</div>


