前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >013.Elasticsearch-6.x聚合统计REST API入门

013.Elasticsearch-6.x聚合统计REST API入门

作者头像
CoderJed
发布2020-07-01 10:23:33
5320
发布2020-07-01 10:23:33
举报
文章被收录于专栏:Jed的技术阶梯

1. 准备测试数据

代码语言:javascript
复制
PUT /shop/product/1
{
    "name": "Charcoal Toothpaste",
    "desc": "Travel-Friendly Daily Use Teeth Whitening Cleaning Activated Organic Charcoal Toothpaste",
    "price": 30,
    "producer": "Charcoal Producer",
    "tags": ["Whitening", "Refreshing"]
}

PUT /shop/product/2
{
    "name": "Netural Toothpaste",
    "desc": "128g Rock salt whitening anti sensitive Toothpaste natural oral deeping cleaning",
    "price": 25,
    "producer": "Netural Producer",
    "tags": ["Whitening", "Refreshing", "Cleaning"]
}

PUT /shop/product/3
{
    "name": "Bamboo Toothpaste",
    "desc": "Bamboo Charcoal Toothpaste Fluoride Free 4 oz",
    "price": 40,
    "producer": "Bamboo Producer",
    "tags": ["Anti-Cavity", "Sensitive"]
}

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

2. 基本的聚合统计分析API

2.1 查询名称包含"Toothpaste"的商品并按照价格降序排序

代码语言:javascript
复制
GET /shop/product/_search
{
  "query": {
    "match": {
      "name": "Toothpaste"
    }
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}

{
  "took" : 55,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : null,
    "hits" : [
      {
        "_index" : "shop",
        "_type" : "product",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "name" : "Bamboo Toothpaste",
          "desc" : "Bamboo Charcoal Toothpaste Fluoride Free 4 oz",
          "price" : 40,
          "producer" : "Bamboo Producer",
          "tags" : [
            "Anti-Cavity",
            "Sensitive"
          ]
        },
        "sort" : [
          40
        ]
      },
      {
        "_index" : "shop",
        "_type" : "product",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "name" : "Charcoal Toothpaste",
          "desc" : "Travel-Friendly Daily Use Teeth Whitening Cleaning Activated Organic Charcoal Toothpaste",
          "price" : 30,
          "producer" : "Charcoal Producer",
          "tags" : [
            "Whitening",
            "Refreshing"
          ]
        },
        "sort" : [
          30
        ]
      },
      {
        "_index" : "shop",
        "_type" : "product",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "name" : "Netural Toothpaste",
          "desc" : "128g Rock salt whitening anti sensitive Toothpaste natural oral deeping cleaning",
          "price" : 25,
          "producer" : "Netural Producer",
          "tags" : [
            "Whitening",
            "Refreshing",
            "Cleaning"
          ]
        },
        "sort" : [
          25
        ]
      }
    ]
  }
}

2.2 分页查询,每页显示1条数据,查询第2页,并且只查询名称和价格字段

代码语言:javascript
复制
# 分页下标从0开始,document并非按照id排序
GET /shop/product/_search
{
  "query": {
    "match_all": {}
  },
  "from": 1,
  "size": 1,
  "_source": ["name", "price"]
}

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "shop",
        "_type" : "product",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "price" : 30,
          "name" : "Charcoal Toothpaste"
        }
      }
    ]
  }
}

2.3 搜索商品名称包含"Toothpaste"并且售价大于30的商品

代码语言:javascript
复制
GET /shop/product/_search
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "name": "Toothpaste"
        }
      },
      "filter": {
        "range": {
          "price": {
            "gt": 30
          }
        }
      }
    }
  }
}

{
  "took" : 15,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "shop",
        "_type" : "product",
        "_id" : "3",
        "_score" : 0.2876821,
        "_source" : {
          "name" : "Bamboo Toothpaste",
          "desc" : "Bamboo Charcoal Toothpaste Fluoride Free 4 oz",
          "price" : 40,
          "producer" : "Bamboo Producer",
          "tags" : [
            "Anti-Cavity",
            "Sensitive"
          ]
        }
      }
    ]
  }
}

2.4 高亮搜索

代码语言:javascript
复制
GET /shop/product/_search
{
  "query": {
    "match": {
      "name": "Bamboo"
    }
  },
  "highlight": {
    "fields": {
      "name": {}
    }
  }
}

{
  "took" : 51,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "shop",
        "_type" : "product",
        "_id" : "3",
        "_score" : 0.2876821,
        "_source" : {
          "name" : "Bamboo Toothpaste",
          "desc" : "Bamboo Charcoal Toothpaste Fluoride Free 4 oz",
          "price" : 40,
          "producer" : "Bamboo Producer",
          "tags" : [
            "Anti-Cavity",
            "Sensitive"
          ]
        },
        "highlight" : {
          "name" : [
            "<em>Bamboo</em> Toothpaste"
          ]
        }
      }
    ]
  }
}

2.5 计算每个tag下的商品数量

代码语言:javascript
复制
GET /shop/product/_search
{
  "aggs": {
    "products_per_tag": {
      "terms": {
        "field": "tags"
      }
    }
  },
  "size": 0 # 这个代表只返回聚合结果而不返回每个Document记录
}

{
  "took" : 38,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "products_per_tag" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "refreshing",
          "doc_count" : 2
        },
        {
          "key" : "whitening",
          "doc_count" : 2
        },
        {
          "key" : "anti",
          "doc_count" : 1
        },
        {
          "key" : "cavity",
          "doc_count" : 1
        },
        {
          "key" : "cleaning",
          "doc_count" : 1
        },
        {
          "key" : "sensitive",
          "doc_count" : 1
        }
      ]
    }
  }
}

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

