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好用

好的,我可以帮你回答这个问题。 使用Java客户端创建索引和映射,需要连接Elasticsearch服务器,并使用Elasticsearch Java API进行操作。以下是一个简单的示例代码,用于创建索引和映射: ```java import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContent; import java.io.IOException; public class ElasticSearchIndexAndMapping { private static final String INDEX_NAME = "example_index"; private static final String TYPE_NAME = "example_type"; public static void main(String[] args) throws IOException { RestHighLevelClient client = new RestHighLevelClient(/* your Elasticsearch client configuration */); // Create index request CreateIndexRequest request = new CreateIndexRequest(INDEX_NAME); // Create index mapping XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); { builder.startObject("properties"); { builder.startObject("field1"); { builder.field("type", "text"); } builder.endObject(); } builder.endObject(); } builder.endObject(); // Put mapping request PutMappingRequest mappingRequest = new PutMappingRequest(INDEX_NAME); mappingRequest.type(TYPE_NAME); mappingRequest.source(builder); // Execute requests client.indices().create(request, RequestOptions.DEFAULT); client.indices().putMapping(mappingRequest, RequestOptions.DEFAULT); client.close(); } } ``` 在此示例中,我们使用了 `CreateIndexRequest` 和 `PutMappingRequest` 类来创建索引和映射。我们首先创建了一个名为 `example_index` 的索引,并指定了一个名为 `example_type` 的类型。然后,我们使用 `XContentBuilder` 来构建映射。在此示例中,我们只创建了一个名为 `field1` 的字段,并将其类型设置为 `text`。最后,我们使用 `RestHighLevelClient` 来执行创建索引和映射的请求。 请注意,以上示例仅表示如何创建索引和映射。如果您需要更详细的功能,例如添加文档或查询索引,请查看Elasticsearch Java API文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值