Last active
August 29, 2015 14:16
-
-
Save bertvan/a0b88edb3c03a9e6cf83 to your computer and use it in GitHub Desktop.
Using "not filter" and "match_all" filter on nested objects
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
curl -XDELETE "http://localhost:9200/test" | |
curl -XPOST "http://localhost:9200/test" -d' | |
{ | |
"mappings": | |
{ | |
"movie": { | |
"properties": { | |
"title": { "type" : "string" }, | |
"actors": { | |
"type": "nested", | |
"properties": { | |
"id": { "type": "double" } | |
} | |
} | |
} | |
} | |
} | |
}' | |
curl -XPUT "http://localhost:9200/test/movie/1" -d' | |
{ | |
"title": "Mr. Holmes", | |
"actors": [ | |
{ | |
"id": 1 | |
}] | |
}' | |
curl -XPUT "http://localhost:9200/test/movie/2" -d' | |
{ | |
"title": "The lady in the van", | |
"actors": [ | |
{ | |
"id": 2 | |
}] | |
}' | |
curl -XPUT "http://localhost:9200/test/movie/3" -d' | |
{ | |
"title": "Ex machina", | |
"actors": [] | |
}' | |
# looking for actor.id == 1 | |
# works as expected | |
curl -XGET "http://localhost:9200/test/_search?pretty=true" -d' | |
{ | |
"filter": { | |
"nested": { | |
"path": "actors", | |
"filter": { | |
"term": { | |
"actors.id": "1" | |
} | |
} | |
} | |
} | |
}' | |
# looking for actor.id != 1 | |
# works as expected, note "nested filter" is around the "not filter" | |
curl -XGET "http://localhost:9200/test/_search?pretty=true" -d' | |
{ | |
"filter": { | |
"nested": { | |
"path": "actors", | |
"filter": { | |
"not": { | |
"filter": { | |
"term": { | |
"actors.id": "1" | |
} | |
} | |
} | |
} | |
} | |
} | |
}' | |
# looking for non-empty values | |
# works as expected | |
curl -XGET "http://localhost:9200/test/_search?pretty=true" -d' | |
{ | |
"filter": { | |
"nested": { | |
"path": "actors", | |
"filter": { | |
"match_all": {} | |
} | |
} | |
} | |
}' | |
# looking for empty values (approach 1) | |
# works as expected, however note that here, the "not filter" is around the "nested filter", which is the other way around as above. | |
# I would like to avoid this and use the lower approach. | |
# is a workaround as in: https://github.com/elasticsearch/elasticsearch/issues/3495 | |
curl -XGET "http://localhost:9200/test/_search?pretty=true" -d' | |
{ | |
"filter": { | |
"not": { | |
"filter": { | |
"nested": { | |
"path": "actors", | |
"filter": { | |
"match_all": {} | |
} | |
} | |
} | |
} | |
} | |
}' | |
# DOES NOT WORK AS EXPECTED (approach 2) | |
# Expecting similar behavior as above, so to find the record with 0 actors | |
curl -XGET "http://localhost:9200/test/_search?pretty=true" -d' | |
{ | |
"filter": { | |
"nested": { | |
"path": "actors", | |
"filter": { | |
"not": { | |
"filter": { | |
"match_all": {} | |
} | |
} | |
} | |
} | |
} | |
}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment