-
-
Save nickhoffman/0b62966c116fd6eb6212 to your computer and use it in GitHub Desktop.
Nested Query on arbitrary fields
This file contains 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
# 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