代码语言:javascript
复制
GET /shop/product/_search
{
  "query": {
    "match": {
      "name": "Toothpaste"
    }
  }, 
  "aggs": {
    "products_per_tag": {
      "terms": {
        "field": "tags"
      }
    }
  },
  "size": 0
}

# 结果与上述结果是一样的

2.7 查询每个tag的商品的平均价格

代码语言:javascript
复制
GET /shop/product/_search
{
  "aggs": {
    "group_by_tag": {
      "terms": {
        "field": "tags"
      },
      "aggs": {
        "avg_price_per_tag": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  },
  "size": 0
}

{
  "took" : 11,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "group_by_tag" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "refreshing",
          "doc_count" : 2,
          "avg_price_per_tag" : {
            "value" : 27.5
          }
        },
        {
          "key" : "whitening",
          "doc_count" : 2,
          "avg_price_per_tag" : {
            "value" : 27.5
          }
        },
        {
          "key" : "anti",
          "doc_count" : 1,
          "avg_price_per_tag" : {
            "value" : 40.0
          }
        },
        {
          "key" : "cavity",
          "doc_count" : 1,
          "avg_price_per_tag" : {
            "value" : 40.0
          }
        },
        {
          "key" : "cleaning",
          "doc_count" : 1,
          "avg_price_per_tag" : {
            "value" : 25.0
          }
        },
        {
          "key" : "sensitive",
          "doc_count" : 1,
          "avg_price_per_tag" : {
            "value" : 40.0
          }
        }
      ]
    }
  }
}

2.8 查询每个tag的商品的平均价格,并且按照平均价格降序排序

代码语言:javascript
复制
GET /shop/product/_search
{
  "aggs": {
    "group_by_tag": {
      "terms": {
        "field": "tags",
        "order": {
          "avg_price_per_tag": "desc"
        }
      },
      "aggs": {
        "avg_price_per_tag": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  },
  "size": 0
}

{
  "took" : 14,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "group_by_tag" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "anti",
          "doc_count" : 1,
          "avg_price_per_tag" : {
            "value" : 40.0
          }
        },
        {
          "key" : "cavity",
          "doc_count" : 1,
          "avg_price_per_tag" : {
            "value" : 40.0
          }
        },
        {
          "key" : "sensitive",
          "doc_count" : 1,
          "avg_price_per_tag" : {
            "value" : 40.0
          }
        },
        {
          "key" : "refreshing",
          "doc_count" : 2,
          "avg_price_per_tag" : {
            "value" : 27.5
          }
        },
        {
          "key" : "whitening",
          "doc_count" : 2,
          "avg_price_per_tag" : {
            "value" : 27.5
          }
        },
        {
          "key" : "cleaning",
          "doc_count" : 1,
          "avg_price_per_tag" : {
            "value" : 25.0
          }
        }
      ]
    }
  }
}

2.9 按照指定的价格范围进行分组,然后在每个分组内按照tag进行分组,然后统计每组的平均价格

代码语言:javascript
复制
GET /shop/product/_search
{
  "aggs": {
    "group_by_price_range": {
      "range": {
        "field": "price",
        "ranges": [
          {
            "from": 0,
            "to": 30
          },
          {
            "from": 30,
            "to": 60
          }
        ]
      },
      "aggs": {
        "group_by_tag": {
          "terms": {
            "field": "tags"
          },
          "aggs": {
            "avg_price": {
              "avg": {
                "field": "price"
              }
            }
          }
        }
      }
    }
  },
  "size": 0
}

{
  "took" : 13,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "group_by_price_range" : {
      "buckets" : [
        {
          "key" : "0.0-30.0",
          "from" : 0.0,
          "to" : 30.0,
          "doc_count" : 1,
          "group_by_tag" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "cleaning",
                "doc_count" : 1,
                "avg_price" : {
                  "value" : 25.0
                }
              },
              {
                "key" : "refreshing",
                "doc_count" : 1,
                "avg_price" : {
                  "value" : 25.0
                }
              },
              {
                "key" : "whitening",
                "doc_count" : 1,
                "avg_price" : {
                  "value" : 25.0
                }
              }
            ]
          }
        },
        {
          "key" : "30.0-60.0",
          "from" : 30.0,
          "to" : 60.0,
          "doc_count" : 2,
          "group_by_tag" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "anti",
                "doc_count" : 1,
                "avg_price" : {
                  "value" : 40.0
                }
              },
              {
                "key" : "cavity",
                "doc_count" : 1,
                "avg_price" : {
                  "value" : 40.0
                }
              },
              {
                "key" : "refreshing",
                "doc_count" : 1,
                "avg_price" : {
                  "value" : 30.0
                }
              },
              {
                "key" : "sensitive",
                "doc_count" : 1,
                "avg_price" : {
                  "value" : 40.0
                }
              },
              {
                "key" : "whitening",
                "doc_count" : 1,
                "avg_price" : {
                  "value" : 30.0
                }
              }
            ]
          }
        }
      ]
    }
  }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 准备测试数据
  • 2. 基本的聚合统计分析API
    • 2.1 查询名称包含"Toothpaste"的商品并按照价格降序排序
      • 2.2 分页查询,每页显示1条数据,查询第2页,并且只查询名称和价格字段
        • 2.3 搜索商品名称包含"Toothpaste"并且售价大于30的商品
          • 2.4 高亮搜索
            • 2.5 计算每个tag下的商品数量
              • 2.6 对名称中包含"Toothpaste"的商品,计算每个tag下的商品数量
                • 2.7 查询每个tag的商品的平均价格
                  • 2.8 查询每个tag的商品的平均价格,并且按照平均价格降序排序
                    • 2.9 按照指定的价格范围进行分组,然后在每个分组内按照tag进行分组,然后统计每组的平均价格
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档