Elasticsearch 索引模板 Index Template, 并通过模板创建索引

ES version: 7.11

1. 索引模板

1.1 新建模板

Create or update index template API

curl -u"username:pwd" -XPUT '127.0.0.1:9200/_index_template/friend_add_log?pretty' -H'Content-Type: application/json' -d '
{
    "index_patterns": ["friend_add_log*"],
    "priority": 0,
    "template": {
        "settings": {
            "index": {
                "analysis": {
                    "normalizer": {
                        "my_lowercase": {
                            "type": "custom",
                            "filter": ["lowercase"]
                        }
                    }
                }
            },
            "number_of_shards": 3,
            "number_of_replicas": 0
        },
        "mappings": {
            "properties": {
                "self_im_id": {
                    "type": "keyword"
                },
                "target_im_id": {
                    "type": "keyword"
                },
                "create_time": {
                    "type": "date",
                    "format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
                }
            }
        },
        "aliases": {
            "friend_add_log": {}
        }
    }
}
'

1.2 查看模板是否存在

curl -u"username:pwd" -I '127.0.0.1:9200/_index_template/friend_add_log?pretty'

1.3 查看索引模板

Get index template API

curl -u"username:pwd" -XGET '127.0.0.1:9200/_index_template/friend_add_log?pretty'

1.4 删除模板

Delete index template API

# 从 ES 7.8 开始提供的新版API, 与旧版共存; 删除使用新版API创建的索引模板需要用新版API, 旧版与旧版对应. 两种版本的API不可混用;
curl -u"username:pwd" -X DELETE "127.0.0.1:9200/_index_template/friend_add_log?pretty"
# 旧版API
curl -u"username:pwd" -X DELETE "127.0.0.1:9200/_template/friend_add_log?pretty"

2. 创建索引

2.1 创建索引 friend_add_log_202112

curl -u"username:pwd" -XPUT '127.0.0.1:9200/friend_add_log_202112?pretty' -H 'Content-Type: application/json' -d '{}'

索引 friend_add_log_202112 会命中模板 friend_add_log, 因此会按照模板创建索引. 或者第一次 put 数据的时候, 如果索引不存在, 也会自动根据模板创建索引. 比如例子中的 friend_add_log_202112, 就可以根据月份滚动创建索引.

2.1 查看索引信息, 包括创建时间

curl -u"username:pwd" -XGET '127.0.0.1:9200/friend_add_log_202112?pretty'

 3. 批量构造数据

curl -u"username:pwd" -X POST '127.0.0.1:9200/friend_add_log_202112/_bulk?pretty' -H 'Content-Type: application/json' -d '
{"create":{}}
{"self_im_id":"wxid_d9u29", "target_im_id":"wxid_ldu2e", "create_time":"2021-12-07 17:12:08"}
{"create":{}}
{"self_im_id":"wxid_d9u29", "target_im_id":"wxid_djf7d", "create_time":"2021-12-07 23:10:23"}
{"create":{}}
{"self_im_id":"wxid_d9u29", "target_im_id":"wxid_ake23", "create_time":"2021-12-07 12:11:55"}
{"create":{}}
{"self_im_id":"wxid_hdf51", "target_im_id":"wxid_ldu2e", "create_time":"2021-12-07 19:15:12"}
{"create":{}}
{"self_im_id":"wxid_hdf51", "target_im_id":"wxid_djf7d", "create_time":"2021-12-07 09:16:16"}
{"create":{}}
{"self_im_id":"wxid_hdf51", "target_im_id":"wxid_d7fuv", "create_time":"2021-12-07 15:11:29"}
{"create":{}}
{"self_im_id":"wxid_rte11", "target_im_id":"wxid_d7fuv", "create_time":"2021-12-06 07:45:53"}
{"create":{}}
{"self_im_id":"wxid_rte11", "target_im_id":"wxid_d7fuv", "create_time":"2021-12-05 19:10:49"}
{"create":{}}
{"self_im_id":"wxid_rte11", "target_im_id":"wxid_d7fuv", "create_time":"2021-12-02 10:00:39"}
{"create":{}}
{"self_im_id":"wxid_hj2u2", "target_im_id":"wxid_fgq62", "create_time":"2021-12-07 21:01:53"}
{"create":{}}
{"self_im_id":"wxid_hj2u2", "target_im_id":"wxid_fgq62", "create_time":"2021-12-06 09:54:01"}
'

 

点击链接加入群聊【互联网技术交流群】:282575808

posted @ 2021-12-09 18:00  景岳  阅读(582)  评论(0编辑  收藏  举报