1

Is is possible to write a conditional filter on an Elasticsearch multi-index query?

I am looking at the filter script, but I can't see anywhere in the documentation if the documents index is a variable I can check?

My existing query looks like this, note the filter script doesn't work - but I assume this is where I need to do my query.

{
    "index": "tweets,articles,animals,buildings",
    "type": "item",
    "body": {
        "query": {
            "multi_match": {
                "query": "cat",
                "type": "phrase_prefix",
                "fields": [
                    "label",
                    "body"
                ]
            }
        },
        "filter": {
            "script": {
                "script": "if (_index == \"animals\") {return true;} else {return false}
            }
        },
        "from": 0,
        "size": 8
    }
}

Obviously I'd like to do more in this filter than just exclude items from a certain index, this is simply an example.

2
  • which version of elasticsearch ? Commented Apr 14, 2016 at 17:16
  • 2.1.1 currently, but possible to upgrade if needed? Commented Apr 14, 2016 at 21:58

1 Answer 1

3

You should be able to combine several indices query to solve this task.

Sign up to request clarification or add additional context in comments.

1 Comment

indices query is deprecated in 5.0. But you can filter on _index field just like any other field. {"term": {"_index": "animals"}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.