# Get faceted results based on query only

**URL:** <https://discuss.elastic.co/t/get-faceted-results-based-on-query-only/14130>\
**Category:** Elasticsearch\
**Created:** [October 27, 2013, 7:46pm UTC](https://discuss.elastic.co/t/get-faceted-results-based-on-query-only/14130 "2013-10-27T19:46:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![LM1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lm1/32/2015_2.png) [@LM1](https://discuss.elastic.co/u/LM1)\
**Post date:** [October 27, 2013, 7:46pm UTC](https://discuss.elastic.co/t/get-faceted-results-based-on-query-only/14130/1 "2013-10-27T19:46:54Z")

</div>

I have some posts data and I use elasticsearch to search in title of these  
posts. Then I use filters to filter results based on category ids and some  
other parameters. What I'm trying to do is to get all categories of posts  
that matches the query criteria and ignore the filter.

Here is my what I have done so far. This is the query part:

if ( ! empty($search) )  
{  
$query['multi\_match'] = array(  
'query' =\> $search,  
'fields' =\> array('title', 'title\_en')  
);  
}

Then I apply some filters:

if ( ! empty($loc) )  
{  
$filter['bool']['must'][]['geo\_distance'] = array(  
'distance' =\> $distance . 'meters',  
'loc' =\> $loc  
);  
}

if ( isset($category) )  
{  
$filter['bool']['must'][]['terms'] = array(  
'categories' =\> $category  
);  
}

And then I facet part:

$facets = array(  
'categories' =\> array(  
'terms' =\> array(  
'field' =\> 'categories'  
)  
)  
);

and the final part:

$searchParams['body']['query']['filtered'] = array(  
'query' =\> $query,  
'filter' =\> $filter,  
);  
$searchParams['body']['facets'] = $facets;

The problem is that when I apply geo\_distance or some other filters, this  
affects what is returned by the facet. I want the facet to be affected by  
the query only, so if the $search variable returns 100 results and then geo  
distance reduces that to 10, facet should return all categories from that  
100 results, not 10. Is that possible using elasticsearch?

--  
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:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [October 27, 2013, 8:27pm UTC](https://discuss.elastic.co/t/get-faceted-results-based-on-query-only/14130/2 "2013-10-27T20:27:29Z")

</div>

Hi,

I'm not fluent enough in PHP to understand what the code is doing but it  
looks like what you are interested in are top-level filters. On the  
contrary to filtered queries, these filters don't apply to facets. See

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

.

On Sun, Oct 27, 2013 at 8:46 PM, LM [levani9191@gmail.com](mailto:levani9191@gmail.com) wrote:

> I have some posts data and I use elasticsearch to search in title of these  
> posts. Then I use filters to filter results based on category ids and some  
> other parameters. What I'm trying to do is to get all categories of posts  
> that matches the query criteria and ignore the filter.
> 
> Here is my what I have done so far. This is the query part:
> 
> if ( ! empty($search) )  
> {  
> $query['multi\_match'] = array(  
> 'query' =\> $search,  
> 'fields' =\> array('title', 'title\_en')  
> );  
> }
> 
> Then I apply some filters:
> 
> if ( ! empty($loc) )  
> {  
> $filter['bool']['must']['geo\_distance'] = array(  
> 'distance' =\> $distance . 'meters',  
> 'loc' =\> $loc  
> );  
> }
> 
> if ( isset($category) )  
> {  
> $filter['bool']['must']['terms'] = array(  
> 'categories' =\> $category  
> );  
> }
> 
> And then I facet part:
> 
> $facets = array(  
> 'categories' =\> array(  
> 'terms' =\> array(  
> 'field' =\> 'categories'  
> )  
> )  
> );
> 
> and the final part:
> 
> $searchParams['body']['query']['filtered'] = array(  
> 'query' =\> $query,  
> 'filter' =\> $filter,  
> );  
> $searchParams['body']['facets'] = $facets;
> 
> The problem is that when I apply geo\_distance or some other filters, this  
> affects what is returned by the facet. I want the facet to be affected by  
> the query only, so if the $search variable returns 100 results and then  
> geo distance reduces that to 10, facet should return all categories from  
> that 100 results, not 10. Is that possible using elasticsearch?
> 
> --  
> 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).

--  
Adrien Grand

--  
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:10am UTC](https://discuss.elastic.co/t/get-faceted-results-based-on-query-only/14130/3 "2017-07-06T02:10:24Z")

</div>


