-
-
Save markharwood/2ebe657115835bc0483aad7deb7489f9 to your computer and use it in GitHub Desktop.
Changing Boolean logic in elasticsearch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 7.2 correctly (in my view) assumes should is always optional if a "must" is provided. | |
# In 6.x this logic varied depending on how the bool was wrapped - examples below | |
DELETE test | |
POST test/_doc/1 | |
{ | |
"field1":"car", | |
"field2":"red" | |
} | |
POST test/_doc/2 | |
{ | |
"field1":"car", | |
"field2":"blue" | |
} | |
POST test/_doc/3 | |
{ | |
"field1":"bus", | |
"field2":"red" | |
} | |
# should clause is optional in 6.x and 7.2 | |
GET test/_search | |
{ | |
"query": { | |
"bool": { | |
"must": [ | |
{ | |
"term": { | |
"field1": "car" | |
} | |
} | |
], | |
"should": [ | |
{ | |
"term": { | |
"field2": "red" | |
} | |
} | |
] | |
} | |
} | |
} | |
# should clause is mandatory in 6.x and optional in 7.2 | |
GET test/_search | |
{ | |
"query": { | |
"constant_score": { | |
"filter": { | |
"bool": { | |
"must": [ | |
{ | |
"term": { | |
"field1": "car" | |
} | |
} | |
], | |
"should": [ | |
{ | |
"term": { | |
"field2": "red" | |
} | |
} | |
] | |
} | |
} | |
} | |
} | |
} | |
# should clause is mandatory in 6.x and optional in 7.2 | |
GET test/_search | |
{ | |
"query": { | |
"bool": { | |
"filter": { | |
"bool": { | |
"must": [ | |
{ | |
"term": { | |
"field1": "car" | |
} | |
} | |
], | |
"should": [ | |
{ | |
"term": { | |
"field2": "red" | |
} | |
} | |
] | |
} | |
} | |
} | |
} | |
} | |
# should clause is optional in 6.x and 7.2 | |
GET test/_search | |
{ | |
"post_filter": { | |
"bool": { | |
"must": [ | |
{ | |
"term": { | |
"field1": "car" | |
} | |
} | |
], | |
"should": [ | |
{ | |
"term": { | |
"field2": "red" | |
} | |
} | |
] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment