计算每个tag下的商品数量

按照 tags 进行分组

  1. GET /ecommerce/product/_search
  2. {
  3. "aggs": {
  4. "group_by_tags": {
  5. "terms": {
  6. "field": "tags"
  7. }
  8. }
  9. }
  10. }

查询会报错

  1. {
  2. "error": {
  3. "root_cause": [
  4. {
  5. "type": "illegal_argument_exception",
  6. "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [tags] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."
  7. }
  8. ],
  9. "type": "search_phase_execution_exception",
  10. "reason": "all shards failed",
  11. "phase": "query",
  12. "grouped": true,
  13. "failed_shards": [
  14. {
  15. "shard": 0,
  16. "index": "ecommerce",
  17. "node": "-_dfHkkASWCagwSHZacgoA",
  18. "reason": {
  19. "type": "illegal_argument_exception",
  20. "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [tags] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."
  21. }
  22. }
  23. ],
  24. "caused_by": {
  25. "type": "illegal_argument_exception",
  26. "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [tags] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."
  27. }
  28. },
  29. "status": 400
  30. }

大概意思是聚合分析要提前生成正排索引,你需要提前把需要聚合对字段设置为 fielddata = true,才会生成对应对正排索引。
所以需要调用这个接口将 tags 对 fielddata 设置为 true。

  1. GET /ecommerce/_mapping/product
  2. {
  3. "properties": {
  4. "tags": {
  5. "type": "text",
  6. "fielddata": true
  7. }
  8. }
  9. }

再执行聚合分析,返回

  1. {
  2. "took": 2,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "failed": 0
  8. },
  9. "hits": {
  10. "total": 4,
  11. "max_score": 1,
  12. "hits": [
  13. {
  14. "_index": "ecommerce",
  15. "_type": "product",
  16. "_id": "2",
  17. "_score": 1,
  18. "_source": {
  19. "name": "jiajieshi yagao",
  20. "desc": "youxiao fangzhu",
  21. "price": 25,
  22. "producer": "jiajieshi producer",
  23. "tags": [
  24. "fangzhu"
  25. ]
  26. }
  27. },
  28. {
  29. "_index": "ecommerce",
  30. "_type": "product",
  31. "_id": "4",
  32. "_score": 1,
  33. "_source": {
  34. "name": "heiren yagao",
  35. "desc": "hei",
  36. "price": 38,
  37. "producer": "heiren producer",
  38. "tags": [
  39. "hei"
  40. ]
  41. }
  42. },
  43. {
  44. "_index": "ecommerce",
  45. "_type": "product",
  46. "_id": "1",
  47. "_score": 1,
  48. "_source": {
  49. "name": "gaolujie yagao",
  50. "desc": "gaoxiao meibai",
  51. "price": 30,
  52. "producer": "gaolujie producer",
  53. "tags": [
  54. "meibai",
  55. "fangzhu"
  56. ]
  57. }
  58. },
  59. {
  60. "_index": "ecommerce",
  61. "_type": "product",
  62. "_id": "3",
  63. "_score": 1,
  64. "_source": {
  65. "name": "heiren yagao",
  66. "desc": "hei",
  67. "price": 38,
  68. "producer": "heiren producer",
  69. "tags": [
  70. "hei"
  71. ]
  72. }
  73. }
  74. ]
  75. },
  76. "aggregations": {
  77. "group_by_tags": {
  78. "doc_count_error_upper_bound": 0,
  79. "sum_other_doc_count": 0,
  80. "buckets": [
  81. {
  82. "key": "fangzhu",
  83. "doc_count": 2
  84. },
  85. {
  86. "key": "hei",
  87. "doc_count": 2
  88. },
  89. {
  90. "key": "meibai",
  91. "doc_count": 1
  92. }
  93. ]
  94. }
  95. }
  96. }{
  97. "took": 65,
  98. "timed_out": false,
  99. "_shards": {
  100. "total": 5,
  101. "successful": 5,
  102. "failed": 0
  103. },
  104. "hits": {
  105. "total": 4,
  106. "max_score": 1,
  107. "hits": [
  108. {
  109. "_index": "ecommerce",
  110. "_type": "product",
  111. "_id": "2",
  112. "_score": 1,
  113. "_source": {
  114. "name": "jiajieshi yagao",
  115. "desc": "youxiao fangzhu",
  116. "price": 25,
  117. "producer": "jiajieshi producer",
  118. "tags": [
  119. "fangzhu"
  120. ]
  121. }
  122. },
  123. {
  124. "_index": "ecommerce",
  125. "_type": "product",
  126. "_id": "4",
  127. "_score": 1,
  128. "_source": {
  129. "name": "heiren yagao",
  130. "desc": "hei",
  131. "price": 38,
  132. "producer": "heiren producer",
  133. "tags": [
  134. "hei"
  135. ]
  136. }
  137. },
  138. {
  139. "_index": "ecommerce",
  140. "_type": "product",
  141. "_id": "1",
  142. "_score": 1,
  143. "_source": {
  144. "name": "gaolujie yagao",
  145. "desc": "gaoxiao meibai",
  146. "price": 30,
  147. "producer": "gaolujie producer",
  148. "tags": [
  149. "meibai",
  150. "fangzhu"
  151. ]
  152. }
  153. },
  154. {
  155. "_index": "ecommerce",
  156. "_type": "product",
  157. "_id": "3",
  158. "_score": 1,
  159. "_source": {
  160. "name": "heiren yagao",
  161. "desc": "hei",
  162. "price": 38,
  163. "producer": "heiren producer",
  164. "tags": [
  165. "hei"
  166. ]
  167. }
  168. }
  169. ]
  170. },
  171. "aggregations": {
  172. "group_by_tags": {
  173. "doc_count_error_upper_bound": 0,
  174. "sum_other_doc_count": 0,
  175. "buckets": [
  176. {
  177. "key": "fangzhu",
  178. "doc_count": 2
  179. },
  180. {
  181. "key": "hei",
  182. "doc_count": 2
  183. },
  184. {
  185. "key": "meibai",
  186. "doc_count": 1
  187. }
  188. ]
  189. }
  190. }
  191. }

