# How to use Term Facet query with multivalue fields?

**URL:** <https://discuss.elastic.co/t/how-to-use-term-facet-query-with-multivalue-fields/11672>\
**Category:** Elasticsearch\
**Created:** [April 23, 2013, 8:58pm UTC](https://discuss.elastic.co/t/how-to-use-term-facet-query-with-multivalue-fields/11672 "2013-04-23T20:58:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pulkitsinghal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pulkitsinghal/32/802_2.png) [@pulkitsinghal](https://discuss.elastic.co/u/pulkitsinghal)\
**Post date:** [April 23, 2013, 8:58pm UTC](https://discuss.elastic.co/t/how-to-use-term-facet-query-with-multivalue-fields/11672/1 "2013-04-23T20:58:02Z")

</div>

I have a document with multivalue fields:

> <https://gist.github.com/pulkitsinghal/5447250#file-document-json>
>
> There are more than three files. show original

But when I run a facet query on it:

> <https://gist.github.com/pulkitsinghal/5447250#file-facetquery-json>
>
> There are more than three files. show original

Each term value returns a total count of 1, even though the count should be  
more than that:

> <https://gist.github.com/pulkitsinghal/5447250#file-result-json>
>
> There are more than three files. show original

1. What's the right way to setup the document data or mappings in order to  
measure the count of the term facets accurately?  
[https://gist.github.com/pulkitsinghal/5447250#file-\_mapping-json](https://gist.github.com/pulkitsinghal/5447250#file-_mapping-json)

2. Perhaps its the query that needs to be tweaked?

- Pulkit

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [April 23, 2013, 9:09pm UTC](https://discuss.elastic.co/t/how-to-use-term-facet-query-with-multivalue-fields/11672/2 "2013-04-23T21:09:25Z")

</div>

A TermsFacet counts the number of docs, not the number of terms per doc or field.

Perhaps, you can use a script to render it, but I'm not sure of that.  
See [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/script-fields/)

curl [http://localhost:9200/index/\_search?pretty](http://localhost:9200/index/_search?pretty) -d '  
{  
"query" : {  
"match\_all" : { }  
},  
"script\_fields" : {  
"tag" : {  
"script" : "doc["field\_name"].values.length"  
}  
}  
}'

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr | @scrutmydocs

Le 23 avr. 2013 à 22:58, pulkitsinghal [pulkitsinghal@gmail.com](mailto:pulkitsinghal@gmail.com) a écrit :

> I have a document with multivalue fields:  
> [Multivalue field and facet query - unexpected results... · GitHub](https://gist.github.com/pulkitsinghal/5447250#file-document-json)
> 
> But when I run a facet query on it:  
> [Multivalue field and facet query - unexpected results... · GitHub](https://gist.github.com/pulkitsinghal/5447250#file-facetquery-json)
> 
> Each term value returns a total count of 1, even though the count should be more than that:  
> [Multivalue field and facet query - unexpected results... · GitHub](https://gist.github.com/pulkitsinghal/5447250#file-result-json)
> 
> 1. What's the right way to setup the document data or mappings in order to measure the count of the term facets accurately?  
> [Multivalue field and facet query - unexpected results... · GitHub](https://gist.github.com/pulkitsinghal/5447250#file-_mapping-json)
> 
> 2. Perhaps its the query that needs to be tweaked?
> 
> - Pulkit
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![pulkitsinghal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pulkitsinghal/32/802_2.png) [@pulkitsinghal](https://discuss.elastic.co/u/pulkitsinghal)\
**Post date:** [April 24, 2013, 1:33am UTC](https://discuss.elastic.co/t/how-to-use-term-facet-query-with-multivalue-fields/11672/3 "2013-04-24T01:33:16Z")

</div>

On Tue, Apr 23, 2013 at 4:09 PM, David Pilato [david@pilato.fr](mailto:david@pilato.fr) wrote:

> A TermsFacet counts the number of docs, not the number of terms per doc or  
> field.

Ok, so I started again in a different index with the mapping set to nested:  
"properties" : {  
"bought\_together" : {  
"type" : "nested"  
}  
}

So I've effectively changed the mapping from:

> <https://gist.github.com/pulkitsinghal/5447250#file-_mapping_index3-json>
>
> There are more than three files. show original

to

> <https://gist.github.com/pulkitsinghal/5447250#file-_mapping_index4-json>
>
> There are more than three files. show original

Now that its indexing it as a nested document, I would expect the term  
facet count to function so once again I run:

> <https://gist.github.com/pulkitsinghal/5447250#file-facetquery-json>
>
> There are more than three files. show original

But there is still some gap in my understanding because I don't get any  
results:  
facets: {  
tag: {  
\_type: terms  
missing: 1  
total: 0  
other: 0  
terms:   
}  
}

Thoughts? Tips?

--  
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).  
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, 2:39am UTC](https://discuss.elastic.co/t/how-to-use-term-facet-query-with-multivalue-fields/11672/4 "2017-07-06T02:39:52Z")

</div>


