Last active
August 29, 2015 14:24
-
-
Save tugberkugurlu/08e51fd91b3b3033c1a4 to your computer and use it in GitHub Desktop.
Elasticsearch nested term query, find a matching item inside a nested type collection for a document
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
PUT users | |
PUT users/_mapping/group | |
{ | |
"group" : { | |
"properties" : { | |
"users" : { | |
"type" : "nested", | |
"properties": { | |
"id" : {"type": "string", "index": "not_analyzed" }, | |
"first" : {"type": "string" }, | |
"last" : {"type": "string" }, | |
"tags" : { "type": "string", "index": "not_analyzed" } | |
} | |
} | |
} | |
} | |
} | |
POST users/group | |
{ | |
"users": [ | |
{ | |
"id": "tu", | |
"first": "Tugberk", | |
"last": "Ugurlu", | |
"tags": [ | |
"linux", "aspnet", "dotnet" | |
] | |
} | |
] | |
} | |
POST users/group | |
{ | |
"users": [ | |
{ | |
"id": "sg", | |
"first": "Scott", | |
"last": "Guthrie", | |
"tags": ["azure", "aspnet", "silverlight"] | |
} | |
] | |
} | |
POST users/group | |
{ | |
"users": [ | |
{ | |
"id": "sg", | |
"first": "Scott", | |
"last": "Guthrie", | |
"tags": ["azure", "aspnet", "silverlight"] | |
}, | |
{ | |
"id": "jd", | |
"first": "John", | |
"last": "Doe", | |
"tags": ["perl", "ruby", "PHP"] | |
} | |
] | |
} | |
GET users/group/_search | |
{ | |
"query": { | |
"nested": { | |
"path": "users", | |
"query": { | |
"filtered": { | |
"query": { | |
"match_all": {} | |
}, | |
"filter": { | |
"term": { | |
"id": "sg" | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
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
{ | |
"took": 2, | |
"timed_out": false, | |
"_shards": { | |
"total": 5, | |
"successful": 5, | |
"failed": 0 | |
}, | |
"hits": { | |
"total": 2, | |
"max_score": 1, | |
"hits": [ | |
{ | |
"_index": "users", | |
"_type": "group", | |
"_id": "AU6OwpnXidzUtyfB9MUe", | |
"_score": 1, | |
"_source": { | |
"users": [ | |
{ | |
"id": "sg", | |
"first": "Scott", | |
"last": "Guthrie", | |
"tags": [ | |
"azure", | |
"aspnet", | |
"silverlight" | |
] | |
} | |
] | |
} | |
}, | |
{ | |
"_index": "users", | |
"_type": "group", | |
"_id": "AU6OytpmidzUtyfB9Mad", | |
"_score": 1, | |
"_source": { | |
"users": [ | |
{ | |
"id": "sg", | |
"first": "Scott", | |
"last": "Guthrie", | |
"tags": [ | |
"azure", | |
"aspnet", | |
"silverlight" | |
] | |
}, | |
{ | |
"id": "jd", | |
"first": "John", | |
"last": "Doe", | |
"tags": [ | |
"perl", | |
"ruby", | |
"PHP" | |
] | |
} | |
] | |
} | |
} | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment