目录
- 【5分钟学会一个知识点-探索现代搜索与分析引擎的魅力】01.Elasticsearch基本操作-增删改查
- 1.基本操作
- 1.1索引操作
- 1.2文档操作
- 1.3查询
- 1.4修改数据
- 1.5查询
- 1.5.1条件查询
- 1.5.1.1遍历所有的索引
- 1.5.1.2查询某个索引
- 1.5.1.3条件查询1:使用GET url传参数
- 1.5.1.4条件查询2:使用body传参
- 1.5.1.5body全量查询
- 1.5.1.6分页查询
- 1.5.1.7过滤一些不需要的字段
- 1.5.1.8排序
- 1.5.1.9多条件查询
- 1.5.1.10 查询结果高亮与完全匹配
- 1.5.1.11范围查询
- 1.5.1.12聚合函数
- 1.5.1.13加上size=0就可以只有聚合结果
- 1.5.1.14计算平均值
- 1.5.1.15映射关系
- 1.6别名
- 1.6.1 创建别名
🥰微信公众号:【给点知识】分享小知识,快速成长,欢迎关注呀!(底部点击二维码)
🥰学习宗旨:活到老,学到老。
😍写作宗旨:致力于改变现有文章难读难懂问题。
【5分钟学会一个知识点-探索现代搜索与分析引擎的魅力】01.Elasticsearch基本操作-增删改查
1.基本操作
1.1索引操作
- 创建索引
PUT /{index}/
- 查看某个索引的信息
GET /{index}/
- 查看全部的索引
GET /_cat/indices
- 删除索引
DELETE /{index}
1.2文档操作
- 文档添加:使用POST方法,因为POST不是幂等性的,每次请求,都会创建不同的id所以,需要使用非幂等的方法
POST /{index}/_doc
{
"price":6999,
"name":"mac1.1.0",
"notes":"苹果mac大陆版本 6+64G"
}
// 输出结果
{
"_index": "computer",
"_id": "isICNI8B0eUrClfT84k3",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
- 文档添加:当指定id的时候就可以使用PUT请求,因为id一定,那么只能创建一条数据
PUT /{index}/_doc/{id}
PUT /computer/_doc/001
或者
PUT /computer/_create/001
{
"price":6999,
"name":"mac1.2.0",
"notes":"苹果mac大陆版本 6+128G"
}
// 输出结果
{
"_index": "computer",
"_id": "001",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
1.3查询
- 查询所有的数据
GET /{index}/_search
- 主键查询
GET /{index}/_doc/{id}/
1.4修改数据
- 修改是根据文档id修改,如果存在就修改否则就新增
PUT修改是全量的更新
PUT /{index}/_doc/{id}
body:
{
"price":6999,
"name":"mac1.2.0",
"notes":"苹果mac大陆版本 6+128G"
}
# 输出结果如下
返回结果:例如下面的result显示的是upadted
{
"_index": "computer",
"_id": "1001",
"_version": 2,
"result": "updated", // 这里显示结果是updated
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 5,
"_primary_term": 3
}
如果传入参数如下,就会造成,传入几个字段就更新几个字段,因为PUT是幂等的
PUT /{index}/_doc/{id}
POST /{index}/_doc/{id}
body:
{
"price":2999
}
# 输出结果如下只有一个字段了
{
"_index": "computer",
"_id": "1001",
"_score": 1.0,
"_source": {
"price": 2999
}
}
以上是因为 PUT是全量更新,为了达到局部更新,这里可以使用POST实现
2. POST实现局部更新
POST /{index}/_update/{id}
{
"doc":{
"price":2999,
"name":"mac1.2.0",
"notes":"苹果mac大陆版本 6+128G"
}
}
如果增加了一个字段,那么直接增加也会出现一个字段
POST /{index}/_update/{id}
{
"doc":{
"price":2999,
"name":"mac1.2.0",
"notes":"苹果mac大陆版本 6+128G",
"text":"text"
}
}
# 输出结果
"hits": [
{
"_index": "computer",
"_id": "1001",
"_score": 1.0,
"_source": {
"price": 2999,
"name": "mac1.2.0",
"notes": "苹果mac大陆版本 6+128G",
"text": "text"
}
}
🥰微信公众号:【给点知识】分享小知识,快速成长,欢迎关注呀!(底部点击二维码)
🥰学习宗旨:活到老,学到老。
😍写作宗旨:致力于改变现有文章难读难懂问题。
1.5查询
1.5.1条件查询
1.5.1.1遍历所有的索引
GET /_search
1.5.1.2查询某个索引
GET /{index}/_search
1.5.1.3条件查询1:使用GET url传参数
GET /{index}/_search?q=filed:xxx
eg:http://10.67.199.181:8080/iphone/_search?q=name:小米
1.5.1.4条件查询2:使用body传参
GET/POST /{index}/_search
{
"query":{ // 我要做个什么:查询
"match":{ // 什么查询:匹配
"name":"小米华为" // 查询那个字段以及对应字段的的值
}
}
}
# 查询索引
{
"took": 26,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 2.7901525,
"hits": [
{
"_index": "iphone",
"_id": "SSMWTo8B3uHRqn1-nWGc",
"_score": 2.7901525,
"_source": {
"price": 9999,
"name": "华为meta60",
"notes": "华为meta60 6+64G"
}
},
{
"_index": "iphone",
"_id": "SiMWTo8B3uHRqn1-xGER",
"_score": 1.7503443,
"_source": {
"price": 9999,
"name": "保时捷版本华为meta60",
"notes": "华为meta60 6+64G"
}
}
]
}
}
1.5.1.5body全量查询
GET/POST /{index}/_search
{
"query":{ // 我要做个什么:查询
"match_all":{ // 什么查询:全量匹配
}
}
}
1.5.1.6分页查询
GET/POST /{index}/_search
{
"query":{
"match_all":{
}
},
"from":0,// 开始
"size":2 // 每页个数
}
1.5.1.7过滤一些不需要的字段
```python
GET/POST /{index}/_search
{
"query":{
"match_all":{
}
},
"from":0,// 开始
"size":2, // 每页个数
"_source":["name"]
}
1.5.1.8排序
GET/POST /{index}/_search
{
"query":{
"match_all":{
}
},
"from":0,
"size":2,
"_source":["name"],
"sort":{
"price":{
"order":"desc"
}
}
}
1.5.1.9多条件查询
GET/POST /{index}/_search
1.或者条件:should
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "小米"
}
},
{
"match": {
"price": "1999"
}
}
]
}
}
}
2. 同时成立:must
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "小米"
}
},
{
"match": {
"price": "1999"
}
}
]
}
}
}
1.5.1.10 查询结果高亮与完全匹配
GET/POST /{index}/_search
{
"query":{
"match_phrase":{ // 完全不配
"name":"小米"
}
},
"hightlight":{ // 对那个字段进行高亮
"field":{
"name":{}
}
}
}
1.5.1.11范围查询
GET/POST /{index}/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "华为"
}
},
{
"match": {
"price": "2988"
}
}
],
"filter":{
"range":{
"price":{
"gt":"11009"
}
}
}
}
}
}
1.5.1.12聚合函数
GET/POST /{index}/_search
# body
{
"aggs":{
"price_agg":{
"terms":{
"field":"price"
}
}
}
}
# 返回结果
{
"took": 11,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "computer",
"_id": "1001",
"_score": 1.0,
"_source": {
"price": 2999,
"name": "mac1.2.0",
"notes": "苹果mac大陆版本 6+128G",
"text": "text"
}
},
{
"_index": "computer",
"_id": "isICNI8B0eUrClfT84k3",
"_score": 1.0,
"_source": {
"price": 6999,
"name": "mac1.1.0",
"notes": "苹果mac大陆版本 6+64G"
}
},
{
"_index": "computer",
"_id": "001",
"_score": 1.0,
"_source": {
"price": 6999,
"name": "mac1.2.0",
"notes": "苹果mac大陆版本 6+128G"
}
}
]
},
"aggregations": {
"price_agg": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 6999,
"doc_count": 2
},
{
"key": 2999,
"doc_count": 1
}
]
}
}
}
1.5.1.13加上size=0就可以只有聚合结果
GET/POST /{index}/_search
# body
{
"aggs":{
"price_agg":{
"terms":{
"field":"price"
}
}
},
"size":0
}
# 返回结果
{
"took": 40,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"price_agg": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 6999,
"doc_count": 2
},
{
"key": 2999,
"doc_count": 1
}
]
}
}
}
1.5.1.14计算平均值
GET/POST /{index}/_search
# body
{
"aggs":{
"price_avg":{
"avg":{
"field":"price"
}
}
},
"size":0
}
# 返回结果
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"price_agg": {
"value": 5665.666666666667
}
}
}
1.5.1.15映射关系
场景描述:有些查询时分词,有些不是,那么如何设置让那个字段可以分词或者不可以分词呢?
通过以下方式可以给每个字段设置所含有的属性
type:数据类型 keyword表示关键词必须全部匹配
index:是否可以被索引,true表示可以被索引
{
"properties":{
"name":{
"type":"text",
"index":true
},
"gender":{
"type":"keyword",
"index":true
},
"tel":{
"type":"keyword",
"index":false
}
}
}
- 创建索引
PUT /{index}
PUT /person
- 给索引设置属性
PUT /{index}/_mapping
{
"properties":{
"name":{
"type":"text",
"index":true
},
"gender":{
"type":"keyword",
"index":true
},
"tel":{
"type":"keyword",
"index":false
}
}
}
- 直接在创建索引的时候直接加入属性
PUT /my_index
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"price": {
"type": "integer"
},
"notes": {
"type": "text"
}
}
}
}
查询看效果:name是text类型,可以被分词查询.
POST /{index}/_search
// 请求体
{
"query":{
"match":{
"name":"里张"
}
}
}
// 返回结果
{
"took": 14,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.2613049,
"hits": [
{
"_index": "person",
"_id": "003",
"_score": 1.2613049,
"_source": {
"name": "里斯",
"gender": "男生",
"tel": "19889999999"
}
},
{
"_index": "person",
"_id": "004",
"_score": 1.2613049,
"_source": {
"name": "张三",
"gender": "男生",
"tel": "18888888888"
}
}
]
}
}
注意:如果想实现text不分词查询也可使用
{
"query":{
"match_phrase":{
"name":"里斯"
}
}
}
然后再查询其他字段
GET /{index}/_search
{
"query":{
"match":{
"gender":"男女"
}
}
}
// 返回结果
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 0,
"relation": "eq"
},
"max_score": null,
"hits": []
}
}
使用gender:男生。是可以查询的
GET /{index}/_search
{
"query":{
"match":{
"gender":"男生"
}
}
}
{
"took": 13,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0.6931471,
"hits": [
{
"_index": "person",
"_id": "003",
"_score": 0.6931471,
"_source": {
"name": "里斯",
"gender": "男生",
"tel": "19889999999"
}
},
{
"_index": "person",
"_id": "004",
"_score": 0.6931471,
"_source": {
"name": "张三",
"gender": "男生",
"tel": "18888888888"
}
}
]
}
}
8.x版本的是类型是text才可以设置index 是否可以被索引。测试发现当type=keyword时设置index无效。当为 text时是可以的。
{
"mappings":{
"properties":{
"name":{
"type":"text",
"index":true
},
"gender":{
"type":"keyword",
"index":false
},
"tel":{
"type":"text",
"index":false
}
}
}
}
使用tel查询的时候提示错误不能被查询
1.6别名
1.6.1 创建别名
一个别名可以对应多个索引,别名对应多个索引可以用于检索不同的内容。
比如有索引
index001、index002、iphone
{
"took": 47,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 13,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "index001",
"_id": "001",
"_score": 1.0,
"_source": {
"name": "法兰",
"tell": "19998888888",
"index001": "英国"
}
},
{
"_index": "index001",
"_id": "002",
"_score": 1.0,
"_source": {
"name": "里斯",
"tell": "17778888888",
"index001": "美国"
}
},
{
"_index": "index001",
"_id": "003",
"_score": 1.0,
"_source": {
"name": "张三",
"tell": "18888888888",
"index001": "中国"
}
},
{
"_index": "index002",
"_id": "001",
"_score": 1.0,
"_source": {
"name": "里斯2",
"tell": "18999998888",
"index002": "美国"
}
},
{
"_index": "index002",
"_id": "002",
"_score": 1.0,
"_source": {
"name": "青青",
"tell": "11111111111",
"index002": "英国"
}
},
{
"_index": "index002",
"_id": "003",
"_score": 1.0,
"_source": {
"name": "王五2",
"tell": "13333333336",
"index002": "中国"
}
},
{
"_index": "iphone",
"_id": "RyMWTo8B3uHRqn1-JGGx",
"_score": 1.0,
"_source": {
"price": 6999,
"name": "iphone15 pro",
"notes": "苹果15大陆版本 6+64G"
}
},
{
"_index": "iphone",
"_id": "SSMWTo8B3uHRqn1-nWGc",
"_score": 1.0,
"_source": {
"price": 9999,
"name": "华为meta60",
"notes": "华为meta60 6+64G"
}
},
{
"_index": "iphone",
"_id": "SiMWTo8B3uHRqn1-xGER",
"_score": 1.0,
"_source": {
"price": 9999,
"name": "保时捷版本华为meta60",
"notes": "华为meta60 6+64G"
}
},
{
"_index": "iphone",
"_id": "SyMXTo8B3uHRqn1-CGFr",
"_score": 1.0,
"_source": {
"price": 99,
"name": "三星盖乐世",
"notes": "三星盖乐世 6+64G"
}
}
]
}
}
创建别名
POST /_aliases
{
"actions": [
{
"add": {
"indices": ["index001", "index002", "iphone"],
"alias": "alias-index001-index002-iphone"
}
}
]
}
别名搜索:直接所有别名就可以直接搜索到对应的值
GET /{index}/_search
# 查询请求体
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "三星"
}
}
]
}
}
}
# 返回结果
{
"took": 255,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 1,
"skipped": 0,
"failed": 2,
"failures": [
{
"shard": 0,
"index": "index001",
"node": "W8VpzO2_RDSExCsKqgEG0w",
"reason": {
"type": "query_shard_exception",
"reason": "failed to create query: Cannot search on field [name] since it is not indexed.",
"index_uuid": "m_O1UmvdSRCIXgFipofDbw",
"index": "index001",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "Cannot search on field [name] since it is not indexed."
}
}
},
{
"shard": 0,
"index": "index002",
"node": "W8VpzO2_RDSExCsKqgEG0w",
"reason": {
"type": "query_shard_exception",
"reason": "failed to create query: Cannot search on field [name] since it is not indexed.",
"index_uuid": "mQynij2cTqmJVEQUxGGZBw",
"index": "index002",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "Cannot search on field [name] since it is not indexed."
}
}
}
]
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 3.1534967,
"hits": [
{
"_index": "iphone",
"_id": "SyMXTo8B3uHRqn1-CGFr",
"_score": 3.1534967,
"_source": {
"price": 99,
"name": "三星盖乐世",
"notes": "三星盖乐世 6+64G"
}
}
]
}
}