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("{\n" +
                "    \"settings\" : {\n" +
                "        \"number_of_shards\" : 3,\n" +
                "        \"number_of_replicas\" : 2\n" +
                "    },\n" +
                "    \"mappings\" : {\n" +
                "        \"tweet\" : {\n" +
                "            \"properties\" : {\n" +
                "                \"message\" : { \"type\" : \"text\" }\n" +
                "            }\n" +
                "        }\n" +
                "    },\n" +
                "    \"aliases\" : {\n" +
                "        \"twitter_alias\" : {}\n" +
                "    }\n" +
                "}", 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(
                "{\n" +
                        "  \"properties\": {\n" +
                        "    \"message\": {\n" +
                        "      \"type\": \"text\"\n" +
                        "    }\n" +
                        "  }\n" +
                        "}",
                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好用

posted @ 2018-08-02 13:32  bronk  阅读(4603)  评论(0编辑  收藏  举报