Advertisement
Guest User

Untitled

a guest
May 29th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. //Step 1 :: Index data
  2. DELETE demo
  3.  
  4. POST demo/product/
  5. {
  6. "brand": "BrandA",
  7. "id" : "Pro 005",
  8. "name": "Meo",
  9. "barcode" : "000051",
  10. "unit" : 1
  11. }
  12.  
  13. POST demo/product/
  14. {
  15. "brand": "BrandA",
  16. "id" : "Pro 001",
  17. "name": "Meo",
  18. "barcode" : "000052",
  19. "unit" : 2
  20. }
  21.  
  22. POST demo/product/
  23. {
  24. "brand": "BrandA",
  25. "id" : "Pro 002",
  26. "name": "Meo",
  27. "barcode" : "000053",
  28. "unit" : 3
  29. }
  30.  
  31.  
  32. POST demo/product/
  33. {
  34. "brand": "BrandB",
  35. "id" : "Pro 006",
  36. "name": "Meo",
  37. "barcode" : "000061",
  38. "unit" : 1
  39. }
  40.  
  41. POST demo/product/
  42. {
  43. "brand": "BrandB",
  44. "id" : "Pro 007",
  45. "name": "Meo",
  46. "barcode" : "000062",
  47. "unit" : 13
  48. }
  49.  
  50.  
  51. //Step 2 :: ดึงข้อมูลโดยแบ่งกลุ่มตาม brand และแต่ละกลุ่มเรียงตามจำนวน unit จากมากไปน้อย
  52.  
  53. GET demo/product/_search?search_type=count
  54. {
  55. "aggs": {
  56. "group_by_brand": {
  57. "terms": {
  58. "field": "brand",
  59. "size": 10
  60. },
  61. "aggs": {
  62. "NAME": {
  63. "top_hits": {
  64. "_source": [
  65. "id",
  66. "barcode",
  67. "name"
  68. ],
  69. "size": 10,
  70. "sort": [
  71. {
  72. "unit": {
  73. "order": "desc"
  74. }
  75. }
  76. ]
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83.  
  84.  
  85. ผลที่ได้ดังนี้
  86.  
  87. {
  88. "took": 2,
  89. "timed_out": false,
  90. "_shards": {
  91. "total": 5,
  92. "successful": 5,
  93. "failed": 0
  94. },
  95. "hits": {
  96. "total": 5,
  97. "max_score": 0,
  98. "hits": []
  99. },
  100. "aggregations": {
  101. "group_by_brand": {
  102. "doc_count_error_upper_bound": 0,
  103. "sum_other_doc_count": 0,
  104. "buckets": [
  105. {
  106. "key": "branda",
  107. "doc_count": 3,
  108. "NAME": {
  109. "hits": {
  110. "total": 3,
  111. "max_score": null,
  112. "hits": [
  113. {
  114. "_index": "demo",
  115. "_type": "product",
  116. "_id": "AUzV-9pi8FIkg0OYT-vp",
  117. "_score": null,
  118. "_source": {
  119. "name": "Meo",
  120. "id": "Pro 002",
  121. "barcode": "000053"
  122. },
  123. "sort": [
  124. 3
  125. ]
  126. },
  127. {
  128. "_index": "demo",
  129. "_type": "product",
  130. "_id": "AUzV-8vB8FIkg0OYT-vo",
  131. "_score": null,
  132. "_source": {
  133. "name": "Meo",
  134. "id": "Pro 001",
  135. "barcode": "000052"
  136. },
  137. "sort": [
  138. 2
  139. ]
  140. },
  141. {
  142. "_index": "demo",
  143. "_type": "product",
  144. "_id": "AUzV-8Wc8FIkg0OYT-vn",
  145. "_score": null,
  146. "_source": {
  147. "name": "Meo",
  148. "id": "Pro 005",
  149. "barcode": "000051"
  150. },
  151. "sort": [
  152. 1
  153. ]
  154. }
  155. ]
  156. }
  157. }
  158. },
  159. {
  160. "key": "brandb",
  161. "doc_count": 2,
  162. "NAME": {
  163. "hits": {
  164. "total": 2,
  165. "max_score": null,
  166. "hits": [
  167. {
  168. "_index": "demo",
  169. "_type": "product",
  170. "_id": "AUzV--eT8FIkg0OYT-vr",
  171. "_score": null,
  172. "_source": {
  173. "name": "Meo",
  174. "id": "Pro 007",
  175. "barcode": "000062"
  176. },
  177. "sort": [
  178. 13
  179. ]
  180. },
  181. {
  182. "_index": "demo",
  183. "_type": "product",
  184. "_id": "AUzV--KM8FIkg0OYT-vq",
  185. "_score": null,
  186. "_source": {
  187. "name": "Meo",
  188. "id": "Pro 006",
  189. "barcode": "000061"
  190. },
  191. "sort": [
  192. 1
  193. ]
  194. }
  195. ]
  196. }
  197. }
  198. }
  199. ]
  200. }
  201. }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement