Last active
December 17, 2015 11:29
-
-
Save hariinfo/5602824 to your computer and use it in GitHub Desktop.
Product model using ES Parent-child
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 model is as follows | |
# catentry identifies a SKU, it can have one to many attributes, each attribute can have one to many attribute values | |
#Delete the index if one exists | |
curl -XDELETE 'http://localhost:9200/products' && echo | |
#Create a new products index | |
curl -XPUT 'http://localhost:9200/products' -d '{}' && echo | |
curl -XPUT localhost:9200/products/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"}, | |
"catentry_id": {"type": "string", "index": "not_analyzed"} | |
} | |
} | |
}' && echo | |
curl -XPUT localhost:9200/products/attributes/_mapping -d '{ | |
"attributes": { | |
"_parent": { | |
"type": "catentry" | |
}, | |
"_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"}, | |
"catentry_id": {"type": "string", "index": "not_analyzed"} | |
} | |
} | |
}' && echo | |
curl -XPUT localhost:9200/products/catentry/_mapping -d '{ | |
"catentry": { | |
"properties": { | |
"catentry_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 | |
#Load some test data | |
curl -s -XPOST localhost:9200/_bulk?pretty=true --data-binary ' | |
{ "index" : { "_index" : "products", "_type" : "attributes", "_id" : "0001", "parent" : "EI01Y", "routing" : "EI01Y" } } | |
{"attribute_id": "0001","identifier":"0001","displayable":"true","usage":"1","catentry_id" : "EI01Y"} | |
{ "index" : { "_index" : "products", "_type" : "attributes", "_id" : "0002", "parent" : "EI01Y", "routing" : "EI01Y" } } | |
{"attribute_id": "0002","identifier":"0002","displayable":"true","usage":"1","catentry_id" : "EI01Y"} | |
{ "index" : { "_index" : "products", "_type" : "attrvalues", "_id" : "0001-01", "parent" : "0001", "routing" : "EI01Y" } } | |
{ "identifier": "0001-01","stringvalue":"test","integervalue":"test","floatvalue":"test","catentry_id" : "EI01Y"} | |
{ "index" : { "_index" : "products", "_type" : "attrvalues", "_id" : "0001-02", "parent" : "0001", "routing" : "EI01Y" } } | |
{ "identifier": "0001-02","stringvalue":"test","integervalue":"test","floatvalue":"test","catentry_id" : "EI01Y"}' | |
curl -s -XPOST localhost:9200/_bulk?pretty=true --data-binary ' | |
{ "index" : { "_index" : "products", "_type" : "product", "_id" : "EI01Y" } } | |
{"catentry_id": "0001","identifier":"0001","displayable":"true","usage":"1","catentry_id" : "EI01Y"} | |
curl -XPOST localhost:9200/products/catentry/EI01Y?pretty=true -d '{"catentry_id":"EI01Y", "partnumber":"123-456-789"}' | |
curl -XPOST 'localhost:9200/products/_search?pretty' -d ' | |
{ | |
"query": { | |
"match_all" : {} | |
} | |
}' | |
curl -XGET 'http://localhost:9200/products/_search?pretty=true' -d ' | |
{ | |
"query" : { | |
"filtered" : { | |
"query" : { | |
"match_all" : { } | |
}, | |
"filter" : { | |
"has_child" : { | |
"query" : { | |
"match_all" : { } | |
}, | |
"type" : "attrvalues" | |
} | |
} | |
} | |
} | |
}' | |
curl -XGET 'http://localhost:9200/products/_search?pretty=true' -d ' | |
{ | |
"query" : { | |
"filtered" : { | |
"query" : { | |
"match_all" : { } | |
}, | |
"filter" : { | |
"has_child" : { | |
"query" : { | |
"match_all" : { } | |
}, | |
"type" : "attributes" | |
} | |
} | |
} | |
} | |
}' | |
# Get all attributes associated to a catentry | |
curl 'http://localhost:9200/products/attributes/_search?pretty=true' -d '{ | |
"query" : { | |
"has_parent" : { | |
"parent_type" : "catentry", | |
"query" : { | |
"term" : { | |
"catentry_id" : "EI01Y" | |
} | |
} | |
} | |
} | |
}' && echo | |
# Get all attributes, attributes attribute values associated to a catentry | |
curl -XPOST 'http://localhost:9200/products/_search?pretty=true' -d '{ | |
"query": { | |
"filtered": { | |
"query": { | |
"term": {"catentry_id" :"EI01Y" } | |
} | |
} | |
} | |
}' | |
# Get all catentries with an attribute_id | |
curl 'http://localhost:9200/products/_search?pretty=true' -d '{ | |
"query" : { | |
"constant_score" : { | |
"filter" : { | |
"has_child" : { | |
"type" : "attributes", | |
"query" : { | |
"term" : { | |
"attribute_id" : "0001" | |
} | |
} | |
} | |
} | |
} | |
} | |
}' && echo | |
# Get all attributes for a catentry | |
curl 'http://localhost:9200/products/_search?pretty=true' -d '{ | |
"query" : { | |
"constant_score" : { | |
"filter" : { | |
"has_parent" : { | |
"type" : "attributes", | |
"query" : { | |
"term" : { | |
"catentry_id" : "EI01Y" | |
} | |
} | |
} | |
} | |
} | |
} | |
}' && echo | |
## Sample CRUD operation | |
# Update key-value for a product attribute by using attribute key | |
curl -XPOST "http://localhost:9200/products/attributes/0001/_update?parent=EI01Y" -d '{ | |
"script" : "ctx._source.displayable = \"false\"" | |
}' && echo | |
#Delete an attribute by id | |
curl -XDELETE http://localhost:9200/products/attributes/0001?pretty=true && echo | |
#Elastic Load script | |
#!/bin/bash | |
2 c=1 | |
3 while [ $c -le 200000 ];do | |
4 | |
5 echo { \"index\" : { \"_index\" : \"products\", \"_type\" : \"attributes\", \"_id\" : \"700000000000000000$c\", \"parent\" : \"EI01Y\", \"routing\" : \"EI01Y\" } } | |
6 echo {\"attribute_id\": \"700000000000000000$c\",\"identifier\":\"swatchSize$c\",\"displayable\":\"true\",\"usage\": \"1\",\"catentry_id\" : \"EI01Y\"} | |
7 echo { \"index\" : { \"_index\" : \"products\", \"_type\" : \"attrvalues\", \"_id\" : \"0001-01\", \"parent\" : \"700000000000000000$c\", \"routing\" : \"EI01Y\" } } | |
8 echo { \"identifier\": \"XL$c\",\"stringvalue\":\"test\",\"integervalue\":\"test\",\"floatvalue\":\"test\", \"catentry_id\" : \"EI01Y\"} | |
9 | |
10 (( c++ )) | |
11 done | |
curl -s -XPOST localhost:9200/_bulk?pretty=true --data-binary' --data-binary @data.json | |
# Update key-value for a product attribute by using attribute key | |
curl -XPOST "http://localhost:9200/products/attributes/7000000000000000006/_update?parent=EI01Y" -d '{ | |
"script" : "ctx._source.displayable = \"false\"" | |
}' && echo | |
curl -XGET "http://localhost:9200/products/attributes/7000000000000000006?pretty=true" && echo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment