# Hierarchy Facet Script

**URL:** <https://discuss.elastic.co/t/hierarchy-facet-script/7376>\
**Category:** Elasticsearch\
**Created:** [April 18, 2012, 10:07am UTC](https://discuss.elastic.co/t/hierarchy-facet-script/7376 "2012-04-18T10:07:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lorenzo\_Melegari](https://avatars.discourse-cdn.com/v4/letter/l/13edae/32.png) [@Lorenzo\_Melegari](https://discuss.elastic.co/u/Lorenzo_Melegari)\
**Post date:** [April 18, 2012, 10:07am UTC](https://discuss.elastic.co/t/hierarchy-facet-script/7376/1 "2012-04-18T10:07:57Z")

</div>

Hi,  
I have indexed documents with a multivalue field (MYFIELD)  
on which I run the following query:

{  
"query": {  
"query\_string": {  
"query": "_"  
}  
},  
"facets": {  
"MYFIELD\_facet": {  
"terms": {  
"field": "MYFIELD",  
"allterms": true,  
\* "script": "splitted = term.split('/'); return splitted[0];"_  
}  
}  
}  
}

The query succeeded, however results are different than what I expected.  
If a document contains values 'A/B' and 'A/C' for field MYFIELD it is  
counted twice in the facet count!

- 

facets: {  
- MYFIELD\_facet\_H1: {  
- \_type: terms  
- terms: [  
- {  
- term: A  
- count: 2  
}

I am now using ES 0.19, I need this feature for production soon (I am using  
this trick to handle hierarchies).  
Any help is appreciated! Thanks in advance

Lorenzo Melegari

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [April 19, 2012, 2:38pm UTC](https://discuss.elastic.co/t/hierarchy-facet-script/7376/2 "2012-04-19T14:38:50Z")

</div>

You mean the same doc has A/B and A/C? Then yea, it will be counted twice  
(not per doc, but per term).

On Wed, Apr 18, 2012 at 1:07 PM, Lorenzo Melegari \<  
[lorenzo.melegari@gmail.com](mailto:lorenzo.melegari@gmail.com)\> wrote:

> Hi,  
> I have indexed documents with a multivalue field (MYFIELD)  
> on which I run the following query:
> 
> {  
> "query": {  
> "query\_string": {  
> "query": "_"  
> }  
> },  
> "facets": {  
> "MYFIELD\_facet": {  
> "terms": {  
> "field": "MYFIELD",  
> "allterms": true,  
> \* "script": "splitted = term.split('/'); return splitted[0];"_  
> }  
> }  
> }  
> }
> 
> The query succeeded, however results are different than what I expected.  
> If a document contains values 'A/B' and 'A/C' for field MYFIELD it is  
> counted twice in the facet count!
> 
> - 
> 
> facets: {  
> - MYFIELD\_facet\_H1: {  
> - \_type: terms  
> - terms: [  
> - {  
> - term: A  
> - count: 2  
> }
> 
> I am now using ES 0.19, I need this feature for production soon (I am  
> using this trick to handle hierarchies).  
> Any help is appreciated! Thanks in advance
> 
> Lorenzo Melegari

---

<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:31am UTC](https://discuss.elastic.co/t/hierarchy-facet-script/7376/3 "2017-07-06T03:31:55Z")

</div>


