Java High Level REST Client 之 創建索引


1. 創建索引請求

CreateIndexRequest request = new CreateIndexRequest("twitter");


2.設置

2.1 分別設置

2.1.1 索引設置

request.settings(Settings.builder() 
    .put("index.number_of_shards", 3)
    .put("index.number_of_replicas", 2)
);

2.1.2 映射表

request.mapping(
        "{\n" +
        "  \"properties\": {\n" +
        "    \"message\": {\n" +
        "      \"type\": \"text\"\n" +
        "    }\n" +
        "  }\n" +
        "}", 
        XContentType.JSON);

也可以用map:

Map<String, Object> message = new HashMap<>();
message.put("type", "text");
Map<String, Object> properties = new HashMap<>();
properties.put("message", message);
Map<String, Object> mapping = new HashMap<>();
mapping.put("properties", properties);
request.mapping(mapping);

或者XContentBuilder :

XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
    builder.startObject("properties");
    {
        builder.startObject("message");
        {
            builder.field("type", "text");
        }
        builder.endObject();
    }
    builder.endObject();
}
builder.endObject();
request.mapping(builder);

2.1.3 索引別名

request.alias(new Alias("twitter_alias").filter(QueryBuilders.termQuery("user", "kimchy")));

別名不僅僅可以關聯一個索引,它能聚合多個索引。

例如我們為索引my_index_1 和 my_index_2 創建一個別名my_index_alias,這樣對my_index_alias的操作(僅限讀操作),會操作my_index_1和my_index_2,類似於聚合了my_index_1和my_index_2.但是我們是不能對my_index_alias進行寫操作,當有多個索引時alias,不能區分到底操作哪一個。

2.2 整體設置

request.source("{\n" +
        "    \"settings\" : {\n" +
        "        \"number_of_shards\" : 1,\n" +
        "        \"number_of_replicas\" : 0\n" +
        "    },\n" +
        "    \"mappings\" : {\n" +
        "        \"properties\" : {\n" +
        "            \"message\" : { \"type\" : \"text\" }\n" +
        "        }\n" +
        "    },\n" +
        "    \"aliases\" : {\n" +
        "        \"twitter_alias\" : {}\n" +
        "    }\n" +
        "}", XContentType.JSON);


3.其他參數

超時 (Timeout to wait for the all the nodes to acknowledge the index creation as a TimeValue):

request.setTimeout(TimeValue.timeValueMinutes(2));

連接主節點超時(Timeout to connect to the master node as a TimeValue):

request.setMasterTimeout(TimeValue.timeValueMinutes(1));
image


4.執行請求

4.1 同步執行

CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);

4.2 異步執行

client.indices().createAsync(request, RequestOptions.DEFAULT, listener);

其中listener:

ActionListener<CreateIndexResponse> listener =
        new ActionListener<CreateIndexResponse>() {

    @Override
    public void onResponse(CreateIndexResponse createIndexResponse) {
        
    }

    @Override
    public void onFailure(Exception e) {
        
    }
};


5.響應

boolean acknowledged = createIndexResponse.isAcknowledged(); 
boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged();








免責聲明!

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



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