# Query with "mandatory" filter

**URL:** <https://discuss.elastic.co/t/query-with-mandatory-filter/116443>\
**Category:** Elasticsearch\
**Created:** [January 22, 2018, 8:46am UTC](https://discuss.elastic.co/t/query-with-mandatory-filter/116443 "2018-01-22T08:46:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kuseman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kuseman/32/60609_2.png) [@kuseman](https://discuss.elastic.co/u/kuseman)\
**Post date:** [January 22, 2018, 8:46am UTC](https://discuss.elastic.co/t/query-with-mandatory-filter/116443/1 "2018-01-22T08:46:36Z")

</div>

Hi,

Started to migrate our system to ES 6/7 single type and having some troubles i need assistance with.  
We have a system which makes use of mustache templates and we query (before migration) a type along with a template and parameters, now when migrating to a single type I've added our previously type into a new field **\_docType** to our documents and now to get this running I must in every template add a term-filter for **\_docType** , and this is the part I don't like. I don't want to remember to add this in each new template that is written and would like to make use of URI-query to always append a filter for **\_docType** ie. **/\_search/template?q=\_docType:SomeType**.

Problem with this is that if I have a "query"-clause in my template it's not getting applied, the URI query takes precedence.

Is there any other way to accomplish this?

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [January 22, 2018, 8:54am UTC](https://discuss.elastic.co/t/query-with-mandatory-filter/116443/2 "2018-01-22T08:54:42Z")

</div>

You could also use a [filtered alias](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html#filtered) to achieve the same thing. That wouldn't change your query at all, but instead of querying your index, you'd query the filtered alias

```
# create your filtered alias
POST /_aliases
{
    "actions" : [
        {
            "add" : {
                 "index" : "your-index",
                 "alias" : "your-alias",
                 "filter" : { "term" : { "_docType" : "SomeType" } }
            }
        }
    ]
}

# 2. query the alias instead of the index
GET your-alias/_search?q=...
```

---

<div class="post-metadata">

**Author:** ![kuseman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kuseman/32/60609_2.png) [@kuseman](https://discuss.elastic.co/u/kuseman)\
**Post date:** [January 22, 2018, 11:00am UTC](https://discuss.elastic.co/t/query-with-mandatory-filter/116443/3 "2018-01-22T11:00:44Z")

</div>

Thanks, know about aliases and might go for that one, will need some more work for our framework but might be worth it.

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [January 22, 2018, 11:02am UTC](https://discuss.elastic.co/t/query-with-mandatory-filter/116443/4 "2018-01-22T11:02:01Z")

</div>

Cool, let us know in case that works out for you.

---

<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:** [February 19, 2018, 11:02am UTC](https://discuss.elastic.co/t/query-with-mandatory-filter/116443/5 "2018-02-19T11:02:21Z")

</div>

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