Query with "mandatory" filter


(Marcus) #1

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?


(Val Crettaz) #2

You could also use a filtered alias 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=...

(Marcus) #3

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


(Val Crettaz) #4

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


(system) #5

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