Created
March 5, 2015 20:57
-
-
Save bertvan/27318e9430202fa86210 to your computer and use it in GitHub Desktop.
Using "not filter" and "match_all" filter on nested objects (SENSE)
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
DELETE /test | |
POST /test | |
{ | |
"mappings": | |
{ | |
"movie": { | |
"properties": { | |
"title": { "type" : "string" }, | |
"actors": { | |
"type": "nested", | |
"properties": { | |
"id": { "type": "double" } | |
} | |
} | |
} | |
} | |
} | |
} | |
PUT /test/movie/1 | |
{ | |
"title": "Mr. Holmes", | |
"actors": [ | |
{ | |
"id": 1 | |
}] | |
} | |
PUT /test/movie/2 | |
{ | |
"title": "The lady in the van", | |
"actors": [ | |
{ | |
"id": 2 | |
}] | |
} | |
PUT /test/movie/3 | |
{ | |
"title": "Ex machina", | |
"actors": [] | |
} | |
# looking for actor.id == 1 | |
# works as expected | |
GET test/_search | |
{ | |
"filter": { | |
"nested": { | |
"path": "actors", | |
"filter": { | |
"term": { | |
"id": "1" | |
} | |
} | |
} | |
} | |
} | |
# looking for actor.id != 1 | |
# works as expected, note "nested filter" is around the "not filter" | |
GET test/_search | |
{ | |
"filter": { | |
"nested": { | |
"path": "actors", | |
"filter": { | |
"not": { | |
"filter": { | |
"term": { | |
"id": "1" | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
# looking for non-empty values | |
# works as expected | |
GET test/_search | |
{ | |
"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 | |
GET test/_search | |
{ | |
"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 | |
GET test/_search | |
{ | |
"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