Last active
August 29, 2015 14:19
-
-
Save up1/ab7b1d66a35d505f5d9c to your computer and use it in GitHub Desktop.
Elasticsearch :: Example of grouping data
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
//Step 1 :: Index data | |
DELETE demo | |
POST demo/product/ | |
{ | |
"brand": "BrandA", | |
"id" : "Pro 005", | |
"name": "Meo", | |
"barcode" : "000051", | |
"unit" : 1 | |
} | |
POST demo/product/ | |
{ | |
"brand": "BrandA", | |
"id" : "Pro 001", | |
"name": "Meo", | |
"barcode" : "000052", | |
"unit" : 2 | |
} | |
POST demo/product/ | |
{ | |
"brand": "BrandA", | |
"id" : "Pro 002", | |
"name": "Meo", | |
"barcode" : "000053", | |
"unit" : 3 | |
} | |
POST demo/product/ | |
{ | |
"brand": "BrandB", | |
"id" : "Pro 006", | |
"name": "Meo", | |
"barcode" : "000061", | |
"unit" : 1 | |
} | |
POST demo/product/ | |
{ | |
"brand": "BrandB", | |
"id" : "Pro 007", | |
"name": "Meo", | |
"barcode" : "000062", | |
"unit" : 13 | |
} | |
//Step 2 :: ดึงข้อมูลโดยแบ่งกลุ่มตาม brand และแต่ละกลุ่มเรียงตามจำนวน unit จากมากไปน้อย | |
GET demo/product/_search?search_type=count | |
{ | |
"aggs": { | |
"group_by_brand": { | |
"terms": { | |
"field": "brand", | |
"size": 10 | |
}, | |
"aggs": { | |
"NAME": { | |
"top_hits": { | |
"_source": [ | |
"id", | |
"barcode", | |
"name" | |
], | |
"size": 10, | |
"sort": [ | |
{ | |
"unit": { | |
"order": "desc" | |
} | |
} | |
] | |
} | |
} | |
} | |
} | |
} | |
} | |
ผลที่ได้ดังนี้ | |
{ | |
"took": 2, | |
"timed_out": false, | |
"_shards": { | |
"total": 5, | |
"successful": 5, | |
"failed": 0 | |
}, | |
"hits": { | |
"total": 5, | |
"max_score": 0, | |
"hits": [] | |
}, | |
"aggregations": { | |
"group_by_brand": { | |
"doc_count_error_upper_bound": 0, | |
"sum_other_doc_count": 0, | |
"buckets": [ | |
{ | |
"key": "branda", | |
"doc_count": 3, | |
"NAME": { | |
"hits": { | |
"total": 3, | |
"max_score": null, | |
"hits": [ | |
{ | |
"_index": "demo", | |
"_type": "product", | |
"_id": "AUzV-9pi8FIkg0OYT-vp", | |
"_score": null, | |
"_source": { | |
"name": "Meo", | |
"id": "Pro 002", | |
"barcode": "000053" | |
}, | |
"sort": [ | |
3 | |
] | |
}, | |
{ | |
"_index": "demo", | |
"_type": "product", | |
"_id": "AUzV-8vB8FIkg0OYT-vo", | |
"_score": null, | |
"_source": { | |
"name": "Meo", | |
"id": "Pro 001", | |
"barcode": "000052" | |
}, | |
"sort": [ | |
2 | |
] | |
}, | |
{ | |
"_index": "demo", | |
"_type": "product", | |
"_id": "AUzV-8Wc8FIkg0OYT-vn", | |
"_score": null, | |
"_source": { | |
"name": "Meo", | |
"id": "Pro 005", | |
"barcode": "000051" | |
}, | |
"sort": [ | |
1 | |
] | |
} | |
] | |
} | |
} | |
}, | |
{ | |
"key": "brandb", | |
"doc_count": 2, | |
"NAME": { | |
"hits": { | |
"total": 2, | |
"max_score": null, | |
"hits": [ | |
{ | |
"_index": "demo", | |
"_type": "product", | |
"_id": "AUzV--eT8FIkg0OYT-vr", | |
"_score": null, | |
"_source": { | |
"name": "Meo", | |
"id": "Pro 007", | |
"barcode": "000062" | |
}, | |
"sort": [ | |
13 | |
] | |
}, | |
{ | |
"_index": "demo", | |
"_type": "product", | |
"_id": "AUzV--KM8FIkg0OYT-vq", | |
"_score": null, | |
"_source": { | |
"name": "Meo", | |
"id": "Pro 006", | |
"barcode": "000061" | |
}, | |
"sort": [ | |
1 | |
] | |
} | |
] | |
} | |
} | |
} | |
] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment