Skip to content

Instantly share code, notes, and snippets.

@nickhoffman
Created November 8, 2011 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickhoffman/0b62966c116fd6eb6212 to your computer and use it in GitHub Desktop.
Save nickhoffman/0b62966c116fd6eb6212 to your computer and use it in GitHub Desktop.
Nested Query on arbitrary fields
# The documents that're index all have an arbitrary nested field, named "message", "post", and "content", respectively.
# Can a nested query be built to search all of the nested fields without specifically referencing each nested field?
curl -s -XDELETE localhost:9200/test 2>&1 >/dev/null
curl -s -XPUT localhost:9200/test >/dev/null
curl -XPUT localhost:9200/test/tweet/_mapping -d '
{
"tweet" : {
"properties" : {
"name" : {
"type" : "string"
},
"comments" : {
"properties" : {
"message" : {
"type" : "string"
},
"username" : {
"type" : "string"
}
},
"type" : "nested"
}
}
}
}
'
echo
curl -XPUT localhost:9200/test/tweet/1 -d '{
"name" : "Jane",
"comments" : [ {"message" : "this is text", "username" : "Jane"} ]
}'
echo
curl -XPUT localhost:9200/test/tweet/2 -d '{
"name" : "Jack",
"comments" : [ {"post" : "this is text", "username" : "Jack"} ]
}'
echo
curl -XPUT localhost:9200/test/tweet/3 -d '{
"name" : "Jack",
"comments" : [ {"content" : "stuff here", "username" : "Jack"} ]
}'
echo
curl localhost:9200/test/_search?pretty=true -d '
{
"query": {
"nested": {
"path": "comments",
"query": {
"bool": {
"must": [ { "text": { "comments.text": "this" } } ]
}
}
}
}
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment