博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

TransportClient 新建index,mappings dynamic_templates。

Posted on 2018-03-16 09:51  和风细雨汪汪  阅读(544)  评论(0)    收藏  举报
public void createIndex(TransportClient client, String index){
        CreateIndexRequest request = new CreateIndexRequest(index);
        String setting =""
                +"{"
                +"        \"number_of_shards\": 3,"
                +"        \"number_of_replicas\": 1,"
                +"        \"search_analyzer\": \"ik_max_word\""
                +"}";
        String mapping ="{"
                +"            \"dynamic_templates\": ["
                +"                {"
                +"                    \"strings\": {"
                +"                        \"match\": \"*\","
                +"                        \"match_mapping_type\": \"string\","
                +"                        \"mapping\": {"
                +"                            \"type\": \"string\","
                +"                            \"analyzer\": \"ik_max_word\","
                +"                            \"fields\": {"
                +"                                \"raw\": {"
                +"                                    \"type\": \"string\","
                +"                                    \"index\": \"not_analyzed\","
                +"                                    \"ignore_above\": 256"
                +"                                }"
                +"                            }"
                +"                        }"
                +"                    }"
                +"                }"
                +"            ]"
                +"        }";
            request.settings(setting).mapping("_default_", mapping);
            client.admin().indices().create(request);
    }
POST  http://192.168.1.12:9200/test

{
    "settings": {
        "number_of_shards": 3,
        "number_of_replicas": 1,
        "search_analyzer": "ik_max_word"
    },
    "mappings": {
        "_default_": {
            "dynamic_templates": [
                {
                    "strings": {
                        "match": "*",
                        "match_mapping_type": "string",
                        "mapping": {
                            "type": "string",
                            "analyzer": "ik_max_word",
                            "fields": {
                                "raw": {
                                    "type": "string",
                                    "index": "not_analyzed",
                                    "ignore_above": 2560
                                }
                            }
                        }
                    }
                }
            ]
        }
    }
}

 

{
    "settings": {
        "number_of_shards": 5,
        "number_of_replicas": 1,
        "search_analyzer": "ik_max_word"
    },
    "mappings": {
        "_default_": {
            "dynamic_templates": [
                {
                    "strings": {
                        "match": "*",
                        "unmatch": "id",
                        "match_mapping_type": "string",
                        "mapping": {
                            "type": "string",
                            "analyzer": "ik_max_word",
                            "fields": {
                                "raw": {
                                    "type": "string",
                                    "index": "not_analyzed",
                                    "ignore_above": 2560
                                }
                            }
                        }
                    }
                },
                {
                    "integers": {
                        "match": "id",
                        "mapping": {
                            "type": "integer"
                        }
                    }
                }
            ]
        }
    }
}