对名称中包含yagao的商品,计算每个tag下的商品数量

  1. GET /ecommerce/product/_search
  2. {
  3. "size": 0,
  4. "query": {
  5. "match": {
  6. "name": "yagao"
  7. }
  8. },
  9. "aggs": {
  10. "group_by_tags": {
  11. "terms": {
  12. "field": "tags"
  13. }
  14. }
  15. }
  16. }

计算每个tag下的商品的平均价格

  1. GET /ecommerce/product/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "group_by_tags": {
  6. "terms": {
  7. "field": "tags"
  8. },
  9. "aggs": {
  10. "avg_price": {
  11. "avg": {
  12. "field": "price"
  13. }
  14. }
  15. }
  16. }
  17. }
  18. }
  19. // 返回结果
  20. {
  21. "took": 13,
  22. "timed_out": false,
  23. "_shards": {
  24. "total": 5,
  25. "successful": 5,
  26. "failed": 0
  27. },
  28. "hits": {
  29. "total": 4,
  30. "max_score": 0,
  31. "hits": []
  32. },
  33. "aggregations": {
  34. "group_by_tags": {
  35. "doc_count_error_upper_bound": 0,
  36. "sum_other_doc_count": 0,
  37. "buckets": [
  38. {
  39. "key": "fangzhu",
  40. "doc_count": 2,
  41. "avg_price": {
  42. "value": 27.5
  43. }
  44. },
  45. {
  46. "key": "hei",
  47. "doc_count": 2,
  48. "avg_price": {
  49. "value": 38
  50. }
  51. },
  52. {
  53. "key": "meibai",
  54. "doc_count": 1,
  55. "avg_price": {
  56. "value": 30
  57. }
  58. }
  59. ]
  60. }
  61. }
  62. }

计算每个tag下的商品的平均价格并排序

  1. GET /ecommerce/product/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "group_by_tags": {
  6. "terms": {
  7. "field": "tags",
  8. "order": {
  9. "avg_price": "desc"
  10. }
  11. },
  12. "aggs": {
  13. "avg_price": {
  14. "avg": {
  15. "field": "price"
  16. }
  17. }
  18. }
  19. }
  20. }
  21. }

按照指定的价格范围区间进行分组,然后在每组内再按照tag进行分组,最后再算每组的平均价格

  1. GET /ecommerce/product/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "group_by_price": {
  6. "range": {
  7. "field": "price",
  8. "ranges": [
  9. {
  10. "from": 10,
  11. "to": 20
  12. },
  13. {
  14. "from": 20,
  15. "to": 30
  16. },
  17. {
  18. "from": 30,
  19. "to": 40
  20. }
  21. ]
  22. },
  23. "aggs": {
  24. "group_by_tags": {
  25. "terms": {
  26. "field": "tags"
  27. },
  28. "aggs": {
  29. "average_price": {
  30. "avg": {
  31. "field": "price"
  32. }
  33. }
  34. }
  35. }
  36. }
  37. }
  38. }
  39. }
  40. // 返回结果
  41. {
  42. "took": 11,
  43. "timed_out": false,
  44. "_shards": {
  45. "total": 5,
  46. "successful": 5,
  47. "failed": 0
  48. },
  49. "hits": {
  50. "total": 4,
  51. "max_score": 0,
  52. "hits": []
  53. },
  54. "aggregations": {
  55. "group_by_price": {
  56. "buckets": [
  57. {
  58. "key": "10.0-20.0",
  59. "from": 10,
  60. "to": 20,
  61. "doc_count": 0,
  62. "group_by_tags": {
  63. "doc_count_error_upper_bound": 0,
  64. "sum_other_doc_count": 0,
  65. "buckets": []
  66. }
  67. },
  68. {
  69. "key": "20.0-30.0",
  70. "from": 20,
  71. "to": 30,
  72. "doc_count": 1,
  73. "group_by_tags": {
  74. "doc_count_error_upper_bound": 0,
  75. "sum_other_doc_count": 0,
  76. "buckets": [
  77. {
  78. "key": "fangzhu",
  79. "doc_count": 1,
  80. "average_price": {
  81. "value": 25
  82. }
  83. }
  84. ]
  85. }
  86. },
  87. {
  88. "key": "30.0-40.0",
  89. "from": 30,
  90. "to": 40,
  91. "doc_count": 3,
  92. "group_by_tags": {
  93. "doc_count_error_upper_bound": 0,
  94. "sum_other_doc_count": 0,
  95. "buckets": [
  96. {
  97. "key": "hei",
  98. "doc_count": 2,
  99. "average_price": {
  100. "value": 38
  101. }
  102. },
  103. {
  104. "key": "fangzhu",
  105. "doc_count": 1,
  106. "average_price": {
  107. "value": 30
  108. }
  109. },
  110. {
  111. "key": "meibai",
  112. "doc_count": 1,
  113. "average_price": {
  114. "value": 30
  115. }
  116. }
  117. ]
  118. }
  119. }
  120. ]
  121. }
  122. }
  123. }