ElasticSearch学习02 | 字数总计: 2.7k | 阅读时长: 12分钟 | 阅读量:
ElasticSearch学习02 操作ElasticSearch 入口是直接用Kibana的Dev进行操作
安装kibana 直接下载安装kibana Kibana 7.10.0 | Elastic
编辑/config/kibana.yml
1 2 3 4 5 6 7 8 9 server.host: node1 server.port: 5601 elasticsearch.hosts: ["http://node1:9200" ]kibana.index: ".kibana" elasticsearch.username: "elastic" elasticsearch.password: "123456"
这里有个问题
通过keystore配置加密的用户名密码信息时候发现报错 FATAL Error: [config validation of [elasticsearch].password]: expected value of type [string] but got [number]
查证得知kibana官方不允许在加密时使用纯数字, 参考GitHub上这个issues:Elasticsearch password set via elasticsearch-setup-passwords not working for Kibana · Issue #55031 · elastic/kibana (github.com)
遂换成config明文配置
简单操作 进入http://node1:5601/
并登陆
进入Dev tools
获取所有 Index GET /_cat/indices?v
1 2 3 4 5 6 7 8 9 10 health status index uuid pri rep docs.count docs.deleted store.size pri.store.size green open .security-7 dlSUad_BT-OIUxadPsyBrw 1 1 53 0 401.3kb 212.6kb green open .apm-custom-link nJcicuQlTUqma-yjSIKCGg 1 1 0 0 416b 208b green open .kibana_task_manager_1 pjSAVxQPRtWS3YgW6y7LFg 1 1 6 771 327.4kb 141kb green open .apm-agent-configuration hqlRQ1nYQGatUxBH8_lzDw 1 1 0 0 416b 208b green open my-index-000002 n82uHoR8TrKeHriH7obgUw 3 2 0 0 1.8kb 624b green open .kibana_1 KSYlU-26RM2gIQjj87vUTg 1 1 47 11 6.4mb 2.1mb green open .kibana-event-log-7.10.2-000001 -3Uwz3erTNC1ftD-86vzEw 1 1 2 0 22.1kb 11kb green open my-index-000001 YxpCxYuPQ3mal4fq9pHXSw 1 1 0 0 416b 208b
创建第一个索引 PUT /my-index-000003
成功
1 2 3 4 5 { "acknowledged" : true , "shards_acknowledged" : true , "index" : "my-index-000003" }
失败(重复)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 { "error" : { "root_cause" : [ { "type" : "resource_already_exists_exception" , "reason" : "index [my-index-000001/YxpCxYuPQ3mal4fq9pHXSw] already exists" , "index_uuid" : "YxpCxYuPQ3mal4fq9pHXSw" , "index" : "my-index-000001" } ], "type" : "resource_already_exists_exception" , "reason" : "index [my-index-000001/YxpCxYuPQ3mal4fq9pHXSw] already exists" , "index_uuid" : "YxpCxYuPQ3mal4fq9pHXSw" , "index" : "my-index-000001" }, "status" : 400 }
删除索引 DELETE /my-index-000001
1 2 3 { "acknowledged" : true }
新增记录 向指定的 /Index/Type 发送 PUT 请求,就可以在 Index 里面新增一条记录。比如,向/accounts/person发送请求,就可以新增一条人员记录。
1 2 3 4 5 6 POST /my-index-000003 /_doc/4 jFk4IABLg4faJrdE1cs { "user" : "张三" , "title" : "工程师" , "desc" : "数据库管理" }
服务器返回的 JSON 对象,会给出 Index、Type、Id、Version 等信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 { "_index" : "my-index-000003" , "_type" : "_doc" , "_id" : "4jFk4IABLg4faJrdE1cs" , "_version" : 3 , "result" : "updated" , "_shards" : { "total" : 2 , "successful" : 2 , "failed" : 0 }, "_seq_no" : 2 , "_primary_term" : 1 }
如果不指定id会随机生成一个
更新记录 1 2 3 4 5 6 PUT /my-index-000003 /_doc/4 jFk4IABLg4faJrdE1cs { "user" : "李四" , "title" : "工程师" , "desc" : "数据库管理" }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 { "_index" : "my-index-000003" , "_type" : "_doc" , "_id" : "4jFk4IABLg4faJrdE1cs" , "_version" : 5 , "result" : "updated" , "_shards" : { "total" : 2 , "successful" : 2 , "failed" : 0 }, "_seq_no" : 4 , "_primary_term" : 1 }
查询全部记录 1 GET /my-index-000003/_search
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 { "took" : 883 , "timed_out" : false , "_shards" : { "total" : 1 , "successful" : 1 , "skipped" : 0 , "failed" : 0 }, "hits" : { "total" : { "value" : 1 , "relation" : "eq" }, "max_score" : 1.0 , "hits" : [ { "_index" : "my-index-000003" , "_type" : "_doc" , "_id" : "4jFk4IABLg4faJrdE1cs" , "_score" : 1.0 , "_source" : { "user" : "李四" , "title" : "工程师" , "desc" : "数据库管理" } } ] } }
上面代码中,返回结果的 took字段表示该操作的耗时(单位为毫秒),timed_out字段表示是否超时,hits字段表示命中的记录,里面子字段的含义如下。
total:返回记录数,本例是2条。 max_score:最高的匹配程度,本例是1.0。 hits:返回的记录组成的数组。
返回的记录中,每条记录都有一个_score字段,表示匹配的程序,默认是按照这个字段降序排列。
查询单个记录 1 GET /my-index-000003/_doc/4jFk4IABLg4faJrdE1cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 { "_index" : "my-index-000003" , "_type" : "_doc" , "_id" : "4jFk4IABLg4faJrdE1cs" , "_version" : 5 , "_seq_no" : 4 , "_primary_term" : 1 , "found" : true , "_source" : { "user" : "李四" , "title" : "工程师" , "desc" : "数据库管理" } }
如果 Id 不正确,就查不到数据,found字段就是false。
1 2 3 4 5 6 { "_index" : "my-index-000003" , "_type" : "_doc" , "_id" : "4jFk4IABLg4faJrdE1cs1" , "found" : false }
查看映射 1 GET /my-index-000003/_mapping
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 { "my-index-000003" : { "mappings" : { "properties" : { "desc" : { "type" : "text" , "fields" : { "keyword" : { "type" : "keyword" , "ignore_above" : 256 } } }, "title" : { "type" : "text" , "fields" : { "keyword" : { "type" : "keyword" , "ignore_above" : 256 } } }, "user" : { "type" : "text" , "fields" : { "keyword" : { "type" : "keyword" , "ignore_above" : 256 } } } } } } }
全文搜索 ES 的查询非常特别,使用自己的查询语法,要求 GET 请求带有数据体。
1 2 3 4 GET /my-index-000003 /_search { "query" : { "match" : { "desc" : "数据" }} }
上面代码使用?Match 查询,指定的匹配条件是desc字段里面包含”数据”这个词。返回结果如下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 { "took" : 3 , "timed_out" : false , "_shards" : { "total" : 1 , "successful" : 1 , "skipped" : 0 , "failed" : 0 }, "hits" : { "total" : { "value" : 1 , "relation" : "eq" }, "max_score" : 0.5753642 , "hits" : [ { "_index" : "my-index-000003" , "_type" : "_doc" , "_id" : "4jFk4IABLg4faJrdE1cs" , "_score" : 0.5753642 , "_source" : { "user" : "李四" , "title" : "工程师" , "desc" : "数据库管理" } } ] } }
Elastic 默认一次返回10条结果,可以通过size字段改变这个设置。
1 2 3 4 5 GET /my-index-000003 /_search { "query" : { "match" : { "desc" : "管理" }}, "size" : 1 }
上面代码指定,每次只返回一条结果。
还可以通过from字段,指定位移。
1 2 3 4 5 6 GET /my-index-000003 /_search { "query" : { "match" : { "desc" : "管理" }}, "from" : 1 , "size" : 1 }
逻辑运算 如果有多个搜索关键字, ESc认为它们是or关系。
1 2 3 4 5 6 GET /my-index-000003 /_search { "query" : { "match" : { "desc" : "软件 系统 管理" }} }
上面代码搜索的是软件 or 系统 or 管理。
如果要执行多个关键词的and搜索,必须使用布尔查询。
1 2 3 4 5 6 7 8 9 10 11 GET /my-index-000003 /_search { "query" : { "bool" : { "must" : [ { "match" : { "desc" : "数据" } }, { "match" : { "desc" : "管理" } } ] } } }
索引生命周期 ILM
:索引生命周期管理,即Manage the index lifecycle
。
使用ILM
应确保集群中的所有节点运行的是同一个版本,不然无法保证他们会按预期工作。
索引的生命周期有四个阶段:
Hot
:索引更新和查询很活跃。
Warm
:索引不再更新,但仍然有查询
Cold
:索引不再更新,只有很少的查询,而且查询速度也很慢
Delete
:索引不需要了,可以安全的删除
rollover 当索引满足一定条件之后,将不再写入数据,而是自动创建一个索引,所有的数据将写入新的索引。
使用滚动索引能够:
优化活跃索引,在高性能hot
节点上提升高接收速率。
优化warm
节点搜索性能。
将旧的、访问频率低的数据转移到成本低的cold
节点上。
通过删除整个索引,根据索引保留策略删除数据。
官方推荐使用data stream
数据流来管理时间序列数据。每个数据流都需要一个索引模板
,其中包括:
数据流的名称或通配符(*)模式。
数据流时间戳字段。该字段必须映射为date
或date_nanos
数据类型。并且包含在索引到该数据流的每个文档中。
当创建每一个索引时将应用索引模板的映射和设置。
数据流专为追加数据而设计,其中数据流名称可用作操作(读取、写入、翻转、收缩等)目标。如果需要更新数据,可以使用索引别名
来管理时间序列数据。
自动 rollover ILM
会根据你的配置:索引大小
、文档数量
、所在阶段
,当满足这些条件时,自动实现rollover
操作 创建一个索引策略 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 PUT _ilm/policy/my_policy { "policy": { "phases": { "hot": { "actions": { "rollover": { //rollover前距离索引的创建时间最大为7天 "max_age": "7d", //rollover前索引的最大大小不超过50G "max_size": "50G", //rollover前索引的最大文档数不超过1个(测试用) "max_docs": 1, } } }, "warm": { //rollover之后进入warm阶段的时间不小于30天 "min_age": "30d", "actions": { "forcemerge": { //强制分片merge到segment为1 "max_num_segments": 1 }, "shrink": { //收缩分片数为1 "number_of_shards": 1 }, "allocate": { //副本数为2 "number_of_replicas": 2 } } }, "cold": { //rollover之后进入cold阶段的时间不小于60天 "min_age": "60d", "actions": { "allocate": { "require": { //分配到cold 节点,ES可根据机器资源配置不同类型的节点 "type": "cold" } } } }, "delete": { //rollover之后进入cold阶段的时间不小于60天 "min_age": "90d", "actions": { "delete": {} } } } } }
创建一个索引模版,指定使用的索引策略 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 PUT _template/my_template { //模版匹配的索引名以"index-"开头 "index_patterns": ["myindex-*"], "settings": { //索引分片数为2 "number_of_shards":2 , //索引副本数为1 "number_of_replicas": 1, //索引使用的索引策略为my_policy "index.lifecycle.name": "full_policy", //索引rollover后切换的索引别名为 test-alias "index.lifecycle.rollover_alias": "myindex" } }
创建一个符合上述索引模版的索引 1 2 3 4 5 6 7 8 9 PUT index-000001 { "aliases": { "myindex":{ //别名为 myindex //允许索引被写入数据 "is_write_index": true } } }
当发生rollover时,老索引的别名myindex将被去掉,新创建的索引别名为myidex,同时索引名自动在索引名上自增,变为myindex-0002。
更新策略 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 PUT _ilm/policy/my_policy { "policy": { "phases": { "hot": { "actions": { "rollover": { "max_size": "25GB" } } }, "delete": { "min_age": "30d", "actions": { "delete": {} } } } } }
查看索引策略发现,每次更新索引策略的版本 都会增加。
切换索引使用的索引策略 1 2 3 4 PUT myindex/_settings { "lifecycle.name": "my_other_policy" }
查看设置
kibana可视化设置
可以方便快捷地对相关模块进行可视化设置