# Subsearch within a facet

**URL:** <https://discuss.elastic.co/t/subsearch-within-a-facet/3865>\
**Category:** Elasticsearch\
**Created:** [February 2, 2011, 4:45pm UTC](https://discuss.elastic.co/t/subsearch-within-a-facet/3865 "2011-02-02T16:45:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)\
**Post date:** [February 2, 2011, 4:45pm UTC](https://discuss.elastic.co/t/subsearch-within-a-facet/3865/1 "2011-02-02T16:45:16Z")

</div>

Hi,

I want my users to search products and then I'm executing a faceted search,  
so they can also get the categories all those products belong to. I have  
three levels of categorias, named "categorias1", "categorias2", and  
"categorias3", where highest level is 1, then 2, then 3, as expected. If I  
execute this:

{

"from" : 0, "size" : 10,

"query" : {

"fuzzy" : { "nombre" : "pc" }

},

facets : {

"myfacet" : {

```
        "terms" : {

            "field" : "categorias1",

            "size" : 10

        }

    }

}

```

}

This query works like a charm, but let's say now the user clicks on a  
specific "categorias1", and now I want to display the search results within  
that "categorias1", and faceted as well by the next level, i.e.  
"categorias2". I'm trying this query without success:

{

"from" : 0, "size" : 2,

"query" : {

"fuzzy" : { "nombre" : "pc" }

},

"filter" : {

"term" : { "categorias1" : "Informática" }

},

facets : {

```
     "myfacet" : {

       "terms" : {

            "field" : "categorias2",

            "size" : 10

        }

    }

}

```

}

This is based on issue 650 and I'm running ES 0.15.0-SNAPSHOT (built with  
Gradle):

> <https://github.com/elastic/elasticsearch/issues/650>

Is this expected to provide the products within the specific "categorias1"  
that the user has clicked on, faceted with next level "categorias2"? Or do I  
have to build the query in a different way?

Thanks.

---

<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:** [February 2, 2011, 6:02pm UTC](https://discuss.elastic.co/t/subsearch-within-a-facet/3865/2 "2011-02-02T18:02:31Z")

</div>

You need to adda a facet\_filter to the terms facet, otherwise, it will be compute the facets only for the query (without the filter from 650).  
On Wednesday, February 2, 2011 at 6:45 PM, Enrique Medina Montenegro wrote:

> Hi,
> 
> I want my users to search products and then I'm executing a faceted search, so they can also get the categories all those products belong to. I have three levels of categorias, named "categorias1", "categorias2", and "categorias3", where highest level is 1, then 2, then 3, as expected. If I execute this:
> 
> {  
> "from" : 0, "size" : 10,  
> "query" : {  
> "fuzzy" : { "nombre" : "pc" }  
> },  
> facets : {  
> "myfacet" : {  
> "terms" : {  
> "field" : "categorias1",  
> "size" : 10  
> }  
> }  
> }  
> }
> 
> This query works like a charm, but let's say now the user clicks on a specific "categorias1", and now I want to display the search results within that "categorias1", and faceted as well by the next level, i.e. "categorias2". I'm trying this query without success:
> 
> {  
> "from" : 0, "size" : 2,  
> "query" : {  
> "fuzzy" : { "nombre" : "pc" }  
> },  
> "filter" : {  
> "term" : { "categorias1" : "InformÃ¡tica" }  
> },  
> facets : {  
> "myfacet" : {  
> "terms" : {  
> "field" : "categorias2",  
> "size" : 10  
> }  
> }  
> }  
> }
> 
> This is based on issue 650 and I'm running ES 0.15.0-SNAPSHOT (built with Gradle):
> 
> [Search: Allow to pass a search filter, applying only on the query (and not on facets for example) · Issue #650 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/issue/650)
> 
> Is this expected to provide the products within the specific "categorias1" that the user has clicked on, faceted with next level "categorias2"? Or do I have to build the query in a different way?
> 
> Thanks.

---

<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, 4:12am UTC](https://discuss.elastic.co/t/subsearch-within-a-facet/3865/3 "2017-07-06T04:12:50Z")

</div>


