Skip to content

Instantly share code, notes, and snippets.

@michaeldcruz
Created April 22, 2013 16:03
Show Gist options
  • Save michaeldcruz/5436325 to your computer and use it in GitHub Desktop.
Save michaeldcruz/5436325 to your computer and use it in GitHub Desktop.
Elasticsearch parent/child relationship example
# Bulk Sample Data
curl -s -XPOST localhost:9200/_bulk?pretty=true --data-binary '
{ "index" : { "_index" : "products", "_type" : "style_variant", "_id" : "1" } }
{ "name" : "Style Variant 1" }
{ "index" : { "_index" : "products", "_type" : "style_variant", "_id" : "2" } }
{ "name" : "Style Variant 2" }
{ "index" : { "_index" : "products", "_type" : "style_variant", "_id" : "3" } }
{ "name" : "Style Variant 3" }
{ "index" : { "_index" : "products", "_type" : "product", "_id" : "1", "parent" : "1" } }
{ "name" : "Shirt 1", "vendor_name" : "Alternative Apparel", "size" : "S", "retail_price": 100, "tc_color" : "Black" }
{ "index" : { "_index" : "products", "_type" : "product", "_id" : "2", "parent" : "2" } }
{ "name" : "Shirt 2", "vendor_name" : "Alternative Apparel", "size" : "M", "retail_price": 120, "tc_color" : "Red" }
{ "index" : { "_index" : "products", "_type" : "product", "_id" : "3", "parent" : "2" } }
{ "name" : "Pants 1", "vendor_name" : "J Crew", "size" : "32", "retail_price": 280, "tc_color" : "Black" }
{ "index" : { "_index" : "products", "_type" : "product", "_id" : "4", "parent" : "3" } }
{ "name" : "Pants 2", "vendor_name" : "J Crew", "size" : "34", "retail_price": 220, "tc_color" : "Blue" }
{ "index" : { "_index" : "products", "_type" : "product", "_id" : "5", "parent" : "3" } }
{ "name" : "Shoes 1", "vendor_name" : "J Crew", "size" : "12", "retail_price": 250, "tc_color" : "Brown" }
'
curl -XPOST 'http://localhost:9200/products/style_variant/_search?pretty=true' -d '{
"query": {
"has_child": {
"type" : "product",
"query": {
"filtered": {
"query":= { "match_all": { }},
"filter": {
"and": [
{"terms": {"size": ["12", "34"]}},
{"term": {"tc_color": "Blue"}}
]
}
}
}
}
}
}'
curl -XDELETE 'http://localhost:9200/products/'
curl -XPOST 'http://localhost:9200/products' -d '{
"mappings": {
"style_variant": {
"properties": {
"name": {
"type": "string"
}
}
},
"product": {
"_parent": {
"type": "style_variant"
},
"properties": {
"name": { "type": "string" },
"vendor_name": { "type" : "string", "index" : "not_analyzed" },
"size": { "type" : "string" },
"retail_price": { "type" : "float" },
"tc_color": { "type" : "string", "index" : "not_analyzed" }
}
}
}
}
}'
@joelash
Copy link

joelash commented Apr 24, 2013

Is line #7 is the has_child_query.json as typo or is there really an '=' after the colon?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment