Skip to content

Instantly share code, notes, and snippets.

@jbong
Last active December 18, 2015 01:39
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 jbong/5705764 to your computer and use it in GitHub Desktop.
Save jbong/5705764 to your computer and use it in GitHub Desktop.
elasticsearch grandparent issue 0.90.1
# 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":"default"
}
}
}
}'
# Add Parent mapping
curl -XPOST "http://localhost:9200/grandissue/parent/_mapping" -d '{
"parent":{
"_parent":{
"type":"grandparent"
},
"properties":{
"name":{
"type":"string",
"index":"analyzed",
"analyzer":"default"
}
}
}
}'
# Add Child mapping
curl -XPOST "http://localhost:9200/grandissue/child/_mapping" -d '{
"child":{
"_parent":{
"type":"parent"
},
"properties":{
"name":{
"type":"string",
"index":"analyzed",
"analyzer":"default"
}
}
}
}'
# Add a grandparent document
curl -XPOST "http://localhost:9200/grandissue/grandparent/1" -d '{
"name" : "Bob"
}'
# Test Search for grandparent - should return document
curl -XGET 'http://localhost:9200/grandissue/grandparent/_search?pretty=true' -d '{
"query" : {
"match" : {
"name" : "bob"
}
}
}'
# Add a parent document
curl -XPOST "http://localhost:9200/grandissue/parent/2?parent=1&routing=1" -d '{
"name" : "Jim"
}'
# Test Search for parent (include grandparent id & type in script fields) - should return document
curl -XGET 'http://localhost:9200/grandissue/parent/_search?pretty=true' -d '{
"query" : {
"match" : {
"name" : "jim"
}
},
"script_fields": {
"parent_id_in_script": {
"script": "org.elasticsearch.index.mapper.Uid.idFromUid(doc[\"_parent\"].value)"
},
"parent_type_in_script": {
"script": "org.elasticsearch.index.mapper.Uid.typeFromUid(doc[\"_parent\"].value)"
}
}
}'
# Test Search parent that has a grand parent. This will not return any results if the following 2 conditions are TRUE:
# 1. elasticsearch version 0.90.1
# 2. child mapping (effectively a grandchild in this example) declared above has been submitted to es.
#
# If the child mapping declared above has been omitted from the index then this query will work as expected.
#
# NOTE: ES version 0.90.0 correctly still returns a document.
curl -XGET 'http://localhost:9200/grandissue/parent/_search?pretty=true' -d '{
"query" : {
"has_parent" : {
"parent_type" : "grandparent",
"query" : {
"match_all" : {}
}
}
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment