Hm, it should be same/similar ... under the covers they are executing almost exactly the same query.
Did you run the test multiple times for both queries? I'm not saying there isn't an effect there, but the time difference is small enough it could simply be random chance (e.g. noise) and not a statistically significant effect.
Ideally:
- Run the
filtered
query a few dozen times to warm up the OS cache and JVM
- Run it another 20 times and record all values
- Shut the server down. Restart
- Run
bool
a few dozen times to warm up OS cache and JVM
- Run it another 20 times and record all values
Then compare the values (preferably with a T-Test) to see if there is a real difference. It's not fun, but benchmarking is so easily skewed by the smallest things...you really have to take a statistical population approach.
With that said, I checked what Lucene queries are being run. The filtered
is doing this in 2.0:
GET /test/_validate/query?explain=true
{
"query": {
"filtered": {
"filter": {
"term": {
"last_name": "brown"
}
}
}
}
}
{
"valid": true,
"_shards": {...},
"explanations": [
{
"index": "test",
"valid": true,
"explanation": "+*:* #last_name:brown"
}
]
}
Whereas the bool
is doing this:
GET /test/_validate/query?explain=true
{
"query": {
"bool": {
"filter": [
{
"term": {
"last_name": "brown"
}
}
]
}
}
}
{
"valid": true,
"_shards": {...},
"explanations": [
{
"index": "test",
"valid": true,
"explanation": "#last_name:brown"
}
]
}
Which is slightly different (the filtered
is including a match_all query in there). If you want to try, you could try adding a match_all to the bool and see if it improves. I'd be surprised if it does, but perhaps there is an optimization that's not being used otherwise?
GET /test/_validate/query?explain=true
{
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"filter": [
{
"term": {
"last_name": "brown"
}
}
]
}
}
}
However...I'll stress my first point, it's likely just noise. Make sure you run these a number of times to even out noise, try to isolate any other processes from touching the cluster, etc.