springboot+elasticsearch7.3.0インデックスの作成

2664 ワード

まず、以下を参照してください.https://blog.csdn.net/qq_27950699/article/details/99354774Clientの構築
まず、組み込みパッケージを除外します(不要な場合があります.自分でテストできます):

    org.elasticsearch.client
    elasticsearch-rest-high-level-client
    7.3.0
    
        
            org.elasticsearch
            elasticsearch
        
        
            org.elasticsearch.client
            elasticsearch-rest-client
        
    


    org.elasticsearch.client
    elasticsearch-rest-client
    7.3.0


    org.elasticsearch
    elasticsearch
    7.3.0




統合開始es:index:インデックス名
EsConfig esConfig = new EsConfig();
RestHighLevelClient client = esConfig.client();
CreateIndexRequest request = new CreateIndexRequest(index);
request.settings(Settings.builder()
        .put("index.number_of_shards",10)   // 
        .put("index.number_of_replicas", 1));// 
request.alias(new Alias(index+"alias"));// 
request.setTimeout(TimeValue.timeValueMinutes(2));// 2 
//  ( )
try {
    CreateIndexResponse createIndexResponse = client.indices().create(request,RequestOptions.DEFAULT);
    //  
    boolean acknowledged = createIndexResponse.isAcknowledged();
    boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged();
    System.out.println(acknowledged+","+shardsAcknowledged);
   client.close();
} catch (IOException e) {
    logger.error(" {} :"+e.getMessage(),index);
    return false;
}
//  ( )
/** ActionListener listener = new ActionListener() {
                @Override
                public void onResponse(CreateIndexResponse createIndexResponse) {
                    boolean acknowledged = createIndexResponse.isAcknowledged();
                    boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged();
                }
                @Override
                public void onFailure(Exception e) {
                    e.printStackTrace();
                }
            };
  client.indices().createAsync(request, listener);*/

return true;
 , client.close();