Elasticsearch 之 HTTP 篇
HTTP 操作
http 调试工具准备,例如:postman、apifox、apipost…
curl 也可
1、索引(表)
创建 PUT
对比关系型数据库,创建【索引】就等同于创建【表】。
- 但在使用上,Elasticsearch 的 Index 更强大、更灵活,比如它是分布式的,可以自带分片、复制、副本等能力,MySQL 表通常没有这些。
向 ES 服务器发 PUT
请求:http://127.0.0.1:9200/shopping
- 请求后,服务器返回响应:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "shopping"
}
- 如果重复发 PUT 请求:http://127.0.0.1:9200/shopping 添加索引,会返回错误信息:
{
"error": {
"root_cause": [
{
"type": "resource_already_exists_exception",
"reason": "index [shopping/Z3ALzZdnQzGEJNoNSEGlvw] already exists",
"index_uuid": "Z3ALzZdnQzGEJNoNSEGlvw",
"index": "shopping"
}
],
"type": "resource_already_exists_exception",
"reason": "index [shopping/Z3ALzZdnQzGEJNoNSEGlvw] already exists",
"index_uuid": "Z3ALzZdnQzGEJNoNSEGlvw",
"index": "shopping"
},
"status": 400
}
查询 GET
查看所有索引
向 ES 服务器发 GET
请求:http://127.0.0.1:9200/_cat/indices?v
- 这里请求路径中的
_cat
表示查看的意思,indices
表示索引, - 所以整体含义就是查看当前 ES 服务器中的所有索引,就好像 MySQL 中的 show tables 的感觉,
- 服务器响应结果如下:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open shopping Z3ALzZdnQzGEJNoNSEGlvw 1 1 0 0 208b 208b
响应结果说明:
表头 | 含义 |
---|---|
health | 当前服务器健康状态:green(集群完整)、yellow(单点正常/集群不完整)、red(单点不正常) |
status | 索引打开、关闭状态 |
index | 索引名 |
uuid | 索引统一编号 |
pri | 主分片数量 |
rep | 副本数量 |
docs.count | 可用文档数量 |
docs.deleted | 文档删除状态(逻辑删除) |
store.size | 主分片和副本整体占空间大小 |
pri.store.size | 主分片占空间大小 |
查看单个索引
向 ES 服务器发 GET 请求:http://127.0.0.1:9200/shopping
- url 中
shopping
代表 索引名称
返回结果如下:
{
"shopping": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1744101764908",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "Z3ALzZdnQzGEJNoNSEGlvw",
"version": {
"created": "7080099"
},
"provided_name": "shopping"
}
}
}
}
删除 DELETE
向 ES 服务器发 DELETE
请求:http://127.0.0.1:9200/shopping
返回结果如下:
{
"acknowledged": true
}
2、文档(行)
这里的文档可以类比为关系型数据库中的表数据,添加的数据格式为 JSON 格式。
创建 POST
向 ES 服务器发 POST
请求:http://127.0.0.1:9200/shopping/_doc,请求体 JSON 内容为:
{
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
返回结果如下:
{
"_index": "shopping", //索引
"_type": "_doc", // 类型-文档
"_id": "aemnFJYBLUeT2LeerGCL", // 唯一标识,可以类比为 MySQL 中的主键,随机生成
"_version": 1, // 版本
"result": "created", // 结果,这里的 create 表示创建成功
"_shards": {
"total": 2, // 分片 - 总数
"successful": 1, // 分片 - 总数
"failed": 0 // 分片 - 总数
},
"_seq_no": 0,
"_primary_term": 1
}
⚠️注意:
- 上面的数据创建后,由于没有指定数据唯一性标识(ID),默认情况下, ES 服务器会随机生成一个。
如果想要自定义唯一性标识,需要在创建时指定:http://127.0.0.1:9200/shopping/_doc/1,请求体 JSON 内容为:
{
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
返回结果如下:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1",
"_version": 1, // 自定义唯一标识
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
主键查询 GET
- 查看文档时,需要指明文档的唯一性标识,类似于 MySQL 中数据的主键查询
- 向 ES 服务器发 GET 请求:http://127.0.0.1:9200/shopping/_doc/1
返回结果如下:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1",
"_version": 1,
"_seq_no": 1,
"_primary_term": 1,
"found": true,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
}
查找不存在的内容
- 向 ES 服务器发 GET 请求:http://127.0.0.1:9200/shopping/_doc/1001
返回结果如下:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"found": false
}
查询索引下所有数据 GET
- 相当于查询某张表下的所有数据
- 向 ES 服务器发 GET 请求:http://127.0.0.1:9200/shopping/_search
返回结果如下:
{
"took": 384,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "aemnFJYBLUeT2LeerGCL",
"_score": 1.0,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
}
]
}
}
修改 POST
全量修改
和新增文档一样,输入相同的 URL 地址请求,如果请求体变化,会将原有的数据内容覆盖
- 向 ES 服务器发 POST 请求:http://127.0.0.1:9200/shopping/_doc/1
请求体 JSON 内容为:
{
"title": "华为手机",
"category": "华为",
"images": "http://www.gulixueyuan.com/hw.jpg",
"price": 4999.00
}
修改成功后,服务器响应结果:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1",
"_version": 2,
"result": "updated", // updated 表示数据被更新
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 1
}
再次进行主键查询,结果如下:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1",
"_version": 2,
"_seq_no": 4,
"_primary_term": 1,
"found": true,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://www.gulixueyuan.com/hw.jpg",
"price": 4999.00
}
}
可以看到,修改成功。
局部修改
修改数据时,也可以只修改某一给条数据的局部信息
向 ES 服务器发 POST
请求:http://127.0.0.1:9200/shopping/_update/1
请求体 JSON 内容为:
{
"doc": {
"title": "小米手机",
"category": "小米"
}
}
删除 DELETE
删除一个文档不会立即从磁盘上移除,它只是被标记成已删除(逻辑删除)。
向 ES 服务器发 DELETE
请求:http://127.0.0.1:9200/shopping/_doc/1
返回结果:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1", // 对应删除标识
"_version": 4,
"result": "deleted", // 表示删除成功
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 6,
"_primary_term": 2
}
向 ES 服务器发 GET请求:http://127.0.0.1:9200/shopping/_doc/1,查看是否删除成功:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1",
"found": false
}
3、映射操作(表结构)
先创建一个索引:
- 向 ES 服务器发
PUT
请求:http://127.0.0.1:9200/user
创建映射 PUT
- 向 ES 服务器发
PUT
请求:http://127.0.0.1:9200/user/_mapping - 附带 JSON 体如下
{
"properties": {
"name": {
"type": "text",
"index": true
},
"sex": {
"type": "keyword", // 关键词类型,查询的时候不会分词
"index": true
},
"tel": {
"type": "keyword",
"index": false // 字段不会被索引,不能用来搜索
}
}
}
映射数据说明:
- 字段名:任意填写,下面指定许多属性,例如:title、subtitle、images、price
type
:类型,Elasticsearch 中支持的数据类型非常丰富,说几个关键的:- String 类型,又分两种:
text
:可分词keyword
:不可分词,数据会作为完整字段进行匹配
- Numerical:数值类型,分两类
- 基本数据类型:
long
、integer
、short
、byte
、double
、float
、half_float
- 浮点数的高精度类型:
scaled_float
- 基本数据类型:
- Date:日期类型
- Array:数组类型
- Object:对象
- String 类型,又分两种:
index
:是否索引,默认为 true,也就是说你不进行任何配置,所有字段都会被索引- true:字段会被索引,则可以用来进行搜索
- false:字段不会被索引,不能用来搜索
store
:是否将数据进行独立存储,默认为 false- 原始的文本会存储在
source
里面,默认情况下其他提取出来的字段都不是独立存储
的,是从source
里面提取出来的 - 当然你也可以独立的存储某个字段,只要设置
"store":true
即可,获取独立存储的字段要比从_source
中解析快得多,但是也会占用更多的空间,所以要根据实际业务需求来设置
- 原始的文本会存储在
analyzer
:分词器,这里的 ik_max_word 即使用 ik 分词器
索引映射关联 PUT
- 创建索引(表)时并创建映射(表结构)
- 向 ES 服务器发
PUT
请求:http://127.0.0.1:9200/student - 附带 JSON 体如下
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"sex": {
"type": "keyword"
},
"tel": {
"type": "long",
"index": false
}
}
}
}
查询映射 GET
- 向 ES 服务器发
GET
请求:http://127.0.0.1:9200/user/_mapping
4、复杂查询
确保索引下有数据
-
先向索引(表)中新增多条测试数据
-
然后查询所有数据,向 ES 服务器发
GET
请求:http://127.0.0.1:9200/shopping/_search
{
"took": 661,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "psZeGZYBs2T-OKTlWq1C",
"_score": 1.0,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "p8ZeGZYBs2T-OKTloK3v",
"_score": 1.0,
"_source": {
"title": "红米手机",
"category": "红米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 1999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "qMZfGZYBs2T-OKTlDK1W",
"_score": 1.0,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 5999.00
}
}
]
}
}
URL 带参查询
查找 category 为小米的文档,
向 ES 服务器发 GET 请求:http://127.0.0.1:9200/shopping/_search?q=category:小米
,返回结果如下:
- 可以看出来,会自动进行分词查询,即会查出
category
包含小
或米
的所有数据
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 2.5700645,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "psZeGZYBs2T-OKTlWq1C",
"_score": 2.5700645,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "p8ZeGZYBs2T-OKTloK3v",
"_score": 1.0296195,
"_source": {
"title": "红米手机",
"category": "红米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 1999.00
}
}
]
}
}
请求体带参查询
URL 带参数形式查询,这很容易让不善者心怀恶意,或者参数值出现中文会出现乱码情况。
为了避免这些情况,我们可用使用带 JSON 请求体请求进行查询。
接下带 JSON 请求体,还是查找 category 为小米的文档,
向 ES 服务器发 GET
请求:http://127.0.0.1:9200/shopping/_search,附带 JSON 体如下:
{
"query": {
"match": {
"category": "小米"
}
}
}
返回结果如下:
{
"took": 790,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 2.5700645,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "psZeGZYBs2T-OKTlWq1C",
"_score": 2.5700645,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "p8ZeGZYBs2T-OKTloK3v",
"_score": 1.0296195,
"_source": {
"title": "红米手机",
"category": "红米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 1999.00
}
}
]
}
}
查询所有文档(请求体)
查找所有文档内容,也可以这样,
向 ES 服务器发 GET
请求:http://127.0.0.1:9200/shopping/_search,附带 JSON 体如下:
- 不带也是同样的效果
{
"query": {
"match_all": {}
}
}
查询指定字段
如果你想查询指定字段,
向 ES 服务器发 GET
请求:http://127.0.0.1:9200/shopping/_search,附带 JSON 体如下:
- 比如,只查询
title
字段
{
"query": {
"match_all": {}
},
"_source": [
"title"
]
}
返回结果如下:
{
"took": 53,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "psZeGZYBs2T-OKTlWq1C",
"_score": 1.0,
"_source": {
"title": "小米手机"
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "p8ZeGZYBs2T-OKTloK3v",
"_score": 1.0,
"_source": {
"title": "红米手机"
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "qMZfGZYBs2T-OKTlDK1W",
"_score": 1.0,
"_source": {
"title": "华为手机"
}
}
]
}
}
分页查询
from
:当前页的起始索引,默认从 0 开始。from = (pageNum - 1) * size
size
:每页显示多少条
向 ES 服务器发 GET
请求:http://127.0.0.1:9200/shopping/_search,附带 JSON 体如下:
{
"query": {
"match_all": {}
},
"from": 0,
"size": 2
}
返回结果如下:
{
"took": 113,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "psZeGZYBs2T-OKTlWq1C",
"_score": 1.0,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "p8ZeGZYBs2T-OKTloK3v",
"_score": 1.0,
"_source": {
"title": "红米手机",
"category": "红米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 1999.00
}
}
]
}
}
排序查询
-
根据价格进行降序查询
-
向 ES 服务器发
GET
请求:http://127.0.0.1:9200/shopping/_search,附带 JSON 体如下:
{
"query": {
"match_all": {}
},
"sort": {
"price": {
"order": "desc"
}
}
}
返回结果如下:
{
"took": 298,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "qMZfGZYBs2T-OKTlDK1W",
"_score": null,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 5999.00
},
"sort": [
5999.0
]
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "psZeGZYBs2T-OKTlWq1C",
"_score": null,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
},
"sort": [
3999.0
]
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "p8ZeGZYBs2T-OKTloK3v",
"_score": null,
"_source": {
"title": "红米手机",
"category": "红米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 1999.00
},
"sort": [
1999.0
]
}
]
}
}
组合查询
bool
把各种其它查询通过must
(必须)、must not
(必须不)、should
(应该) 的方式进行组合
must 与查询
- 假设想找出小米牌子,价格为 3999 元的。(
must
相当于 sql 的and
) - 向 ES 服务器发
GET
请求:http://127.0.0.1:9200/shopping/_search,附带 JSON 体如下:
{
"query": {
"bool": {
"must": [
{
"match": {
"category": "小米"
}
},
{
"match": {
"price": 3999.00
}
}
]
}
}
}
返回结果如下:
{
"took": 119,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 2.89712,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "psZeGZYBs2T-OKTlWq1C",
"_score": 2.89712,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
}
]
}
}
should 或查询
- 假设想找出小米和华为牌子。(
should
相当于 sql 的or
) - 向 ES 服务器发
GET
请求:http://127.0.0.1:9200/shopping/_search,附带 JSON 体如下:
{
"query": {
"bool": {
"should": [
{
"match": {
"category": "小米"
}
},
{
"match": {
"category": "华为"
}
}
]
}
}
}
返回结果如下:
- 字段不是关键词类型,会分词查询
{
"took": 13,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.89712,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "psZeGZYBs2T-OKTlWq1C",
"_score": 1.89712,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "qMZfGZYBs2T-OKTlDK1W",
"_score": 1.3862942,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 5999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "p8ZeGZYBs2T-OKTloK3v",
"_score": 0.6931471,
"_source": {
"title": "红米手机",
"category": "红米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 1999.00
}
}
]
}
}
范围查询
range
查询找出那些落在指定区间内的数字或者时间。range
查询允许以下字符
操作符 | 说明 |
---|---|
gt | 大于 > |
gte | 大于等于 >= |
lt | 小于 < |
lte | 小于等于 <= |
-
假设想找出小米和华为的牌子,价格大于 4000 元的手机。
-
向 ES 服务器发
GET
请求:http://127.0.0.1:9200/shopping/_search,附带 JSON 体如下:
{
"query": {
"bool": {
"should": [
{
"match": {
"category": "小米"
}
},
{
"match": {
"category": "华为"
}
}
],
"filter": {
"range": {
"price": {
"gt": 4000
}
}
}
}
}
}
返回结果如下:
{
"took": 40,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.3862942,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "qMZfGZYBs2T-OKTlDK1W",
"_score": 1.3862942,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 5999.00
}
}
]
}
}
完全匹配
向 ES 服务器发 GET
请求:http://127.0.0.1:9200/shopping/_search,附带 JSON 体如下:
{
"query": {
"match_phrase": {
"category": "小米"
}
}
}
返回结果如下:
- 不会进行分词查询
{
"took": 65,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.89712,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "psZeGZYBs2T-OKTlWq1C",
"_score": 1.89712,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
}
]
}
}
高亮查询
向 ES 服务器发 GET
请求:http://127.0.0.1:9200/shopping/_search,附带 JSON 体如下:
{
"query": {
"match_phrase": {
"category": "米"
}
},
"highlight": {
"fields": {
"category": {} // 高亮这字段
}
}
}
返回结果如下:
{
"took": 403,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0.6931471,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "psZeGZYBs2T-OKTlWq1C",
"_score": 0.6931471,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
},
"highlight": {
"category": [
"小<em>米</em>"
]
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "p8ZeGZYBs2T-OKTloK3v",
"_score": 0.6931471,
"_source": {
"title": "红米手机",
"category": "红米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 1999.00
},
"highlight": {
"category": [
"红<em>米</em>"
]
}
}
]
}
}
聚合查询
分组查询示例
聚合允许使用者对 es 文档进行统计分析,类似与关系型数据库中的 group by
,当然还有很多其他的聚合,例如取最大值 max、平均值 avg 等等。
接下来按 price
字段进行分组:
-
新增个相同
price
的数据 -
向 ES 服务器发
GET
请求:http://127.0.0.1:9200/shopping/_search,附带 JSON 体如下:
{
"aggs": { //聚合操作
"price_group": { //名称,随意起名
"terms": { //分组
"field": "price" //分组字段
}
}
}
}
返回结果如下:
{
"took": 1309,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "psZeGZYBs2T-OKTlWq1C",
"_score": 1.0,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "p8ZeGZYBs2T-OKTloK3v",
"_score": 1.0,
"_source": {
"title": "红米手机",
"category": "红米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 1999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "qMZfGZYBs2T-OKTlDK1W",
"_score": 1.0,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 5999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "wcbjGZYBs2T-OKTlL62H",
"_score": 1.0,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
}
]
},
"aggregations": {
"price_group": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 3999.0,
"doc_count": 2
},
{
"key": 1999.0,
"doc_count": 1
},
{
"key": 5999.0,
"doc_count": 1
}
]
}
}
}
不返回原始数据写法:
上面返回结果会附带原始数据的。若不想要不附带原始数据的结果,可以加多个 size
筛选属性:
{
"aggs": {
"price_group": {
"terms": {
"field": "price"
}
}
},
"size": 0
}
返回结果如下:
{
"took": 10,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"price_group": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 3999.0,
"doc_count": 2
},
{
"key": 1999.0,
"doc_count": 1
},
{
"key": 5999.0,
"doc_count": 1
}
]
}
}
}
平均值查询示例
- 向 ES 服务器发
GET
请求:http://127.0.0.1:9200/shopping/_search,附带 JSON 体如下:
{
"aggs": {
"price_avg": { // 名称,随意起名
"avg": { // 求平均
"field": "price"
}
}
},
"size": 0
}
返回结果如下:
{
"took": 188,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"price_avg": {
"value": 3999.0
}
}
}