目录
Java api实现 索引管理
Pom
测试联通
增加索引
打印结果
异步执行
删除索引
查看索引是否存在
关闭索引
开启索引
Java api实现 索引管理
Pom
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
测试联通
private RestHighLevelClient client;
//链接
@Test
void testCreateClient() throws IOException {
HttpHost host = HttpHost.create("http://localhost:9200");
RestClientBuilder builder = RestClient.builder(host);
client = new RestHighLevelClient(builder);
client.close();
} |
增加索引
|
设置 mapping
设置 别名
设置 超时时间
设置 主节点超时时间
打印结果
System.out.println( r.isAcknowledged()+" ------ "+ r.isShardsAcknowledged());
异步执行
package com.esbase.fintech.ais;
import org.apache.http.HttpHost;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
//高级客户端
@SpringBootTest
public class SpringBootEsApplicationTestsGj {
private RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://localhost:9200")));
//创建索引
@Test
public void testCreateIndex() throws IOException, InterruptedException {
CreateIndexRequest request = new CreateIndexRequest("boodsdksasd901dd");
Map<String,Object> map = new HashMap<>();
map.put("type","text");
Map<String,Object> map2 = new HashMap<>();
map2.put("type","text");
Map<String,Object> mapP = new HashMap<>();
mapP.put("name",map);
mapP.put("age",map2);
Map<String,Object> mapP1 = new HashMap<>();
mapP1.put("properties",mapP);
request.mapping(mapP1);
// CreateIndexResponse r = client.indices().create(request, RequestOptions.DEFAULT);
// System.out.println( r.isAcknowledged()+" ------ "+ r.isShardsAcknowledged());
ActionListener<CreateIndexResponse> rs = new ActionListener<CreateIndexResponse>() {
@Override
public void onResponse(CreateIndexResponse createIndexResponse) {
System.out.println( createIndexResponse.isAcknowledged()+" ------ "+ createIndexResponse.isShardsAcknowledged());
}
@Override
public void onFailure(Exception e) {
System.out.println("error:"+e);
}
};
client.indices().createAsync(request,RequestOptions.DEFAULT,rs);
Thread.sleep(1000);
client.close();
}
} |
删除索引
查看索引是否存在
关闭索引
开启索引
ok
持续更新