Skip to content

Instantly share code, notes, and snippets.

@tugberkugurlu
Last active August 29, 2015 14:24
Show Gist options
  • Save tugberkugurlu/08e51fd91b3b3033c1a4 to your computer and use it in GitHub Desktop.
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
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"
}
}
}
}
}
}
}
{
"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