Day03-索引模板,DSL语句,集群迁移API,ES集群状态统计API,KQL语句及分片重路由API实战
- 1、索引模板
- 1.1 什么是索引模板
- 1.2 查看索引模板
- 1.3 创建/修改索引模板
- 1.4 删除索引模板
- 2、ES的DSL语句查询
- 2.1 什么是DSL
- 2.2 全文检索-match查询
- 2.3 精确匹配-match_phrase查询
- 2.4 全量查询-match_all查询
- 2.5 分页查询-size-form
- 2.6 使用"\_source"查看的指定字段
- 2.7 查询存在某个字段的文档-exists
- 2.8 语法高亮-highlight
- 2.9 基于字段进行排序查询-sort
- 2.10 多条件查询-bool
- 2.11 过滤查询(范围查询-filter)
- 2.12 精确匹配多个值-terms
- 2.13 多词搜索
- 2.14 权重案例
- 2.15 聚合查询
- 3、ES集群迁移实战
- 4、ES集群常用的API
- 4.1 ES集群健康状态API(health)
- 4.2 ES集群的设置及优先级(settings)
- 4.3 集群状态API(state)
- 4.4 集群统计API
- 4.5 查看集群的分片分配情况(allocation)
- 4.6 集群分片重路由API(reroute)
昨日内容回顾:
-
ES集群的常见术语
-
index
-
shard
-
replica
-
document
-
主分片和副本分片
- primary shard: read wirte (rw)
- replica shard: read only (ro)
-
-
索引的管理
- PUT http://10.0.0.101:9200/index_name
- POST http://10.0.0.101:9200/index_name/_settings
- DELETE http://10.0.0.101:9200/index_name
- GET http://10.0.0.101:9200/index_name
- GET http://10.0.0.101:9200/_cat/indices
- POST http://10.0.0.101:9200/index_name/_close
- POST http://10.0.0.101:9200/index_name/_open
- POST http://10.0.0.101:9200/_alias
-
文档的管理
- POST http://10.0.0.101:9200/index_name/_doc[/document_id]
- DELETE http://10.0.0.101:9200/index_name/_doc/document_id
- POST http://10.0.0.101:9200/index_name/_doc/document_id/_update
- GET http://10.0.0.101:9200/index_name/_doc/document_id
- GET http://10.0.0.101:9200/index_name/_search
-
文档的批量操作
- POST http://10.0.0.101:9200/_bulk
- POST http://10.0.0.101:9200/_mget
-
自定义数据类型:
- date
- ip
- keyword
- text
- double
- long
- …
-
中文分词器
- IK
- 支持自定义词典
-
kibana的部署
- 索引管理
- 开发工具使用
- 官方样例数据安装卸载
1、索引模板
1.1 什么是索引模板
指的是创建索引的一种方式。用户可以根据需求自定义对应的索引模板。
1.2 查看索引模板
(1)查看所有的索引模板
GET http://10.0.0.103:9200/_template
(2)查看单个索引模板
GET http://10.0.0.103:9200/_template/.monitoring-es
1.3 创建/修改索引模板
POST http://10.0.0.103:9200/_template/oldboyedu-linux85
{"aliases": {"DBA": {},"SRE": {},"K8S": {}},"index_patterns": ["oldboyedu-linux85*"],"settings": {"index": {"number_of_shards": 3,"number_of_replicas": 0}},"mappings": {"properties":{"ip_addr": {"type": "ip"},"access_time": {"type": "date"},"address": {"type" :"text"},"name": {"type": "keyword"}}}
}
1.4 删除索引模板
DELETE http://10.0.0.103:9200/_template/oldboyedu-linux85
2、ES的DSL语句查询
2.1 什么是DSL
Elasticsearch 提供了基于JSON的完整 Query DSL(Domain Specific Language,领域特定语言)来定义查询。
准备数据:
(1)创建索引添加映射关系
PUT http://10.0.0.101:9200/oldboyedu-linux85-shopping
{"mappings": {"properties": {"item": {"type": "text"},"title": {"type": "text"},"price": {"type": "double"},"type": {"type": "keyword"},"group": {"type": "long"},"auther": {"type": "text"},"birthday": {"type": "date","format": "yyyy-MM-dd"},"province": {"type": "keyword"},"city": {"type": "keyword"},"remote_ip": {"type": "ip"}}}
}
(2)导入数据
POST 10.0.0.101:9200/_bulk
参考"Linux85期商品收集作业.json"内容即可。
2.2 全文检索-match查询
(1)查询彭斌收集的数据
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"query":{"match":{"auther":"彭斌"}}
}
2.3 精确匹配-match_phrase查询
(1)查询张宇杰的数据
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"query":{"match_phrase":{"auther":"张宇杰"}}
}
2.4 全量查询-match_all查询
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"query": {"match_all": {}}
}
2.5 分页查询-size-form
了解原理
(1)每页显示3条数据,查询第四页
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"query":{"match_phrase":{"auther":"张宇杰"}},"size": 3,"from":9
}
(2)查询第六组数据,每页显示7条数据,查询第9页
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"query":{"match":{"group":6}},"size":7,"from": 56
}相关参数说明:size:指定每页显示多少条数据,默认值为10.from:指定跳过数据偏移量的大小,默认值为0,即默认看第一页。查询指定页码的from值 = "(页码 - 1) * 每页数据大小(size)"温馨提示:生产环境中,不建议深度分页,百度的页码数量控制在76页左右。
2.6 使用"_source"查看的指定字段
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"query":{"match_phrase":{"auther":"丘鸿彬"}},"_source":["title","auther","price"]
}
2.7 查询存在某个字段的文档-exists
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"query": {"exists" : {"field": "hobby"}}
}
2.8 语法高亮-highlight
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"query": {"match_phrase": {"title": "孙子兵法"}},"highlight": {"pre_tags": ["<span style='color:red;'>"],"post_tags": ["</span>"],"fields": {"title": {}}}
}相关参数说明:highlight:设置高亮。fields:指定对哪个字段进行语法高亮。pre_tags:自定义高亮的前缀标签。post_tags:自定义高亮的后缀标签。
2.9 基于字段进行排序查询-sort
01-升序查询最便宜商品及价格
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"query":{"match_phrase":{"auther":"于萌"}},"sort":{"price":{"order": "asc"}},"size":1
}
02-降序查询最贵的商品及价格
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"query":{"match_phrase":{"auther":"于萌"}},"sort":{"price":{"order": "desc"}},"size":1
}相关字段说明:sort:基于指定的字段进行排序。此处为指定的是"price"order:指定排序的规则,分为"asc"(升序)和"desc"(降序)。
2.10 多条件查询-bool
01-查看作者是于萌且商品价格为24.90
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"query": {"bool": {"must": [{"match_phrase": {"auther": "于萌"}},{"match": {"price": 24.90}}]}}
}
02-查看作者是于萌或者是高超的商品并降序排序
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"query": {"bool": {"should": [{"match_phrase": {"auther": "于萌"}},{"match_phrase": {"auther": "高超"}}]}},"sort":{"price":{"order": "desc"}}
}
03-查看作者是于萌或者是高超且商品价格为168或者198
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"query": {"bool": {"should": [{"match_phrase": {"auther": "于萌"}},{"match_phrase": {"auther": "高超"}},{"match": {"price": 168.00}},{"match": {"price": 198.00}}],"minimum_should_match": "60%"}}
}
04-查看作者不是于萌或者是高超且商品价格为168或者198的商品
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"query": {"bool": {"must_not": [{"match_phrase": {"auther": "于萌"}},{"match_phrase": {"auther": "高超"}}],"should": [{"match": {"price": 168.00}},{"match": {"price": 198.00}}],"minimum_should_match": 1}}
}
05-综合案例
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"query": {"bool": {"must": [{"match_phrase": {"title": "零食"}}],"must_not": [{"match_phrase": {"auther": "于萌"}},{"match_phrase": {"auther": "高超"}}],"should": [{"match": {"price": 168.00}},{"match": {"price": 9.9}},{"match": {"price": 19.9}}],"minimum_should_match": 1}},"highlight": {"pre_tags": ["<span style='color:red;'>"],"post_tags": ["</span>"],"fields": {"title": {}}},"_source": ["title","price","auther"],"sort": {"price": {"order": "desc"}}
}
2.11 过滤查询(范围查询-filter)
01-查询3组成员产品价格3599到10500的商品的最便宜的3个
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"query": {"bool": {"must": [{"match": {"group": 3}}],"filter": {"range": {"price": {"gte": 3599,"lte": 10500}}}}},"sort": {"price": {"order": "asc"}},"size": 3
}
02-查询2,4,6这3个组的最贵的3个产品且不包含酒的商品
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"query":{"bool":{"must_not":[{"match":{"title": "酒"}}],"should": [{"match":{"group":2}},{"match":{"group":4}},{"match":{"group":6}}]}},"sort":{"price":{"order": "desc"}},"size":3
}
2.12 精确匹配多个值-terms
01-查询商品价格为9.9和19.8的商品
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"query": {"terms": {"price": [9.9,19.9]}}
}
2.13 多词搜索
01-多词搜索包含"小面包"关键字的所有商品
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"query": {"bool": {"must": [{"match": {"title": {"query": "小面包","operator": "and"}}}]}},"highlight": {"pre_tags": ["<h1>"],"post_tags": ["</h1>"],"fields": {"title": {}}}
}
2.14 权重案例
01-权重案例
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"query": {"bool": {"must": [{"match": {"title": {"query": "小面包","operator": "and"}}}],"should": [{"match": {"title": {"query": "下午茶","boost": 5}}},{"match": {"title": {"query": "良品铺子","boost": 2}}}]}},"highlight": {"pre_tags": ["<h1>"],"post_tags": ["</h1>"],"fields": {"title": {}}}
}
2.15 聚合查询
01-统计每个组收集的商品数量
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"aggs": {"oldboyedu_group": {"terms": {"field": "group"}}},"size": 0
}
02-统计2组最贵的商品
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"query": {"match": {"group": 2}},"aggs": {"oldboyedu_max_shopping": {"max": {"field": "price"}}},"sort":{"price":{"order":"desc"}},"size": 1
}
03-统计3组最便宜的商品
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"query": {"match": {"group": 3}},"aggs": {"oldboyedu_min_shopping": {"min": {"field": "price"}}},"sort":{"price":{"order": "asc"}},"size": 1
}
04-统计4组商品的平均价格
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"query": {"match": {"group": 4}},"aggs": {"oldboyedu_avg_shopping": {"avg": {"field": "price"}}},"size": 0
}
05-统计买下5组所有商品要多少钱
GET 10.0.0.101:9200/oldboyedu-linux85-shopping/_search
{"query": {"match": {"group":5}},"aggs": {"oldboyedu_sum_shopping": {"sum": {"field": "price"}}},"size": 0
}
kibana的KQL语句
3、ES集群迁移实战
(1)搭建两套多实例集群
[root@elk101.oldboyedu.com ~]# curl 10.0.0.101:9200/_cat/nodes
10.0.0.102 57 88 1 0.09 0.08 0.06 cdfhilmrstw * elk102.oldboyedu.com
10.0.0.103 64 97 1 0.01 0.05 0.11 cdfhilmrstw - elk103.oldboyedu.com
10.0.0.101 71 97 0 0.00 0.01 0.05 cdfhilmrstw - elk101.oldboyedu.com[root@elk101.oldboyedu.com ~]# curl 10.0.0.101:19200/_cat/nodes
10.0.0.102 49 88 0 0.09 0.08 0.06 mdi - elk102.oldboyedu.com
10.0.0.101 51 97 0 0.00 0.01 0.05 mdi * elk101.oldboyedu.com
10.0.0.103 61 97 0 0.01 0.05 0.11 mdi - elk103.oldboyedu.com
(2)同集群迁移实战
POST http://10.0.0.103:9200/_reindex
{"source": {"index": "oldboyedu-linux85-shopping"},"dest": {"index": "oldboyedu-linux85-shopping-new"}
}
(3)不同集群迁移
3.1 elk101修改配置文件
vim /oldboyedu/softwares/es7/elasticsearch-7.17.5/config/elasticsearch.yml
...
# 添加如下一行代码,表示添加远程主机的白名单,用于数据迁移信任的主机。
reindex.remote.whitelist: "10.0.0.*:19200"推荐阅读https://www.elastic.co/guide/en/elasticsearch/reference/7.17/docs-reindex.html
3.2 elk101同步数据
data_rsync.sh /oldboyedu/softwares/es7/elasticsearch-7.17.5/config/elasticsearch.yml
3.3 所有节点重启ES7服务
systemctl restart es7
3.4 迁移数据
POST http://10.0.0.103:9200/_reindex
{"source": {"index": "oldboyedu-linux85-student","remote": {"host": "http://10.0.0.101:19200"},"query": {"bool": {"filter": {"range": {"age": {"gt": 25}}}}}},"dest": {"index": "oldboyedu-linux85-student-jiaoshi07"}
}
3.5 验证数据
GET/POST 10.0.0.101:9200/oldboyedu-linux85-student-jiaoshi07/_search
4、ES集群常用的API
4.1 ES集群健康状态API(health)
(1)安装jq工具
yum -y install epel-release
yum -y install jq
(2)测试取数据
curl http://10.0.0.103:9200/_cluster/health 2>/dev/null| jq
curl http://10.0.0.103:9200/_cluster/health 2>/dev/null| jq .status
curl http://10.0.0.103:9200/_cluster/health 2>/dev/null| jq .active_shards_percent_as_number
相关参数说明:
cluster_name集群的名称。
status集群的健康状态,基于其主分片和副本分片的状态。ES集群有以下三种状态:green所有分片都已分配。yellow所有主分片都已分配,但一个或多个副本分片未分配。如果集群中的某个节点发生故障,则在修复该节点之前,某些数据可能不可用。red一个或多个主分片未分配,因此某些数据不可用。这可能会在集群启动期间短暂发生,因为分配了主分片。timed_out是否在参数false指定的时间段内返回响应(默认情况下30秒)。number_of_nodes集群内的节点数。number_of_data_nodes作为专用数据节点的节点数。active_primary_shards可用主分片的数量。active_shards可用主分片和副本分片的总数。relocating_shards正在重定位的分片数。initializing_shards正在初始化的分片数。unassigned_shards未分配的分片数。delayed_unassigned_shards分配因超时设置而延迟的分片数。number_of_pending_tasks尚未执行的集群级别更改的数量。number_of_in_flight_fetch未完成的提取次数。task_max_waiting_in_queue_millis自最早启动的任务等待执行以来的时间(以毫秒为单位)。active_shards_percent_as_number集群中活动分片的比率,以百分比表示。
4.2 ES集群的设置及优先级(settings)
如果您使用多种方法配置相同的设置,Elasticsearch 会按以下优先顺序应用这些设置:
(1)Transient setting(临时配置,集群重启后失效)
(2)Persistent setting(持久化配置,集群重启后依旧生效)
(3)elasticsearch.yml setting(配置文件)
(4)Default setting value(默认设置值)
(1)查询集群的所有配置信息
GET http://10.0.0.103:9200/_cluster/settings?include_defaults=true&flat_settings=true
(2)修改集群的配置信息
PUT http://10.0.0.103:9200/_cluster/settings
{"transient": {"cluster.routing.allocation.enable": "none"}
}
相关参数说明:
"cluster.routing.allocation.enable":"all":允许所有分片类型进行分配。"primaries"仅允许分配主分片。"new_primaries"仅允许新创建索引分配主分片。"none":不允许分配任何类型的分配。参考链接:https://www.elastic.co/guide/en/elasticsearch/reference/7.17/cluster-get-settings.htmlhttps://www.elastic.co/guide/en/elasticsearch/reference/7.17/cluster-update-settings.html
4.3 集群状态API(state)
集群状态是一种内部数据结构,它跟踪每个节点所需的各种信息,包括:
(1)集群中其他节点的身份和属性
(2)集群范围的设置
(3)索引元数据,包括每个索引的映射和设置
(4)集群中每个分片副本的位置和状态
(1)查看集群的状态信息
GET http://10.0.0.103:9200/_cluster/state
(2)只查看节点信息。
GET http://10.0.0.103:9200/_cluster/state/nodes
(3)查看nodes,version,routing_table这些信息,并且查看以"oldboyedu*"开头的所有索引
http://10.0.0.103:9200/_cluster/state/nodes,version,routing_table/oldboyedu*推荐阅读:https://www.elastic.co/guide/en/elasticsearch/reference/7.17/cluster-state.html
4.4 集群统计API
Cluster Stats API 允许从集群范围的角度检索统计信息。返回基本索引指标(分片数量、存储大小、内存使用情况)和有关构成集群的当前节点的信息(数量、角色、操作系统、jvm 版本、内存使用情况、cpu 和已安装的插件)。
(1)查看统计信息
GET http://10.0.0.103:9200/_cluster/stats
推荐阅读:https://www.elastic.co/guide/en/elasticsearch/reference/7.17/cluster-stats.html
4.5 查看集群的分片分配情况(allocation)
集群分配解释API的目的是为集群中的分片分配提供解释。
对于未分配的分片,解释 API 提供了有关未分配分片的原因的解释。
对于分配的分片,解释 API 解释了为什么分片保留在其当前节点上并且没有移动或重新平衡到另一个节点。
当您尝试诊断分片未分配的原因或分片继续保留在其当前节点上的原因时,此 API 可能非常有用,而您可能会对此有所期待。
(1)分析teacher索引的0号分片未分配的原因。
GET http://10.0.0.101:9200/_cluster/allocation/explain
{"index": "teacher","shard": 0,"primary": true
}推荐阅读:https://www.elastic.co/guide/en/elasticsearch/reference/7.17/cluster-allocation-explain.html
4.6 集群分片重路由API(reroute)
reroute 命令允许手动更改集群中各个分片的分配。
例如,可以将分片从一个节点显式移动到另一个节点,可以取消分配,并且可以将未分配的分片显式分配给特定节点。
(1)将"oldboyedu-linux85-student-jiaoshi07"索引的0号分片从elk102节点移动到elk101节点。
POST http://10.0.0.101:9200/_cluster/reroute
{"commands": [{"move": {"index": "oldboyedu-linux85-student-jiaoshi07","shard": 0,"from_node": "elk102.oldboyedu.com","to_node": "elk101.oldboyedu.com"}}]
}
(2)取消副本分片的分配,其副本会重新初始化分配。
POST http://10.0.0.101:9200/_cluster/reroute
{"commands": [{"cancel": {"index": "oldboyedu-linux85-student-jiaoshi07","shard": 0,"node": "elk103.oldboyedu.com"}}]
}推荐阅读:https://www.elastic.co/guide/en/elasticsearch/reference/7.17/cluster-reroute.html
作业:
(1)完成课堂所有练习及思维导图;
(2)调研ES6迁移数据到ES7的解决方案;
扩展作业:
使用granfana展示ES数据。