user3658423
user3658423

Reputation: 1944

elasticsearch and filters optimization

Does elasticsearch optimize filters automatically? For example: In an "and" filter, if there are multiple numeric range filters defined separately for gte and lte operators instead of one range filter with both lte and gte bounds, will it cause any performance issues? Would ES automatically optimize these filters?

"filter" : {
"and" : [
    {
    "range" : {
            "age" : {
            "gte": 10
            }
    }
    },
    {
    "range" : {
            "age" : {
                "lte": 90
            }
    }
    },
    {
    "range" : {
            "age" : {
            "gte": 30
            }
    }
    }                                                               
]}

Update

Wanted to add: while it makes sense to combine the bounds, I wanted to check this incase when my users who can dynamically create filters do such a scenario.

Upvotes: 0

Views: 94

Answers (1)

Val
Val

Reputation: 217274

The main rule of thumb with filters is that you should always apply the most restrictive filters first, so as to reduce the set of matched documents and allow the next filter to work on as few documents as possible.

Moreover, in the case you're showing, it doesn't really make sense to have three different range filters for the same age field, you should consolidate them into a single one:

"filter": {
    "range": {
        "age": {
            "gte": 30,
            "lte": 90
        }
    }
}

Also note that you should prefer bool/must over and filters whenever possible as explained in this excellent article, mainly because and filters are not cached.

Finally, the above only applies to pre-2.0 Elasticsearch version. As of 2.0, the whole query/filter DSL will be completely overhauled and queries will be much smarter.

UPDATE

Since your users can create their own filters, the main rule of thumb stays, i.e. you should try to order the filters in such a way that the most restrictive filter comes first. However, if your users really have the power to create their own filters and you're not interfering with what they submit, then you should educate them (if at all possible) on how to best create filters.

Upvotes: 2

Related Questions