What's the best performance, to execute two different queries or a single?

Hey, guys.
I'm planning a new feature and I would like to know what performs better.
Explaining a bit the scenario:

I have two different filters to be executed and generate different aggregations.

I can either:

  • Have a single search with no query filters and two filter aggregations that will split the data and perform inner aggregations.
  • Have two different searches, each with its query filter and aggregation to generate results.

Example:
Suppose that I have the following structure:

PUT /x
PUT /x/x/_mapping
{
  "properties": {
    "x":{
      "type": "integer"
    }
  }
}

PUT /x/x/1
{
  "x": 1
}

PUT /x/x/2
{
  "x": 2
}

PUT /x/x/3
{
  "x": 3
}

I could do:

GET /x/_search
{
  "size": 0,
  "aggs": {
    "f1": {
      "filter": {
        "range": {
          "x": {
            "gt": 2
          }
        }
      },
      "aggs": {
        "max": {
          "max": {
            "field": "x"
          }
        }
      }
    },
    "f2": {
      "filter": {
        "range": {
          "x": {
            "lt": 2
          }
        }
      },
      "aggs": {
        "max": {
          "max": {
            "field": "x"
          }
        }
      }
    }
  }
}

Or two different searches:

GET /x/_search
{
  "query": {
    "range": {
      "x": {
        "gt": 2
      }
    }
  }, 
  "size": 0,
  "aggs": {
    "max": {
      "max": {
        "field": "x"
      }
    }
  }
}

GET /x/_search
{
  "query": {
    "range": {
      "x": {
        "lt": 2
      }
    }
  }, 
  "size": 0,
  "aggs": {
    "max": {
      "max": {
        "field": "x"
      }
    }
  }
}

Considering a large data universe and complex filters, which one would perform better?

A faster query would be, using the 1st one, but changing "gt:2" and "lt:2" to `must_not: {term:2}}" in the body.

Makes sense ? So "x!=2" is a faster filter compared to 2 range filters.

Try that.

The "< 2" and "> 2" filters were only an example. My actual filters will be way more complex.
Can I ask how did you infer the first query would be more efficient (remember that I'm talking about large databases with complex filters)?

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