# Is it possible to make filter queries for every named\_query

**URL:** <https://discuss.elastic.co/t/is-it-possible-to-make-filter-queries-for-every-named-query/67098>\
**Category:** Elasticsearch\
**Created:** [November 24, 2016, 11:19am UTC](https://discuss.elastic.co/t/is-it-possible-to-make-filter-queries-for-every-named-query/67098 "2016-11-24T11:19:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joomler](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@Joomler](https://discuss.elastic.co/u/Joomler)\
**Post date:** [November 24, 2016, 11:19am UTC](https://discuss.elastic.co/t/is-it-possible-to-make-filter-queries-for-every-named-query/67098/1 "2016-11-24T11:19:37Z")

</div>

Is it possible to make filter queries for every named\_query?  
Suppose i have several named queries, i want some kind of aggregation for them: make filter to narrow for dynamic named queries.  
The problem is that in one query i cannot achieve this, because during query i don't know the name.

[https://www.elastic.co/guide/en/elasticsearch/reference/1.7/search-request-named-queries-and-filters.html](https://www.elastic.co/guide/en/elasticsearch/reference/1.7/search-request-named-queries-and-filters.html)

---

<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:** [November 25, 2016, 3:04pm UTC](https://discuss.elastic.co/t/is-it-possible-to-make-filter-queries-for-every-named-query/67098/2 "2016-11-25T15:04:44Z")

</div>

Sorry but I have trouble understanding what you want to achieve. Are you looking for the terms aggregation maybe? [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html)

---

<div class="post-metadata">

**Author:** ![Joomler](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@Joomler](https://discuss.elastic.co/u/Joomler)\
**Post date:** [November 25, 2016, 6:15pm UTC](https://discuss.elastic.co/t/is-it-possible-to-make-filter-queries-for-every-named-query/67098/3 "2016-11-25T18:15:47Z")

</div>

What i read in docs is facets implemented on fields, for example dates or authors, then filtering is done like this:

```
Tom (345)
Mary (218)
...

```

But this is not what i want. I have in my models only 1 text field and facets are aggregated dynamically during search. So, for movie texts i want facets like this:

```
Detective (633)
Comedy (237)
...

```

and each genre is calculated by search query:

Detective

text: "Columbo is an American television series starring Peter Falk as Columbo, a homicide detective with the Los Angeles Police Department.[2][3] The character and show, created by William Link and Richard Levinson, popularized the inverted detective story format, which begins by showing the commission of the crime and its perpetrator; the series therefore has no "whodunit" element. The plot revolves around how a perpetrator whose identity is already known to the audience will finally be caught and exposed (which the show's writers called a "howcatchem," rather than a "whodunit")."

to detect category we do search: (cop | murder | crime | ...) OR (story | ...)  
The key idea, that i do several complex named queries to make some data mining.

Then, after i done named queries, i must write name\_of\_query (\_name) data to database to allow user filter it in web interface of my app.

Is there a way to get name\_of\_query with elastic to do filtering like facets?

---

<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:** [November 28, 2016, 9:45am UTC](https://discuss.elastic.co/t/is-it-possible-to-make-filter-queries-for-every-named-query/67098/4 "2016-11-28T09:45:11Z")

</div>

OK, got it. You can use a `filters` aggregation: [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filters-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filters-aggregation.html)

For the record, note that it would bemore efficient to compute the genre at index time and then to do a `terms` aggregation on the `genre` field.

---

<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:** [December 26, 2016, 9:45am UTC](https://discuss.elastic.co/t/is-it-possible-to-make-filter-queries-for-every-named-query/67098/5 "2016-12-26T09:45:48Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
