Skip to content

Instantly share code, notes, and snippets.

@imotov
Forked from baxford/gist:5551877
Last active December 17, 2015 04:59
Show Gist options
  • Save imotov/5554678 to your computer and use it in GitHub Desktop.
Save imotov/5554678 to your computer and use it in GitHub Desktop.
curl -XDELETE 'http://localhost:9200/app'
curl -XPUT 'http://localhost:9200/app' -d '
{
"settings": {
"index.number_of_shards": 1,
"index.number_of_replicas": 0
},
"mappings" : {
"parent": {
"properties" : {
"children": {
"type" : "nested",
"properties" : {
"id" : {"store" : false, "type": "long", "include_in_all": false},
"sent" : {"store" : false, "type": "long", "include_in_all": false}
}
}
}
}
}
}'
curl -XPUT 'http://localhost:9200/app/parent/2nested' -d '
{
"children": [
{
"id": 1967,
"sent": 1367561477819
},
{
"id": 1968,
"sent": 1367723988849
}
]
}
'
curl -XPUT 'http://localhost:9200/app/parent/manyNested' -d '
{"children":[{"id":1970,"sent":1367557829592},{"id":1967,"sent":1367561477651},{"id":1963,"sent":1367566046098},{"id":2220,"sent":1367574093902},{"id":2221,"sent":1367574178118},{"id":1949,"sent":1367577441224},{"id":2230,"sent":1367578681929},{"id":2255,"sent":1367582445865},{"id":2229,"sent":1367582978773},{"id":2036,"sent":1367586060714},{"id":2181,"sent":1367588265555},{"id":2135,"sent":1367589621826},{"id":2040,"sent":1367590988082},{"id":2265,"sent":1367597111092},{"id":1961,"sent":1367597766420},{"id":2368,"sent":1367607084937},{"id":2325,"sent":1367604622108},{"id":2380,"sent":1367632844989},{"id":2449,"sent":1367637386597},{"id":2162,"sent":1367639335040},{"id":2457,"sent":1367640147955},{"id":2359,"sent":1367640976257},{"id":1995,"sent":1367642443645},{"id":2106,"sent":1367644004789},{"id":2483,"sent":1367645257597},{"id":2244,"sent":1367643998849},{"id":2070,"sent":1367645763061},{"id":1955,"sent":1367648027479},{"id":2413,"sent":1367647150696},{"id":2432,"sent":1367644680952},{"id":2236,"sent":1367645491674},{"id":2154,"sent":1367648593594},{"id":2017,"sent":1367645542935},{"id":2437,"sent":1367649600428},{"id":2447,"sent":1367648433362},{"id":2115,"sent":1367650722908},{"id":2147,"sent":1367649384788},{"id":2415,"sent":1367652386667},{"id":2490,"sent":1367660500351},{"id":2431,"sent":1367660240898},{"id":2565,"sent":1367661020928},{"id":2501,"sent":1367661971510},{"id":2078,"sent":1367663056515},{"id":2184,"sent":1367662922221},{"id":2583,"sent":1367664149452},{"id":2195,"sent":1367664839517},{"id":2562,"sent":1367664317481},{"id":2286,"sent":1367664384121},{"id":2462,"sent":1367667431564},{"id":2332,"sent":1367669188915},{"id":2143,"sent":1367674582587},{"id":1957,"sent":1367675023621},{"id":2238,"sent":1367685309996},{"id":2074,"sent":1367687406146},{"id":2374,"sent":1367686493566},{"id":2336,"sent":1367693189820},{"id":2317,"sent":1367693900534},{"id":2095,"sent":1367695156486},{"id":1947,"sent":1367699112244},{"id":2031,"sent":1367699831117},{"id":2356,"sent":1367699040377},{"id":2492,"sent":1367699156344},{"id":2407,"sent":1367688683169},{"id":2122,"sent":1367699317524},{"id":2049,"sent":1367699998534},{"id":2352,"sent":1367705893143},{"id":2611,"sent":1367704332534},{"id":2463,"sent":1367705247247},{"id":2295,"sent":1367705743731},{"id":2428,"sent":1367707924514},{"id":2713,"sent":1367711489640},{"id":1998,"sent":1367688901861},{"id":2218,"sent":1367715074441},{"id":2438,"sent":1367688967128},{"id":2668,"sent":1367715433513},{"id":2429,"sent":1367716384220},{"id":2081,"sent":1367717543664},{"id":2232,"sent":1367717580481},{"id":2405,"sent":1367718543453},{"id":2133,"sent":1367702124091},{"id":2185,"sent":1367709922527},{"id":2053,"sent":1367720072185},{"id":2409,"sent":1367711905080},{"id":2489,"sent":1367712328854},{"id":2402,"sent":1367713280879},{"id":2007,"sent":1367714124287},{"id":2704,"sent":1367714511892},{"id":2388,"sent":1367715713018},{"id":2360,"sent":1367716008115},{"id":2004,"sent":1367721618552},{"id":2508,"sent":1367717122370},{"id":2027,"sent":1367717322516},{"id":2502,"sent":1367721633478},{"id":2697,"sent":1367718063199},{"id":2525,"sent":1367722480411},{"id":1974,"sent":1367722629388},{"id":2165,"sent":1367718180288},{"id":2695,"sent":1367718702869},{"id":1976,"sent":1367719463806},{"id":1968,"sent":1367721068881},{"id":2030,"sent":1367722280029},{"id":2300,"sent":1367725603161}]}
'
curl -XPOST 'http://localhost:9200/app/_flush'
curl -XPOST 'http://localhost:9200/app/_refresh'
echo
curl -XPOST 'http://localhost:9200/app/parent/manyNested/_update' -d '{
"script" :
"if(ctx._source[\"children\"] != null) { for (int i = 0; i < ctx._source.children.size(); i++){ if(ctx._source.children[i].id == child_id){ctx._source.children.remove(i);i--;}}} if (ctx._source[\"children\"] == null) { ctx._source.children = [{\"id\": child_id, \"sent\": timestamp }] } else {ctx._source.children += [{\"id\": child_id, \"sent\": timestamp }] }"
,
"params" : {
"parent_id" : "1183146090417",
"timestamp" : 1367722894963,
"child_id" : 2030
},
"upsert" : {
"children": [
{
"id": 2030,
"sent": 1367722894963
}
]
}
}' && echo
# However, the same issue doesn't seem to happen to a doc with less nested docs
curl -XPOST 'http://localhost:9200/app/parent/2nested/_update' -d '{
"script" :
"if(ctx._source[\"children\"] != null) { for (int i = 0; i < ctx._source.children.size(); i++){ if(ctx._source.children[i].id == child_id){ctx._source.children.remove(i);i--;}}} if (ctx._source[\"children\"] == null) { ctx._source.children = [{\"id\": child_id, \"sent\": timestamp }] } else {ctx._source.children += [{\"id\": child_id, \"sent\": timestamp }] }"
,
"params" : {
"parent_id" : "1183146090417",
"timestamp" : 1367722894963,
"child_id" : 2030
},
"upsert" : {
"children": [
{
"id": 2030,
"sent": 1367722894963
}
]
}
}' && echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment