一.创建索引
@SpringBootTest(classes = SearchApplication.class)
@RunWith(SpringRunner.class)
public class TestIndex {
@Qualifier("restHighLevelClient")
@Autowired
RestHighLevelClient client;
@Test
public void test1() throws IOException, InterruptedException {
CreateIndexRequest request = new CreateIndexRequest("my_index");
//设置参数
request.settings(Settings.builder().put("number_of_shards","1").put("number_of_replicas","1").build());
//1.指定mapping映射
/*Map<String, Object> field1 = new HashMap<>();
field1.put("type","text");
field1.put("analyzer","standard");
Map<String, Object> field2 = new HashMap<>();
field2.put("type","text");
Map<String, Object> properties =new HashMap<>();
properties.put("field1",field1);
properties.put("field2",field2);
Map<String,Object> mapping = new HashMap<>();
mapping.put("properties",properties);
request.mapping(mapping);*/
//2.指定mapping映射
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.startObject("properties");
{
builder.startObject("field1");
{
builder.field("type","text");
}
builder.endObject();
builder.startObject("field2");
{
builder.field("type","text");
}
builder.endObject();
}
builder.endObject();
}
builder.endObject();
request.mapping(builder);
//设置别名
request.alias(new Alias("prod_index"));
//可选参数
//超时时间
request.setTimeout(TimeValue.timeValueSeconds(5));
//主节点超时时间
request.setMasterTimeout(TimeValue.timeValueSeconds(5));
//设置创建索引api返回相应之前等待活动分片的数量
request.waitForActiveShards(ActiveShardCount.from(1));
//执行
//CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
//异步
ActionListener<CreateIndexResponse> listener = new ActionListener<>() {
@Override
public void onResponse(CreateIndexResponse createIndexResponse) {
System.out.println(createIndexResponse.isAcknowledged());
System.out.println(createIndexResponse.isShardsAcknowledged());
}
@Override
public void onFailure(Exception e) {
e.printStackTrace();
}
};
client.indices().createAsync(request,RequestOptions.DEFAULT,listener);
Thread.sleep(5000);
}
}
二.删除索引
@Test
public void testDelete() throws InterruptedException {
DeleteIndexRequest indexRequest = new DeleteIndexRequest("my_index");
/*AcknowledgedResponse response = client.indices().delete(indexRequest,RequestOptions.DEFAULT);
System.out.println(response.isAcknowledged());*/
//异步
ActionListener<AcknowledgedResponse> responseActionListener = new ActionListener<>() {
@Override
public void onResponse(AcknowledgedResponse response) {
System.out.println(response.isAcknowledged());
}
@Override
public void onFailure(Exception e) {
e.printStackTrace();
}
};
client.indices().deleteAsync(indexRequest,RequestOptions.DEFAULT,responseActionListener);
Thread.sleep(5000);
}
三.查看索引是否存在
@Test
public void testExist() throws IOException {
GetIndexRequest request = new GetIndexRequest("my_index");
//从主节点返回本地索引信息状态
request.local(false);
//可读性
request.humanReadable(true);
//是否返回每个索引的所有默认设置
request.includeDefaults(false);
System.out.println(client.indices().exists(request, RequestOptions.DEFAULT));
}
四.关闭索引
关闭索引后,该索引就不能添加文档了,需要重新开启索引。
@Test
public void testClose() throws IOException {
CloseIndexRequest request = new CloseIndexRequest("my_index");
CloseIndexResponse close = client.indices().close(request, RequestOptions.DEFAULT);
System.out.println(close.isAcknowledged());
}
五.打开索引
@Test
public void testOpen() throws IOException {
OpenIndexRequest request = new OpenIndexRequest("my_index");
OpenIndexResponse open = client.indices().open(request, RequestOptions.DEFAULT);
System.out.println(open.isAcknowledged());
}
Elastic Stack:es JavaApi实现索引管理
原文:https://www.cnblogs.com/wwjj4811/p/13093704.html