Elasticsearch 入门
主要概念与mysql对比
对于HTTP方法,具体作用
| HTTP方法 | 操作 | 说明 |
|---|---|---|
| GET | 查 | 获取请求对象的当前状态 |
| POST | 改 | 改变对象的当前状态 |
| PUT | 增 | 创建一个对象 |
| DELETE | 删 | 销毁对象 |
| HEAD | 请求获取对象的基础信息 |
增删改查
以下在Kibana的Dev Tools里执行
增
PUT /account/info/1 { "price": 10000, "color": "红色", "make": "汉兰达", "sold": "2014-10-28" }
删
删除id=4的记录
DELETE /account/info/1
删不了/settle/jingyi
删settle索引
DELETE /account
改
POST /account/info/1/_update { "doc": {"color": "黑色"} }
查
GET /account/int/1
批量插入
POST /account/info/_bulk { "index": {}} { "price" : 20000, "color" : "红色", "make" : "汉兰达", "sold" : "2014-11-05" } { "index": {}} { "price" : 30000, "color" : "绿色", "make" : "福特", "sold" : "2014-05-18" } { "index": {}} { "price" : 15000, "color" : "蓝色", "make" : "丰田", "sold" : "2014-07-02" } { "index": {}} { "price" : 12000, "color" : "绿色", "make" : "丰田", "sold" : "2014-08-19" } { "index": {}} { "price" : 20000, "color" : "红色", "make" : "汉兰达", "sold" : "2014-11-05" } { "index": {}} { "price" : 80000, "color" : "红色", "make" : "宝马", "sold" : "2014-01-01" } { "index": {}} { "price" : 25000, "color" : "蓝色", "make" : "福特", "sold" : "2014-02-12" }
怎么获取单条记录呢?
统计应用
统计每种颜色的数量
类似mysql语句: select color, count(*) from info group by color;
GET account/info/_search { "size": 0, 【3】 "aggs": { 【1】 "SalesNum": { 【2】 "terms": { 【4】 "field": "color.keyword", "size": 10 } } } }
注释
【1】:如果想要进行统计分析,统计代码需要写在aggs中,aggs是aggregations 的简称,也可以写作 aggregations。
【2】:是指定的列的名称,作用同SQLSERVER统计中as 重命名。
【3】:这里设置了返回值为0,因为这个查询不仅仅返回了我们的统计的内容,还返回了搜索结果的内容,这里我们并不需要搜索结果的内容,所以设置为0.
【4】:这里定义了桶的类型,如果需要不同的统计内容,这些需要使用不同的统计类型。
结果
{
"took" : 21,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 8,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"SalesNum" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "红色",
"doc_count" : 3
},
{
"key" : "绿色",
"doc_count" : 2
},
{
"key" : "蓝色",
"doc_count" : 2
},
{
"key" : "黑色",
"doc_count" : 1
}
]
}
}
}
统计每种颜色的平均价格
类似mysql: select color, avg(price) from info group by color;
GET account/info/_search { "size": 0, "aggs": { "s": { "terms": { "field": "color.keyword", "size": 10 }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } }
结果
{
"took" : 13,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 8,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"s" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "红色",
"doc_count" : 3,
"avg_price" : {
"value" : 40000.0
}
},
{
"key" : "绿色",
"doc_count" : 2,
"avg_price" : {
"value" : 21000.0
}
},
{
"key" : "蓝色",
"doc_count" : 2,
"avg_price" : {
"value" : 20000.0
}
},
{
"key" : "黑色",
"doc_count" : 1,
"avg_price" : {
"value" : 10000.0
}
}
]
}
}
}
统计每一个企业品牌的最低价格和最高价格
类似mysql 语句: select make, min(price), max(price) from info group by make;
GET account/info/_search { "size": 0 ,"aggs": { "make": { "terms": { "field": "make.keyword" }, "aggs": { "price_age": { "avg": { "field": "price" } }, "min_price": { "min": { "field": "price" } }, "max_price":{ "max": { "field": "price" } } } } } }
结果
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 8,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"make" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "汉兰达",
"doc_count" : 3,
"max_price" : {
"value" : 20000.0
},
"min_price" : {
"value" : 10000.0
},
"price_age" : {
"value" : 16666.666666666668
}
},
{
"key" : "丰田",
"doc_count" : 2,
"max_price" : {
"value" : 15000.0
},
"min_price" : {
"value" : 12000.0
},
"price_age" : {
"value" : 13500.0
}
},
{
"key" : "福特",
"doc_count" : 2,
"max_price" : {
"value" : 30000.0
},
"min_price" : {
"value" : 25000.0
},
"price_age" : {
"value" : 27500.0
}
},
{
"key" : "宝马",
"doc_count" : 1,
"max_price" : {
"value" : 80000.0
},
"min_price" : {
"value" : 80000.0
},
"price_age" : {
"value" : 80000.0
}
}
]
}
}
}

浙公网安备 33010602011771号