不介绍安装方式,主要是用来记录在学习elasticsearch时的一些基础入门命令和CRUD操作。
以下的例子,都是经过测试的,本文的例子使用的时kibana工具来进行测试
1、检查elasticsearch是否启动成功
会返回当前elasticsearch的服务名称,版本号等相关信息
2、查看当前elasticsearch的状态信息
GET /_cat/health?v
会返回当前服务的状态索引,elasticsearch使用几个颜色状态来表示集群的状态
green:每个索引的primary shard 和replica shard 都是active
yellow:每个索引的primary shard都是active,但是部分replica shard处于不可用状态
red:不是所有的primary shard都是active 状态
Q:为什么我们本地服务状态是yellow的?
A:当前电脑上启动了一个es进程,相当于只有一个node。es当中只有一个index,是kibana内置的index。由于默认的配置是给每个index分配5个primary shard 和replica shard,而且primary shard 和 replica shard不能在同一台机器上。目前kibana自己创建的index是一个primary shard和一个replica shard 。而我们当前机器上只有一个node,所以只有一个primary shard 被分配了,replica shard 没有第二台机器去启动。
3、GET /_cat/indices/?v
查看当前机器上有哪些索引
基础信息的CRUD操作
1、创建商品信息 put命令,如果索引,type不存在,则创建
PUT /test_index1/test_type1/1
{
"name":"test",
"age":12,
"date":"2018-10-11"
}
返回结果:
{
"_index" : "test_index1",
"_type" : "test_type1",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
2、查询信息
使用get命令,根据index/type/id的方式,可以精确查询到具体的数据
GET /test_index1/test_type1/1
返回结果
{
"_index" : "test_index1",
"_type" : "test_type1",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"name" : "test",
"age" : 12,
"date" : "2018-10-11"
}
}
3、修改信息
使用put命令
PUT /test_index1/test_type1/1
{
"tags":"test1",
"brand":"no",
"price":12
}
执行结果:
{
"_index" : "test_index1",
"_type" : "test_type1",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
需要注意的是版本号加一,并且put命令会把原来的所有属性都替换掉,数据当中是当前显示的所有put当中的内容,在实际开发使用这个命令需要慎重,
4、只更新部分字段
post命令只更新部分字段,注意其格式
POST /test_index1/test_type1/1/_update
{
"doc": {
"name":"wozaiceshi"
}
}
---------返回结果---------
{
"_index" : "test_index1",
"_type" : "test_type1",
"_id" : "1",
"_version" : 3,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
5、删除命令
格式 delete /index/type/id
DELETE /test_index1/test_type1/1
--------------返回结果-----------
{
"_index" : "test_index1",
"_type" : "test_type1",
"_id" : "1",
"_version" : 4,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1
}
注意这里在删除时,版本号也是会加一,es并不会在此刻立即删除数据,后面会介绍es的删除逻辑
多种方式的查询
1、查询所有商品信息
格式 GET /index/type/_search
GET /ecommerce/product/_search
返回结果
{
"took" : 49,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.0,
"hits" : [
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "jiajieshi yagao",
"desc" : "youxiao fangzhu",
"price" : 25,
"producer" : "jiajieshi producer",
"tags" : [
"fangzhu"
]
}
},
对于search的返回结果
took:表示本次查询耗费的时间,49毫秒
timed_out:表示是否超时,false未超时
shards:表示分片信息,total表示总共5个分片,成功了5个
hits:表示的本次查询的结果信息,total表示查询总数,max_score:score的含义,就是document对于一个search的相关度的匹配分数,越相关,就越匹配,分数也高
如果想要在search的基础上进行条件的筛选,可以使用如下命令
GET /ecommerce/product/_search?q=name:yagao&sort=price:desc
这种方式的查询在实际开发当中使用的不多,主要是在进行相关数据的查询时,用于快速查询
2、复杂条件下的查询条件
查询所有信息
GET /ecommerce/product/_search
{
"query": { "match_all": {} }
}
结合一些条件的查询
GET /ecommerce/product/_search
{
"query" : {
"match" : {
"name" : "yagao"
}
},
"sort": [
{ "price": "desc" }
]
}
分页查询:
GET /ecommerce/product/_search
{
"query": { "match_all": {} },
"from": 1,
"size": 1
}
from 和size 类似于mysql当中的 limit m,n 查询
查询结果只返回特定的filed字段,添加_source,筛选列
GET /ecommerce/product/_search
{
"query": { "match_all": {} },
"_source": ["name", "price"]
}
3、query fileter
查询名称等于牙膏,并且价格大于25的商品信息,注意和上述只筛选名称的区别
GET /ecommerce/product/_search
{
"query" : {
"bool" : {
"must" : {
"match" : {
"name" : "yagao"
}
},
"filter" : {
"range" : {
"price" : { "gt" : 25 }
}
}
}
}
}
4、全文检索
GET /ecommerce/product/_search
{
"query" : {
"match" : {
"producer" : "yagao producer"
}
}
}
注意,producer这个字段会被拆解,建立倒排索引
对于上述的筛选条件,会被拆分成yagao producer ---> yagao和producer,进行索引匹配
5、短语检索
和上述全文检索不同的是,短语检索不会进行拆分,会对某个连续的字段进行检索
GET /ecommerce/product/_search
{
"query" : {
"match_phrase" : {
"producer" : "yagao producer"
}
}
}
------
为查询到数据
聚合搜索
聚合搜索的理解就是类似于mysql当中的group by ,已经count, avg函数的使用
1、根据tags进行分组查询
aggs是es的一个分组标签,group_by_tags是我自己定义的名称,terms是es的标签,
需要注意的是,在使用tags进行分组查询之前,需要设置这个tags的属性,将文本内容的fielddata设置为true,只有在开启之后,才能进行使用,否则会报错
PUT /ecommerce/_mapping/product
{
"properties": {
"tags": {
"type": "text",
"fielddata": true
}
}
}
以下截取了部分的分组信息
GET /ecommerce/product/_search
{
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
}
}
}
}
----------查询结果--------------
"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" : 1
},
{
"key" : "qingxin",
"doc_count" : 1
}
]
}
}
2、对于名称当中包含yahao的商品,按照tags进行过滤操作
即先进行条件过滤,在进行分组查询
GET /ecommerce/product/_search
{
"query": {
"match": {
"name": "yangao"
}
},
"aggs": {
"all_tags": {
"terms": {
"field": "tags"
}
}
}
}
----------返回结果
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "2",
"_score" : 0.2876821,
"_source" : {
"name" : "jiajieshi yagao",
"desc" : "youxiao fangzhu",
"price" : 25,
"producer" : "jiajieshi producer",
"tags" : [
"fangzhu"
]
}
},
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name" : "gaolujie yagao",
"desc" : "gaoxiao meibai",
"price" : 30,
"producer" : "gaolujie producer",
"tags" : [
"meibai",
"fangzhu"
]
}
},
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "3",
"_score" : 0.2876821,
"_source" : {
"name" : "zhonghua yagao",
"desc" : "caoben zhiwu",
"price" : 40,
"producer" : "zhonghua producer",
"tags" : [
"qingxin"
]
}
}
]
},
"aggregations" : {
"all_tags" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "fangzhu",
"doc_count" : 2
},
{
"key" : "meibai",
"doc_count" : 1
},
{
"key" : "qingxin",
"doc_count" : 1
}
]
}
}
}
3、先进行分组,再计算每个分组下的商品平均价格
es提供了avg函数来计算平均值
GET /ecommerce/product/_search
{
"aggs": {
"group_by_terms": {
"terms": {
"field": "tags"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
--------结果---------------
{
"took" : 47,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.0,
"hits" : [
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "jiajieshi yagao",
"desc" : "youxiao fangzhu",
"price" : 25,
"producer" : "jiajieshi producer",
"tags" : [
"fangzhu"
]
}
},
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "gaolujie yagao",
"desc" : "gaoxiao meibai",
"price" : 30,
"producer" : "gaolujie producer",
"tags" : [
"meibai",
"fangzhu"
]
}
},
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "zhonghua yagao",
"desc" : "caoben zhiwu",
"price" : 40,
"producer" : "zhonghua producer",
"tags" : [
"qingxin"
]
}
}
]
},
"aggregations" : {
"group_by_terms" : {
"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" : 1,
"avg_price" : {
"value" : 30.0
}
},
{
"key" : "qingxin",
"doc_count" : 1,
"avg_price" : {
"value" : 40.0
}
}
]
}
}
}
4、计算每个tags下的商品平均价格,并按照价格进行降序排序
GET /ecommerce/product/_search
{
"aggs": {
"group_by_terms": {
"terms": {
"field": "tags",
"order": {
"avg_price": "desc"
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
--------返回结果------------
{
"took" : 12,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.0,
"hits" : [
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "jiajieshi yagao",
"desc" : "youxiao fangzhu",
"price" : 25,
"producer" : "jiajieshi producer",
"tags" : [
"fangzhu"
]
}
},
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "gaolujie yagao",
"desc" : "gaoxiao meibai",
"price" : 30,
"producer" : "gaolujie producer",
"tags" : [
"meibai",
"fangzhu"
]
}
},
{
"_index" : "ecommerce",
"_type" : "product",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "zhonghua yagao",
"desc" : "caoben zhiwu",
"price" : 40,
"producer" : "zhonghua producer",
"tags" : [
"qingxin"
]
}
}
]
},
"aggregations" : {
"group_by_terms" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "qingxin",
"doc_count" : 1,
"avg_price" : {
"value" : 40.0
}
},
{
"key" : "meibai",
"doc_count" : 1,
"avg_price" : {
"value" : 30.0
}
},
{
"key" : "fangzhu",
"doc_count" : 2,
"avg_price" : {
"value" : 27.5
}
}
]
}
}
}
5、先按照价格区间进行分组,组内数据按照tag进行分组,最后计算每组的价格
GET /ecommerce/product/_search
{
"aggs": {
"group_by_price": {
"range": {
"field": "price",
"ranges": [
{
"from": 0,
"to": 20
},
{
"from": 20,
"to":40
},
{
"from": 40,
"to":60
}
]
},
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
}
}