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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM