Last active
June 19, 2017 06:31
-
-
Save darklow/7964005 to your computer and use it in GitHub Desktop.
New aggregation framework - filtering by nested object fields
This file contains hidden or 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
#!/bin/bash | |
# ======================================== | |
curl -X DELETE localhost:9200/movies | |
curl -X PUT localhost:9200/movies -d ' | |
{ | |
"mappings": { | |
"movie": { | |
"properties": { | |
"name": { "type": "string" }, | |
"genre": { "type": "string", "index": "not_analyzed" }, | |
"credits": { | |
"type": "nested", | |
"include_in_parent": true, | |
"properties": { | |
"person_id": { "type": "integer" }, | |
"department": { "type": "string", "index": "not_analyzed" } | |
} | |
} | |
} | |
} | |
} | |
}' | |
curl -X PUT localhost:9200/movies/movie/1 -d ' | |
{ | |
"name": "Movie A", | |
"genre": "comedy", | |
"credits": [ | |
{ | |
"person_id": 1, | |
"department": "director" | |
}, | |
{ | |
"person_id": 2, | |
"department": "actor" | |
}, | |
{ | |
"person_id": 3, | |
"department": "producer" | |
} | |
] | |
}' | |
curl -X PUT localhost:9200/movies/movie/2 -d ' | |
{ | |
"name": "Movie B", | |
"genre": "horror", | |
"credits": [ | |
{ | |
"person_id": 1, | |
"department": "director" | |
}, | |
{ | |
"person_id": 2, | |
"department": "actor" | |
}, | |
{ | |
"person_id": 3, | |
"department": "producer" | |
} | |
] | |
}' | |
curl -X PUT localhost:9200/movies/movie/3 -d ' | |
{ | |
"name": "Movie C", | |
"genre": "comedy", | |
"credits": [ | |
{ | |
"person_id": 3, | |
"department": "director" | |
} | |
] | |
}' | |
curl -X POST "http://localhost:9200/movies/_refresh" | |
curl -X POST "http://localhost:9200/movies/_search?pretty=true" -d ' | |
{ | |
"query": { | |
"nested": { | |
"path": "credits", | |
"query": { | |
"match": { | |
"credits.person_id": 1 | |
} | |
} | |
} | |
}, | |
"aggs": { | |
"departments": { | |
"terms": { | |
"field": "credits.department" | |
}, | |
"aggs": { | |
"nested": { | |
"path": "credits" | |
}, | |
"filtered": { | |
"terms": "credits.department", | |
"filter": { | |
"nested": { | |
"path": "credits", | |
"filter": { | |
"term": { | |
"credits.person_id": 1 | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
}, | |
"size": 0 | |
}' | |
echo | |
echo "# Returned doc_count=2 for every department, but should return only for department "director", where person_id=1"; | |
echo | |
# Result: | |
# "aggregations": { | |
# "departments": { | |
# "buckets": [ | |
# { | |
# "key": "producer", | |
# "doc_count": 2, | |
# "filtered": { | |
# "doc_count": 2 | |
# } | |
# }, | |
# { | |
# "key": "director", | |
# "doc_count": 2, | |
# "filtered": { | |
# "doc_count": 2 | |
# } | |
# }, | |
# { | |
# "key": "actor", | |
# "doc_count": 2, | |
# "filtered": { | |
# "doc_count": 2 | |
# } | |
# } | |
# ] | |
# } | |
# } | |
# Should be: | |
# "aggregations": { | |
# "departments": { | |
# "buckets": [ | |
# { | |
# "key": "producer", | |
# "doc_count": 2, | |
# "filtered": { | |
# "doc_count": 0 | |
# } | |
# }, | |
# { | |
# "key": "director", | |
# "doc_count": 2, | |
# "filtered": { | |
# "doc_count": 0 | |
# } | |
# }, | |
# { | |
# "key": "actor", | |
# "doc_count": 2, | |
# "filtered": { | |
# "doc_count": 0 | |
# } | |
# } | |
# ] | |
# } | |
# } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment