数据流 – Data Stream
# 查看数据流 logs-generic-device_raw_log,结果包含了当前指向的索引名 curl -X GET "http://192.168.3.222:9200/_data_stream/logs-generic-device_raw_log?pretty" # 查看索引模板 logs,上面数据流使用的索引是索引模板生成的,若索引模板匹配了多个数据流,则仅优先级高的索引模板才会生效 curl -X GET "http://192.168.3.222:9200/_index_template/logs?pretty" # 查看组件模板 my-component_template: curl -X GET "http://192.168.3.222:9200/_component_template/fee-component_template?pretty" # 删除数据流 curl -X DELETE "http://192.168.3.222:9200/_data_stream/logs-generic-device_raw_log"
索引模板 – Index Template
(1)创建用于索引模板的组件模板:
curl -X PUT "http://192.168.3.222:9200/_component_template/fee-component_template" -H 'Content-Type: application/json' -d '
{
"template": {
"mappings": {
"properties": {
"@timestamp": {
"type": "date"
},
"ecs": {
"type": "object",
"properties": {
"version": {
"ignore_above": 1024,
"type": "keyword"
}
}
},
"data_stream": {
"type": "object",
"properties": {
"namespace": {
"type": "constant_keyword"
},
"type": {
"type": "constant_keyword",
"value": "logs"
},
"dataset": {
"type": "constant_keyword"
}
}
},
"host": {
"index": true,
"store": false,
"type": "ip",
"doc_values": true
},
"message": {
"type": "text",
"analyzer": "whitespace"
}
}
}
}
}'

(2)创建索引模板,如果新数据流或索引与多个索引模板匹配,则使用优先级最高的索引模板。
注意:内置索引模板的优先级为 100,这里要大于100,否则数据流会优先使用默认索引模板logs。
curl -X PUT "http://192.168.3.222:9200/_index_template/fee" -H 'Content-Type: application/json' -d '
{
"priority": 101,
"index_patterns": [
"logs-*-*"
],
"data_stream": {},
"composed_of": [
"fee-component_template",
"logs-settings"
]
}'

(3)删除现有数据流,此时当前数据流下的索引也会立即删除
# 删除数据流 curl -X DELETE "http://192.168.3.222:9200/_data_stream/logs-generic-device_raw_log" # 查看索引,你会发现数据流的索引也被删除了 curl -X GET http://192.168.3.222:9200/_cat/indices
(4)向数据流插入一条数据后,新索引自动创建出来,此时新索引使用优先级更高的自定义索引模板创建,因此 message 字段上可以看到用了 whitespace 分词器了。
curl -X GET http://192.168.3.222:9200/.ds-logs-generic-device_raw_log-2023.07.13-000001/_mappings?pretty

索引
# 查看所有索引 curl -X GET http://192.168.3.222:9200/_cat/indices # 查看 .ds-logs 开头的索引的 settings curl -X GET http://192.168.3.222:9200/.ds-logs-generic-device_raw_log-2023.07.11-000001/_settings?pretty # 查看 .ds-logs 开头的索引的 mappings curl -X GET http://192.168.3.222:9200/.ds-logs-generic-device_raw_log-2023.07.11-000001/_mappings?pretty
分词器
使用默认分词器:
curl -X POST "http://192.168.3.222:9200/_analyze?pretty" -H 'Content-Type: application/json' -d '
{
"text": "The quick brown fox.大家好,我叫张三"
}'
使用 whitespace 分词器:
curl -X POST "http://192.168.3.222:9200/_analyze?pretty" -H 'Content-Type: application/json' -d '
{
"analyzer": "whitespace",
"text": "The quick brown fox.大家好,我叫张三"
}'
搜索
# term 搜索(完全匹配,也就是精确查询,搜索前不会再对搜索词进行分词拆解)
curl -X GET "192.168.3.222:9200/.ds-logs-generic-device_raw_log-2023.07.13-000001/_search?pretty&filter_path=took,hits.total,hits.hits._id,hits.hits._score,hits.hits._source" -H 'Content-Type: application/json' -d '
{
"query" : {
"term" : {
"message" : "name=tom"
}
}
}'
# match 搜索(与 term 搜索不同,会先将搜索词拆分,拆完后,再去匹配)
# message 字段使用的 whitespace 分词器,因此查询条件中可以用 = 号,不会被吃掉,默认分词器是会吃掉的。
# 原始数据中的 'name=tom' 被 whitespace 分词器分词了一个词,因此这里使用 term 查询可以查出来的,但用 'name=to' 就查不出来了
curl -X GET "192.168.3.222:9200/.ds-logs-generic-device_raw_log-2023.07.13-000001/_search?pretty&filter_path=took,hits.total,hits.hits._id,hits.hits._score,hits.hits._source" -H 'Content-Type: application/json' -d '
{
"query": {
"match": {
"message": "name=tom"
}
}
}'
# 短语搜索
curl -X GET "192.168.3.222:9200/.ds-logs-generic-device_raw_log-2023.07.13-000001/_search?pretty&filter_path=took,hits.total,hits.hits._id,hits.hits._score,hits.hits._source" -H 'Content-Type: application/json' -d '
{
"query" : {
"match_phrase" : {
"message" : "name=tom age=18"
}
}
}'
# 通配符与正则表达式查询
curl -X GET "192.168.3.222:9200/.ds-logs-generic-device_raw_log-2023.07.13-000001/_search?pretty&filter_path=took,hits.total,hits.hits._id,hits.hits._score,hits.hits._source" -H 'Content-Type: application/json' -d '
{
"query" : {
"wildcard" : {
"message" : "*sex*"
}
}
}'
重建索引
重建索引,或者说叫索引重命名,此功能要求在源数据上启用 _source,默认都是启用的,官方参考文档。
这里我使用数据流来演示,因此下面的 index 值都是数据流的名称。
# 第1步:创建用于重建数据流索引的数据流 logs-reindex-temp(对于重建数据流来说,这里名称要匹配已有的 Index Template 规则,其实也可以跳过这步,第2步若没有创建会自动创建的)
curl -X PUT http://192.168.3.224:9200/_data_stream/logs-reindex-temp
# 第2步:将数据 reindex 到临时数据流上(对于重建数据流来说,dest 处必须加 "op_type":"create")
curl -X POST "http://192.168.3.224:9200/_reindex?pretty" -H 'Content-Type: application/json' -d '
{
"source": {
"index": "logs-generic-device_raw_log"
},
"dest": {
"index": "logs-reindex-temp",
"op_type":"create"
}
}'
# 第3步:删除旧索引
curl -X DELETE "http://192.168.3.224:9200/_data_stream/logs-generic-device_raw_log"
# 第4步:将数据 reindex 到 logs-generic-device_raw_log 数据流上
curl -X POST "http://192.168.3.224:9200/_reindex?pretty" -H 'Content-Type: application/json' -d '
{
"source": {
"index": "logs-reindex-temp"
},
"dest": {
"index": "logs-generic-device_raw_log",
"op_type":"create"
}
}'
# 第5步:删除临时索引
curl -X DELETE "http://192.168.3.224:9200/_data_stream/logs-reindex-temp"