Skip to content

Instantly share code, notes, and snippets.

@erlingwl
Last active December 24, 2015 09:49
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 erlingwl/6779401 to your computer and use it in GitHub Desktop.
Save erlingwl/6779401 to your computer and use it in GitHub Desktop.
# Remove old data
curl -XDELETE "http://localhost:9200/grandissue"
# Create index with defaults
curl -XPOST "http://localhost:9200/grandissue" -d '{
"settings": {
"index": {
"number_of_shards": 4,
"number_of_replicas": 1
}
}
}'
# Add GrandParent mapping
curl -XPOST "http://localhost:9200/grandissue/grandparent/_mapping" -d '{
"grandparent":{
"properties":{
"name":{
"type":"string",
"index":"analyzed",
"analyzer":"standard"
}
}
}
}'
# Add Parent mapping
curl -XPOST "http://localhost:9200/grandissue/parent/_mapping" -d '{
"parent":{
"_parent":{
"type":"grandparent"
},
"properties":{
"name":{
"type":"string",
"index":"analyzed",
"analyzer":"standard"
}
}
}
}'
# Add Child mapping
curl -XPOST "http://localhost:9200/grandissue/child_type_one/_mapping" -d '{
"child_type_one":{
"_parent":{
"type":"parent"
},
"properties":{
"name":{
"type":"string",
"index":"analyzed",
"analyzer":"standard"
}
}
}
}'
# Add Child mapping
curl -XPOST "http://localhost:9200/grandissue/child_type_two/_mapping" -d '{
"child_type_two":{
"_parent":{
"type":"parent"
},
"properties":{
"name":{
"type":"string",
"index":"analyzed",
"analyzer":"standard"
}
}
}
}'
# Add a grandparent document
curl -XPOST "http://localhost:9200/grandissue/grandparent/1" -d '{
"name" : "Grandpa"
}'
# Add a parent document
curl -XPOST "http://localhost:9200/grandissue/parent/2?parent=1&routing=1" -d '{
"name" : "Dad"
}'
# Add a child document of type child_type_one
curl -XPOST "http://localhost:9200/grandissue/child_type_one/3?parent=2&routing=1" -d '{
"name" : "William"
}'
# Add a child document of type child_type_two
curl -XPOST "http://localhost:9200/grandissue/child_type_two/4?parent=2&routing=1" -d '{
"name" : "Kate"
}'
# Search for child type one with name William, should return Grandpa
curl -XGET 'http://localhost:9200/grandissue/grandparent/_search?pretty=true' -d '{
"query" : {
"bool": {
"must": [
{
"has_child" : {
"type" : "parent",
"query" : {
"bool": {
"must" : [
{
"has_child": {
type: "child_type_one",
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "name:William*"
}
}
]
}
}
}
}
]
}
}
}
}
]
}
}
}'
# Search for child type two with name William, should NOT return Grandpa, but it still does..
curl -XGET 'http://localhost:9200/grandissue/grandparent/_search?pretty=true' -d '{
"query" : {
"bool": {
"must": [
{
"has_child" : {
"type" : "parent",
"query" : {
"bool": {
"must" : [
{
"has_child": {
type: "child_type_two",
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "name:William*"
}
}
]
}
}
}
}
]
}
}
}
}
]
}
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment