介绍

嵌入式类型

tips

Elasticsearch nested - 图1

官网:Nested field type

使用

映射

  1. PUT gulimall_product/
  2. {
  3. "mappings": {
  4. "properties": {
  5. "attrs": {
  6. "type": "nested",
  7. "properties": {
  8. "attrId": {
  9. "type": "long"
  10. },
  11. "attrName": {
  12. "type": "keyword"
  13. },
  14. "attrValue": {
  15. "type": "keyword"
  16. }
  17. }
  18. }
  19. }
  20. }
  21. }

DSL(查询)

  1. GET gulimall_product/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "filter": {
  6. "nested": {
  7. "path": "attrs",
  8. "query": {
  9. "bool": {
  10. "must": [
  11. {
  12. "term": {
  13. "attrs.attrId": {
  14. "value": "6"
  15. }
  16. }
  17. },
  18. {
  19. "terms": {
  20. "attrs.attrValue": [
  21. "骁龙",
  22. "海思(Hisilicon)"
  23. ]
  24. }
  25. }
  26. ]
  27. }
  28. }
  29. }
  30. }
  31. }
  32. }
  33. }

聚合

  1. GET gulimall_product/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "aggs": {
  7. "attr_agg":{
  8. "nested": {
  9. "path": "attrs"
  10. },
  11. "aggs": {
  12. "attr_id_agg": {
  13. "terms": {
  14. "field": "attrs.attrId",
  15. "size": 10
  16. },
  17. "aggs": {
  18. "attr_name_agg": {
  19. "terms": {
  20. "field": "attrs.attrName",
  21. "size": 10
  22. }
  23. },
  24. "attr_value_agg":{
  25. "terms": {
  26. "field": "attrs.attrValue",
  27. "size": 10
  28. }
  29. }
  30. }
  31. }
  32. }
  33. }
  34. },
  35. "size": 0
  36. }