Day03-索引模板,DSL语句,集群迁移API,ES集群状态统计API,KQL语句及分片重路由API实战
昨日内容回顾:
-
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"]
}