# Facets and a tree-like structure

**URL:** <https://discuss.elastic.co/t/facets-and-a-tree-like-structure/11168>\
**Category:** Elasticsearch\
**Created:** [March 15, 2013, 11:45am UTC](https://discuss.elastic.co/t/facets-and-a-tree-like-structure/11168 "2013-03-15T11:45:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pawel\_Mlynarczyk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pawel_mlynarczyk/32/1568_2.png) [@Pawel\_Mlynarczyk](https://discuss.elastic.co/u/Pawel_Mlynarczyk)\
**Post date:** [March 15, 2013, 11:45am UTC](https://discuss.elastic.co/t/facets-and-a-tree-like-structure/11168/1 "2013-03-15T11:45:15Z")

</div>

Hello everybody

I want to index a tree-like structure in ElasticSearch. That structure is  
an (let's call it) Item, that has a child Items. Each of the child may have  
their own child Items.  
What I'm trying to achieve is to make a text search on the Items and return  
root elements which have at least one child in the whole structure that  
matches the search.  
In addition I need to have a facet on another field in the structure and  
again, I would want it to count only the root elements that has at least  
one child that matches.  
I end up using parent/child objects in ElasticSearch, indexing root element  
as parent and flattening the rest of the tree structure so every item in  
the structure is a child of the root element.

> <https://gist.github.com/zwrss/5169241>

The searching in this case works fine, but I'm not able to create a terms  
facet on the child elements - I would like to have a facet on a particular  
field in a child.

Is there a way to handle those facets any other way, or maybe another way  
of handling the whole tree-like structure?

Thanks in advance  
Paweł

--  
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:** ![sslhj](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sslhj/32/119856_2.png) [@sslhj](https://discuss.elastic.co/u/sslhj)\
**Post date:** [March 18, 2013, 4:50am UTC](https://discuss.elastic.co/t/facets-and-a-tree-like-structure/11168/2 "2013-03-18T04:50:07Z")

</div>

I have the same problem as you, so far, no implemention is their in elasticsearch against the hierarchical facet，like path facet in bobo browse. but now i am trying a solution , let's say,  
we have the model  
a/b1/c  
a/b2/d  
i index the date as an array,  
['a', 'a/b1', 'a/b1/c']  
['a', 'a/b2', 'c/b2/d'],

when we need to facet 'a',

SearchResponse searchResponse = client1.prepareSearch()  
.setQuery(matchAllQuery())  
.addFacet(termsFacet("cate").fields("cate").size(10)  
.facetFilter(  
prefixFilter("cate", "a/"))  
)  
.execute().actionGet();

i get the result, 'a/b1' 1; 'a/b2' 1, though anothers, 'a' 2, is returned at the save time

---

<div class="post-metadata">

**Author:** ![Mohammady\_Mahdy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mohammady_mahdy/32/2128_2.png) [@Mohammady\_Mahdy](https://discuss.elastic.co/u/Mohammady_Mahdy)\
**Post date:** [March 19, 2013, 11:26am UTC](https://discuss.elastic.co/t/facets-and-a-tree-like-structure/11168/3 "2013-03-19T11:26:37Z")

</div>

Hmm, if you are still having this problem you can take a look at this:

> **[Hierarchical Faceting With Elastic Search](http://www.springyweb.com/2012/01/hierarchical-faceting-with-elastic.html)**
>
> Our current project is using ElasticSearch for, er search. Hierarchical categorization of content found itself pinned to our sprint plannin...

On Friday, March 15, 2013 3:45:15 PM UTC+4, Paweł Młynarczyk wrote:

> Hello everybody
> 
> I want to index a tree-like structure in Elasticsearch. That structure is  
> an (let's call it) Item, that has a child Items. Each of the child may have  
> their own child Items.  
> What I'm trying to achieve is to make a text search on the Items and  
> return root elements which have at least one child in the whole structure  
> that matches the search.  
> In addition I need to have a facet on another field in the structure and  
> again, I would want it to count only the root elements that has at least  
> one child that matches.  
> I end up using parent/child objects in Elasticsearch, indexing root  
> element as parent and flattening the rest of the tree structure so every  
> item in the structure is a child of the root element.
> 
> [ElasticSearch tree structure search. · GitHub](https://gist.github.com/zwrss/5169241)
> 
> The searching in this case works fine, but I'm not able to create a terms  
> facet on the child elements - I would like to have a facet on a particular  
> field in a child.
> 
> Is there a way to handle those facets any other way, or maybe another way  
> of handling the whole tree-like structure?
> 
> Thanks in advance  
> Paweł

--  
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:45am UTC](https://discuss.elastic.co/t/facets-and-a-tree-like-structure/11168/4 "2017-07-06T02:45:50Z")

</div>


