• es-06-java创建mapping和setting


    说实话, java的方式太繁琐, 不如直接使用DSL进行创建

    1, create

    package com.wenbronk.elasticsearch.usage.index;
    
    import com.wenbronk.elasticsearch.usage.highLevel.RestHighLevelClientParent;
    import org.elasticsearch.ElasticsearchException;
    import org.elasticsearch.action.ActionListener;
    import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
    import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
    import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
    import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
    import org.elasticsearch.action.support.ActiveShardCount;
    import org.elasticsearch.action.support.IndicesOptions;
    import org.elasticsearch.common.settings.Settings;
    import org.elasticsearch.common.unit.TimeValue;
    import org.elasticsearch.common.xcontent.XContentBuilder;
    import org.elasticsearch.common.xcontent.XContentFactory;
    import org.elasticsearch.common.xcontent.XContentType;
    import org.elasticsearch.rest.RestStatus;
    import org.junit.jupiter.api.Test;
    
    import java.io.IOException;
    
    public class Index_1_Create extends RestHighLevelClientParent {
    
        /**
         * index 使用java客户端不好创建, 可以在kibana中进行创建
         * 注意mapping一旦创建, 不可更改
         */
        @Test
        public void createIndices() throws IOException {
    
            CreateIndexRequest request = new CreateIndexRequest("twitter");
    
            // add partition
            request.source("{
    " +
                    "    "settings" : {
    " +
                    "        "number_of_shards" : 3,
    " +
                    "        "number_of_replicas" : 2
    " +
                    "    },
    " +
                    "    "mappings" : {
    " +
                    "        "tweet" : {
    " +
                    "            "properties" : {
    " +
                    "                "message" : { "type" : "text" }
    " +
                    "            }
    " +
                    "        }
    " +
                    "    },
    " +
                    "    "aliases" : {
    " +
                    "        "twitter_alias" : {}
    " +
                    "    }
    " +
                    "}", XContentType.JSON);
    
            // 配置可选参数
            request.timeout(TimeValue.timeValueMinutes(2));
    //        request.timeout("2m");
            request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
    //        request.masterNodeTimeout("1m");
    //        request.waitForActiveShards(2);
            request.waitForActiveShards(ActiveShardCount.DEFAULT);
    
            // 使用异步的方式创建
            client.indices().createAsync(request, new ActionListener<CreateIndexResponse>() {
                @Override
                public void onResponse(CreateIndexResponse createIndexResponse) {
                    boolean acknowledged = createIndexResponse.isAcknowledged();
                    boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged();
                }
    
                @Override
                public void onFailure(Exception e) {
                    System.out.println(e.getCause());
                }
            });
        }
    
    }

    2, delete

    package com.wenbronk.elasticsearch.usage.index;
    
    import com.wenbronk.elasticsearch.usage.highLevel.RestHighLevelClientParent;
    import org.elasticsearch.ElasticsearchException;
    import org.elasticsearch.action.ActionListener;
    import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
    import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
    import org.elasticsearch.action.support.IndicesOptions;
    import org.elasticsearch.common.unit.TimeValue;
    import org.elasticsearch.rest.RestStatus;
    import org.junit.jupiter.api.Test;
    
    public class Index_2_Delete extends RestHighLevelClientParent {
    
        @Test
        public void deleteIndices() {
            DeleteIndexRequest request = new DeleteIndexRequest("twitter");
    
            request.timeout(TimeValue.timeValueMinutes(2));
            //        request.timeout("2m");
    //        request.masterNodeTimeout("1m");
            request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
            request.indicesOptions(IndicesOptions.lenientExpandOpen());
    
            client.indices().deleteAsync(request, new ActionListener<DeleteIndexResponse>() {
                @Override
                public void onResponse(DeleteIndexResponse deleteIndexResponse) {
                    boolean acknowledged = deleteIndexResponse.isAcknowledged();
                }
    
                @Override
                public void onFailure(Exception e) {
                    ElasticsearchException ee = (ElasticsearchException) e;
                    if (ee.status() == RestStatus.NOT_FOUND) {
                        System.out.println("indices not found");
                    }
                }
            });
    
        }
    
    }

    3, exists

    package com.wenbronk.elasticsearch.usage.index;
    
    import com.wenbronk.elasticsearch.usage.highLevel.RestHighLevelClientParent;
    import org.elasticsearch.action.ActionListener;
    import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
    
    public class Index_3_exists extends RestHighLevelClientParent {
    
        public void testExists() {
            GetIndexRequest request = new GetIndexRequest();
            request.indices("twitter");
    
            // 可选参数:
            request.local(false);
            request.humanReadable(true);
            request.includeDefaults(false);
    //        request.indicesOptions(indicesOptions);
    
            client.indices().existsAsync(request, new ActionListener<Boolean>() {
                @Override
                public void onResponse(Boolean aBoolean) {
                    System.out.println(aBoolean);
                }
    
                @Override
                public void onFailure(Exception e) {
    
                }
            });
        }
    }

    4, mapping

    package com.wenbronk.elasticsearch.usage.index;
    
    import com.wenbronk.elasticsearch.usage.highLevel.RestHighLevelClientParent;
    import org.elasticsearch.action.ActionListener;
    import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
    import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
    import org.elasticsearch.common.unit.TimeValue;
    import org.elasticsearch.common.xcontent.XContentType;
    
    public class Index_4_mapping extends RestHighLevelClientParent {
    
        public void testPutMapping() {
    
            PutMappingRequest request = new PutMappingRequest("twitter");
            request.type("tweet");
    
            request.source(
                    "{
    " +
                            "  "properties": {
    " +
                            "    "message": {
    " +
                            "      "type": "text"
    " +
                            "    }
    " +
                            "  }
    " +
                            "}",
                    XContentType.JSON);
    
            // 可选参数
            request.timeout(TimeValue.timeValueMinutes(2));
    //        request.timeout("2m");
            request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
    //        request.masterNodeTimeout("1m");
    
            client.indices().putMappingAsync(request, new ActionListener<PutMappingResponse>() {
                @Override
                public void onResponse(PutMappingResponse putMappingResponse) {
                    boolean acknowledged = putMappingResponse.isAcknowledged();
                    System.out.println(acknowledged);
                }
    
                @Override
                public void onFailure(Exception e) {
    
                }
            });
        }
    
    }

    还是原DSL好用

  • 相关阅读:
    01 《i》控制字体大小 v-for循环绑定类名 v-bind 结合三目运算 动态添加类
    右侧是长方形和半圆结合 光标放上去在规定时间内完成动画
    04-align-content 它对于当单行是没有效果的
    03-flex-wrap是否换行
    02-align-items的用法
    01--顶部的通告特效---仅显示一条一条滚动
    洛谷P2392 kkksc03考前临时抱佛脚(01背包/搜索)
    蓝桥杯 9大臣的旅费(树的直径)
    蓝桥杯 8买不到的数目(数论/线性DP)
    蓝桥杯 7连号区间数(暴力or并查集(?)
  • 原文地址:https://www.cnblogs.com/wenbronk/p/9395893.html
Copyright © 2020-2023  润新知