Skip to content

Instantly share code, notes, and snippets.

@mchruszcz
Created October 20, 2011 11:15
Show Gist options
  • Save mchruszcz/1300910 to your computer and use it in GitHub Desktop.
Save mchruszcz/1300910 to your computer and use it in GitHub Desktop.
Elastic Search field selection and arrays
# Create the first document, create a mapping
$ curl -s -X POST http://localhost:9200/test/cat -d '
{
"title": "Kittens",
"images": [
{
"caption": "O HAI"
},
{
"caption": "I can has cheezburgr"
}
]
}'
# The new mapping
$ curl -s 'http://localhost:9200/test/cat/_mapping?pretty=1'
{
"cat" : {
"properties" : {
"title" : {
"type" : "string"
},
"images" : {
"dynamic" : "true",
"properties" : {
"caption" : {
"type" : "string"
}
}
}
}
}
}
# Query without fields specified
$ curl -s 'http://localhost:9200/test/_search?pretty=1'
...
"_source" :
{
"title": "Kittens",
"images": [
{
"caption": "O HAI"
},
{
"caption": "I can has cheezburgr"
}
]
}
# Query with fields specified
$ curl -s 'http://localhost:9200/test/_search?pretty=1' -d '
{
"fields": ["title", "images"]
}'
...
"fields" : {
"title" : "Kittens"
}
# Query with fields specified using '_source'
$ curl -s 'http://localhost:9200/test/_search?pretty=1' -d '
{
"fields": ["title", "_source.images"]
}'
...
"fields" : {
"title" : "Kittens",
"_source.images" : [ {
"caption" : "O HAI"
}, {
"caption" : "I can has cheezburgr"
} ]
}
# There is no way to select fields within images. None of these
# works:
# * "fields": ["title", "caption"]
# * "fields": ["title", "images.caption"]
# * "fields": ["title", "_source.images.caption"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment