Skip to content

Instantly share code, notes, and snippets.

@markharwood
Last active August 7, 2019 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markharwood/2ebe657115835bc0483aad7deb7489f9 to your computer and use it in GitHub Desktop.
Save markharwood/2ebe657115835bc0483aad7deb7489f9 to your computer and use it in GitHub Desktop.
Changing Boolean logic in elasticsearch
# 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