Skip to content

Instantly share code, notes, and snippets.

@iamedu
Created March 5, 2015 17:58
Show Gist options
  • Save iamedu/c8d263d3e27c8e1c1fce to your computer and use it in GitHub Desktop.
Save iamedu/c8d263d3e27c8e1c1fce to your computer and use it in GitHub Desktop.
Elasticsearch with grandparents and thingies
#Based on http://techhari.blogspot.co.at/2013/05/elasticseach-routing-for-parent-child.html
#Create a new parentchild index
curl -XDELETE 'http://localhost:9200/parentchild' && echo
curl -XPUT 'http://localhost:9200/parentchild' -d '{}' && echo
#Create mapping without routing
curl -XPUT localhost:9200/parentchild/attrvalues/_mapping -d '{
"attrvalues": {
"_parent": {
"type": "attributes"
},
"_routing": {
"required": true,
"path": "catentry_id"
},
"properties": {
"attrvalue_id":
{"type": "string", "index": "not_analyzed"},
"identifier": {"type": "string", "index": "not_analyzed"},
"valusage": {"type": "string", "index": "not_analyzed"},
"stringvalue": {"type": "string", "index": "not_analyzed"},
"integervalue": {"type": "string", "index": "not_analyzed"},
"floatvalue": {"type": "string", "index": "not_analyzed"},
"product_id": {"type": "string", "index": "not_analyzed"},
"catentry_id": {"type": "string", "index": "not_analyzed"}
}
}
}' && echo
curl -XPUT localhost:9200/parentchild/attributes/_mapping -d '{
"attributes": {
"_parent": {
"type": "product"
},
"_routing": {
"required": true,
"path": "catentry_id"
},
"properties": {
"attribute_id": {"type": "string", "index": "not_analyzed"},
"identifier": {"type": "string", "index": "not_analyzed"},
"attrtype_id": {"type": "string", "index": "not_analyzed"},
"displayable": {"type": "string", "index": "not_analyzed"},
"comparable": {"type": "string", "index": "not_analyzed"},
"name": {"type": "string", "index": "not_analyzed"},
"groupname": {"type": "string", "index": "not_analyzed"},
"usage": {"type": "string", "index": "not_analyzed"},
"product_id": {"type": "string", "index": "not_analyzed"}
}
}
}' && echo
curl -XPUT localhost:9200/parentchild/product/_mapping -d '{
"product": {
"properties": {
"product_id": {"type": "string", "index": "not_analyzed"},
"partnumber": {"type": "string", "index": "not_analyzed"},
"mfpartnumber": {"type": "string", "index": "not_analyzed"},
"catalog_id": {"type": "string", "index": "not_analyzed"}
}
}
}' && echo
curl -s -XPOST localhost:9200/_bulk?pretty=true --data-binary '
{ "index" : { "_index" : "parentchild", "_type" : "product", "_id" : "EI01Y"} }
{"product_id": "EI01Y","partnumber":"EI-01Y-01","mfpartnumber":"EI-01Y-01","catalog_id":["10001","10002"]}
{ "index" : { "_index" : "parentchild", "_type" : "attributes", "_id" : "0001", "parent" : "EI01Y", "routing" : "EI01Y" } }
{"attribute_id": "0001","identifier":"0001","displayable":"true","usage":"1","catentry_id" : "EI01Y"}
{ "index" : { "_index" : "parentchild", "_type" : "attributes", "_id" : "0002", "parent" : "EI01Y", "routing" : "EI01Y" } }
{"attribute_id": "0002","identifier":"0002","displayable":"true","usage":"1","catentry_id" : "EI01Y"}
{ "index" : { "_index" : "parentchild", "_type" : "attrvalues", "_id" : "0001-01", "parent" : "0001", "routing" : "EI01Y" } }
{ "identifier": "0001-01","stringvalue":"test","integervalue":"test","floatvalue":"test","catentry_id" : "EI01Y"}
{ "index" : { "_index" : "parentchild", "_type" : "attrvalues", "_id" : "0001-02", "parent" : "0001", "routing" : "EI01Y" } }
{ "identifier": "0001-02","stringvalue":"test","integervalue":"test","floatvalue":"test","catentry_id" : "EI01Y"}
{ "index" : { "_index" : "parentchild", "_type" : "attrvalues", "_id" : "0002-01", "parent" : "0002", "routing" : "EI01Y" } }
{ "identifier": "0002-01","stringvalue":"test","integervalue":"test","floatvalue":"test","catentry_id" : "EI01Y"}
{ "index" : { "_index" : "parentchild", "_type" : "attrvalues", "_id" : "0002-02", "parent" : "0002", "routing" : "EI01Y" } }
{ "identifier": "0002-02","stringvalue":"test","integervalue":"test","floatvalue":"test","catentry_id" : "EI01Y"}
'
# This works
curl -XPOST 'http://localhost:9200/parentchild/_search?pretty=true' -d '{
"query": {
"filtered": {
"query": {
"bool": {
"must": [{"term": {"product_id" :"EI01Y" }}]
}
},
"filter": {
"has_child": {
"type": "attributes",
"query": {
"filtered": {
"query": {
"bool": {
"must": [{"term": {"attribute_id": "0001"}}]
}
}
}
}
}
}
}
}
}'
# This works, we are not able to join multiple level child documents as we have included _routing
# and they reside within the same shard now.
curl -XPOST 'http://localhost:9200/parentchild/_search?pretty=true' -d '{
"query": {
"filtered": {
"query": {
"bool": {
"must": [{"term": {"product_id" :"EI01Y" }}]
}
},
"filter": {
"has_child": {
"type": "attributes",
"query": {
"filtered": {
"query": {
"bool": {
"must": [{"term": {"attribute_id": "0001"}}]
}
},
"filter": {
"has_child": {
"type": "attrvalues",
"query": {
"term": {
"stringvalue":"test"
}
}
}
}
}
}
}
}
}
}
}'
curl -XPOST 'http://localhost:9200/parentchild/_search/?pretty=true' -d '{
"query": {
"has_child": {
"query": {
"term": {
"stringvalue": "test"
}
},
"type": "attrvalues"
}
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment