新增索引:
# 新增索引
PUT http://192.168.80.121:9200/cars
# 请求参数
{
"settings": {
"number_of_shards": 2,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"color": {
"type": "keyword"
},
"make": {
"type": "keyword"
},
"price": {
"type": "float"
},
"sold": {
"type": "keyword"
}
}
}
}
批量新增数:
参考:《使用Http请求实现数据的批量导入》
# 批量导入数
POST http://192.168.80.121:9200/cars/_bulk
# 注意:必须换行
{"index": {"_index": "cars", "_type": "_doc", "_id": 1}}
{ "price" : 10000, "color" : "red", "make" : "honda", "sold" : "2022-10-28" }
{"index": {"_index": "cars", "_type": "_doc", "_id": 2}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2022-11-05" }
{"index": {"_index": "cars", "_type": "_doc", "_id": 3}}
{ "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2022-05-18" }
{"index": {"_index": "cars", "_type": "_doc", "_id": 4}}
{ "price" : 15000, "color" : "blue", "make" : "toyota", "sold" : "2022-07-02" }
{"index": {"_index": "cars", "_type": "_doc", "_id": 5}}
{ "price" : 12000, "color" : "green", "make" : "toyota", "sold" : "2022-08-19" }
{"index": {"_index": "cars", "_type": "_doc", "_id": 6}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2022-11-05" }
{"index": {"_index": "cars", "_type": "_doc", "_id": 7}}
{ "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2022-01-01" }
{"index": {"_index": "cars", "_type": "_doc", "_id": 8}}
{ "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2022-02-12" }
验证数据:
# 验证
GET http://192.168.80.121:9200/cars/_search
注意:在ES中,需要进行聚合、排序、过滤的字段其处理方式比较特殊,因此不能被分词,设置文本类型为keyword。
基本数据格式如下
Elasticsearch中的聚合,包含多种类型,最常用的两种,一个叫桶,一个叫指标(度量):
桶(bucket)
桶的作用,是按照某种方式对数据进行分组(group by),每一组数据在ES中称为一个桶。
度量(metrics)
分组完成以后,我们一般会对组中的数据进行聚合运算,例如求平均值、最大、最小、求和等,这些在ES中称为度量
比较常用的一些度量聚合方式:
首先,我们按照 汽车的颜色color来划分桶
# 请求:GET http://192.168.80.121:9200/cars/_search
# 请求参数
{
# 查询条件可不要
"query": {
"range": {
"price": {
"gte": 1,
"lt": 20000
}
}
},
"size" : 0,
# 聚合处理
"aggs" : {
"popular_colors" : {
"terms" : {
# 分组字段
"field" : "color",
# 排序操作(非必须)
"order": {
"_key": "desc", #根据返回分组名称排序
"_count": "desc" #根据返回数量排序
}
}
}
}
}
请求参数说明:
结果:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 8,
"max_score": 0,
"hits": []
},
"aggregations": {
"popular_colors": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "red",
"doc_count": 4
},
{
"key": "blue",
"doc_count": 2
},
{
"key": "green",
"doc_count": 2
}
]
}
}
}
响应参数说明:
(分组)
聚合指标是指直接对所有数据进行聚合,不进行分组查询;聚合方式为:avg、max、min、sum 、stats 、percentiles
# 请求:GET http://192.168.80.121:9200/cars/_search
# 请求参数,比如计算`price`的平均值
{
"size" : 0,
"aggs":{
"priceAggs": {
"avg": {
"field": "price"
}
}
}
}
#响应
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 8,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"avg_price": {
"value": 26500.0
}
}
}
是指对某个字段进行分组后再进行聚合计算;聚合方式为:avg、max、min、sum 、stats 、percentiles
# 请求:GET http://192.168.80.121:9200/cars/_search
# 请求参数,统计各个汽车颜色的平均价格
{
"size" : 0,
"aggs" : {
"popularColors" : {
"terms" : {
"field" : "color"
},
"aggs":{
"priceAggs": {
"avg": {
"field": "price"
}
}
}
}
}
}
# 结果
请求参数说明:
响应结果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 8,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"populaColors": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "red",
"doc_count": 4,
"priceAggs": {
"value": 32500.0
}
},
{
"key": "blue",
"doc_count": 2,
"priceAggs": {
"value": 20000.0
}
},
{
"key": "green",
"doc_count": 2,
"priceAggs": {
"value": 21000.0
}
}
]
}
}
}
可以看到每个桶中都有自己的priceAggs字段,这是度量聚合(分组聚合计算)的结果
桶不仅可以嵌套运算, 还可以再嵌套其它桶。也就是说在每个分组中,再分更多组。
# 请求:GET http://192.168.80.121:9200/cars/_search
# 请求参数,比如:统计每种颜色的汽车中,分别属于哪些制造商,按照`make`字段再进行分桶
{
"size" : 0,
"aggs" : {
"popularColors" : {
"terms" : {
"field" : "color"
},
"aggs":{
"avgPrice": {
"avg": {
"field": "price"
}
},
"subMaker":{
"terms":{
"field":"make"
}
}
}
}
}
}
subMaker响应结果:
{
"aggregations": {
"popularColors": {
"buckets": [
{
"key": "red",
"doc_count": 4,
"subMaker": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "honda",
"doc_count": 3
},
{
"key": "bmw",
"doc_count": 1
}
]
},
"avgPrice": {
"value": 32500.0
}
},
{
"key": "blue",
"doc_count": 2,
"subMaker": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "ford",
"doc_count": 1
},
{
"key": "toyota",
"doc_count": 1
}
]
},
"avgPrice": {
"value": 20000.0
}
},
{
"key": "green",
"doc_count": 2,
"subMaker": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "ford",
"doc_count": 1
},
{
"key": "toyota",
"doc_count": 1
}
]
},
"avgPrice": {
"value": 21000.0
}
}
]
}
}
}
可以看到,新的聚合subMaker被嵌套在原来每一个color的桶中,如下图:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-t59RzUNb-1660574842886)(images/image-20220815222821845.png)]](https://1000bd.com/contentImg/2024/03/29/928b576563d07673.png)