2024-03-13  阅读(3)
原文作者:吴声子夜歌 原文地址: https://blog.csdn.net/cold___play/article/details/104764741

将文本field的fielddata属性设置为true

    PUT /ecommerce/_mapping/product
    {
      "properties": {
        "tags": {
          "type": "text",
          "fielddata": true
        }
      }
    }

第一个分析需求:计算每个tag下的商品数量

    GET /ecommerce/product/_search
    {
      "aggs": {
        "group_by_tags": {
          "terms": { "field": "tags" }
        }
      }
    }

返回:

    {
      "took": 34,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 4,
        "max_score": 1,
        "hits": [
          {
            "_index": "ecommerce",
            "_type": "product",
            "_id": "2",
            "_score": 1,
            "_source": {
              "name": "jiajieshi yagao",
              "desc": "youxiao fangzhu",
              "price": 25,
              "producer": "jiajieshi producer",
              "tags": [
                "fangzhu"
              ]
            }
          },
          {
            "_index": "ecommerce",
            "_type": "product",
            "_id": "4",
            "_score": 1,
            "_source": {
              "name": "special yagao",
              "desc": "special meibai",
              "price": 50,
              "producer": "special yagao producer",
              "tags": [
                "meibai"
              ]
            }
          },
          {
            "_index": "ecommerce",
            "_type": "product",
            "_id": "1",
            "_score": 1,
            "_source": {
              "name": "gaolujie yagao",
              "desc": "gaoxiao meibai",
              "price": 30,
              "producer": "gaolujie producer",
              "tags": [
                "meibai",
                "fangzhu"
              ]
            }
          },
          {
            "_index": "ecommerce",
            "_type": "product",
            "_id": "3",
            "_score": 1,
            "_source": {
              "name": "zhonghua yagao",
              "desc": "caoben zhiwu",
              "price": 40,
              "producer": "zhonghua producer",
              "tags": [
                "qingxin"
              ]
            }
          }
        ]
      },
      "aggregations": {
        "group_by_tags": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": "fangzhu",
              "doc_count": 2
            },
            {
              "key": "meibai",
              "doc_count": 2
            },
            {
              "key": "qingxin",
              "doc_count": 1
            }
          ]
        }
      }
    }

第二个聚合分析的需求:对名称中包含yagao的商品,计算每个tag下的商品数量

    GET /ecommerce/product/_search
    {
      "size": 0,
      "query": {
        "match": {
          "name": "yagao"
        }
      },
      "aggs": {
        "all_tags": {
          "terms": {
            "field": "tags"
          }
        }
      }
    }

返回:

    {
      "took": 5,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 4,
        "max_score": 0,
        "hits": []
      },
      "aggregations": {
        "all_tags": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": "fangzhu",
              "doc_count": 2
            },
            {
              "key": "meibai",
              "doc_count": 2
            },
            {
              "key": "qingxin",
              "doc_count": 1
            }
          ]
        }
      }
    }

第三个聚合分析的需求:先分组,再算每组的平均值,计算每个tag下的商品的平均价格

    GET /ecommerce/product/_search
    {
        "size": 0,
        "aggs" : {
            "group_by_tags" : {
                "terms" : { "field" : "tags" },
                "aggs" : {
                    "avg_price" : {
                        "avg" : { "field" : "price" }
                    }
                }
            }
        }
    }

返回:

    {
      "took": 5,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 4,
        "max_score": 0,
        "hits": []
      },
      "aggregations": {
        "group_by_tags": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": "fangzhu",
              "doc_count": 2,
              "avg_price": {
                "value": 27.5
              }
            },
            {
              "key": "meibai",
              "doc_count": 2,
              "avg_price": {
                "value": 40
              }
            },
            {
              "key": "qingxin",
              "doc_count": 1,
              "avg_price": {
                "value": 40
              }
            }
          ]
        }
      }
    }

第四个数据分析需求:计算每个tag下的商品的平均价格,并且按照平均价格降序排序

    GET /ecommerce/product/_search
    {
        "size": 0,
        "aggs" : {
            "all_tags" : {
                "terms" : { "field" : "tags", "order": { "avg_price": "desc" } },
                "aggs" : {
                    "avg_price" : {
                        "avg" : { "field" : "price" }
                    }
                }
            }
        }
    }

返回:

    {
      "took": 2,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 4,
        "max_score": 0,
        "hits": []
      },
      "aggregations": {
        "all_tags": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": "meibai",
              "doc_count": 2,
              "avg_price": {
                "value": 40
              }
            },
            {
              "key": "qingxin",
              "doc_count": 1,
              "avg_price": {
                "value": 40
              }
            },
            {
              "key": "fangzhu",
              "doc_count": 2,
              "avg_price": {
                "value": 27.5
              }
            }
          ]
        }
      }
    }

Java 面试宝典是大明哥全力打造的 Java 精品面试题,它是一份靠谱、强大、详细、经典的 Java 后端面试宝典。它不仅仅只是一道道面试题,而是一套完整的 Java 知识体系,一套你 Java 知识点的扫盲贴。

它的内容包括:

  • 大厂真题:Java 面试宝典里面的题目都是最近几年的高频的大厂面试真题。
  • 原创内容:Java 面试宝典内容全部都是大明哥原创,内容全面且通俗易懂,回答部分可以直接作为面试回答内容。
  • 持续更新:一次购买,永久有效。大明哥会持续更新 3+ 年,累计更新 1000+,宝典会不断迭代更新,保证最新、最全面。
  • 覆盖全面:本宝典累计更新 1000+,从 Java 入门到 Java 架构的高频面试题,实现 360° 全覆盖。
  • 不止面试:内容包含面试题解析、内容详解、知识扩展,它不仅仅只是一份面试题,更是一套完整的 Java 知识体系。
  • 宝典详情:https://www.yuque.com/chenssy/sike-java/xvlo920axlp7sf4k
  • 宝典总览:https://www.yuque.com/chenssy/sike-java/yogsehzntzgp4ly1
  • 宝典进展:https://www.yuque.com/chenssy/sike-java/en9ned7loo47z5aw

目前 Java 面试宝典累计更新 400+ 道,总字数 42w+。大明哥还在持续更新中,下图是大明哥在 2024-12 月份的更新情况:

想了解详情的小伙伴,扫描下面二维码加大明哥微信【daming091】咨询

同时,大明哥也整理一套目前市面最常见的热点面试题。微信搜[大明哥聊 Java]或扫描下方二维码关注大明哥的原创公众号[大明哥聊 Java] ,回复【面试题】 即可免费领取。

阅读全文