Query template that will append to existing query for further filtering out results

We have an existing "person" index that has "status" field in it. We have several (around 6) existing queries for retrieving person document with different parameters and logic.

However, we have a new requirement that only "active" status will be retrieved from all queries. Is it possible to apply a "template" and append it to all queries searching on the person index without updating all queries one by one to apply filter for status? This is the query that we need to append for every existing queries we have:

{
"bool": {
    "should": [
        {
            "nested": {
                "path": "additionalAttributes",
                "query": {
                    "terms": {
                        "additionalAttributes.status": [
                            "active"
                        ]
                    }
                }
            }
        },
        {
            "bool": {
                "must_not": [
                    {
                        "nested": {
                            "path": "additionalAttributes",
                            "query": {
                                "exists": {
                                    "field": "additionalAttributes.status"
                                }
                            }
                        }
                    }
                ]
            }
        }
    ]
}

You can use a filtered alias. Instead or searching in the concrete index, search within the alias.

See Aliases | Elasticsearch Guide [8.7] | Elastic

thanks, do you know if this is supported with spring data in java?

It's more an admin question than a dev question. So it should not change a lot of things for your code.

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