目录
https://github.com/shuchang-wang/Eleasticsearch
一、Java api 实现文档管理
1、导包【Maven】
2、代码
二、结合spring-boot-test测试文档查询
0、为什么使用spring boot test
1、导包
2、配置 application.yml
3、代码
1. 主类【配置springboot启动类】
2. 配置类
3.测试对索引文档的CRUD
4、测试对索引的管理
github托管源代码路径:
步骤
获取连接客户端构建请求执行获取结果eg:
package com.alibaba.es; import org.apache.http.HttpHost; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import java.io.IOException; public class TestDemo { public static void main(String[] args) throws IOException { // 1 获取连接客户端 RestHighLevelClient client = new RestHighLevelClient(RestClient.builder( new HttpHost("192.168.21.131",9200,"http") )); // 2构建请求 PUT /book/_doc/1 GetRequest request = new GetRequest("book","1"); // 3执行 GetResponse response = client.get(request, RequestOptions.DEFAULT); // 4获取结果 System.out.println(response.getIndex()); System.out.println(response.getType()); System.out.println(response.getId()); System.out.println(response.getVersion()); System.out.println(response.getSeqNo()); System.out.println(response.getPrimaryTerm()); System.out.println(response.isExists()); System.out.println(response.getSourceAsString()); } }1、测试获取文档【Retrieve】
1.REST API
GET /book/_doc/12.代码实现:
package com.alibaba.es; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.Strings; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; import java.io.IOException; @SpringBootTest(classes = SearchApplication.class) @RunWith(SpringRunner.class) public class TestDocument { @Resource private RestHighLevelClient client; @Test public void testGet() throws IOException { // 1.构建请求 GetRequest getRequest = new GetRequest("test_post", "1"); // -------------------------可选参数start------------------------- //为特定字段配置_source_include // String[] includes = {"user","message"}; // String[] excludes = Strings.EMPTY_ARRAY; // FetchSourceContext context = new FetchSourceContext(true,includes,excludes); // getRequest.fetchSourceContext(context); //为特定字段配置_source_excludes // String[] includes = Strings.EMPTY_ARRAY; // String[] excludes = {"user","message"}; // FetchSourceContext context = new FetchSourceContext(true,includes,excludes); // getRequest.fetchSourceContext(context); // 设置路由 // getRequest.routing("routing"); // -------------------------可选参数end------------------------- // 2.执行 //---------------------同步查询--------------------- GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT); // 3.处理执行结果 // System.out.println(getResponse.getIndex()); // System.out.println(getResponse.getType()); // System.out.println(getResponse.getId()); // System.out.println(getResponse.getVersion()); // System.out.println(getResponse.getSeqNo()); // System.out.println(getResponse.getPrimaryTerm()); // System.out.println(getResponse.isExists()); // System.out.println(getResponse.getSourceAsString()); //---------------------异步查询--------------------- // ActionListener listener = new ActionListener() { // //成功时 // public void onResponse(GetResponse getResponse) { // System.out.println(getResponse.getIndex()); // System.out.println(getResponse.getType()); // System.out.println(getResponse.getId()); // System.out.println(getResponse.getVersion()); // System.out.println(getResponse.getSeqNo()); // System.out.println(getResponse.getPrimaryTerm()); // System.out.println(getResponse.isExists()); // System.out.println(getResponse.getSourceAsString()); // } // //失败时 // public void onFailure(Exception e) { // e.printStackTrace(); // } // }; // client.getAsync(getRequest, RequestOptions.DEFAULT, listener); // try { // Thread.sleep(5000); // } catch (InterruptedException e) { // e.printStackTrace(); // } if (getResponse.isExists()) { System.out.println(getResponse.getIndex()); System.out.println(getResponse.getType()); System.out.println(getResponse.getId()); System.out.println(getResponse.getVersion()); System.out.println(getResponse.getSeqNo()); System.out.println(getResponse.getPrimaryTerm()); System.out.println(getResponse.isExists()); System.out.println(getResponse.getSourceAsString());//以String获取数据 System.out.println(getResponse.getSourceAsBytes());//以bytes获取数据 System.out.println(getResponse.getSourceAsMap());//以Map获取数据 System.out.println(getResponse.getSourceAsMap().get("user"));//获取Map中的数据 } else { System.out.println("获取失败"); } } }2、测试文档新增【Create】
1.REST API
PUT test_post/_doc/2 { "user":"tomas", "postDate":"2019-07-18", "message":"trying out es1" }2.代码实现:
@Test public void testAdd() throws IOException { //1.构建请求 IndexRequest indexRequest = new IndexRequest("test_post"); indexRequest.id("4"); // =======================构建文档数据========================= //方法一 String String jsonString = "{\n" + " \"user\":\"tomas\",\n" + " \"postDate\":\"2019-07-18\",\n" + " \"message\":\"trying out es1\"\n" + "}"; indexRequest.source(jsonString, XContentType.JSON); //方法二 Map // Map jsonMap = new HashMap(); // jsonMap.put("user","tomas"); // jsonMap.put("postDate","2019-07-18"); // jsonMap.put("message","trying out es1"); // indexRequest.source(jsonMap); //方法三 XContentBuilder // XContentBuilder builder = XContentFactory.jsonBuilder(); // builder.startObject(); // { // builder.field("user","tomas"); // builder.timeField("postDate",new Date()); // builder.field("message","trying out es1"); // } // builder.endObject(); // indexRequest.source(builder); //方法四 Object... // indexRequest.source("user","tomas","postDate",new Date(),"message","trying out es1"); //============================可选参数============================ //设置超时时间 // indexRequest.timeout("1s"); // indexRequest.timeout(TimeValue.timeValueSeconds(1)); //手动维护版本号 // indexRequest.version(4); // indexRequest.versionType(VersionType.EXTERNAL); //2.执行 //同步执行 IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT); // System.out.println(indexResponse.getIndex()); // System.out.println(indexResponse.getType()); // System.out.println(indexResponse.getId()); // System.out.println(indexResponse.getVersion()); // System.out.println(indexResponse.getResult()); // System.out.println(indexResponse.getShardInfo()); // System.out.println(indexResponse.getSeqNo()); // System.out.println(indexResponse.getPrimaryTerm()); // System.out.println(indexResponse.status()); // System.out.println(indexResponse.toString()); if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) { DocWriteResponse.Result result = indexResponse.getResult(); System.out.println("CREATE:" + result); } else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) { DocWriteResponse.Result result = indexResponse.getResult(); System.out.println("UPDATE:" + result); }else{ System.out.println(indexResponse.getResult() ); } // "_shards" : { // "total" : 2, // "successful" : 2, // "failed" : 0 // } ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo(); if(shardInfo.getTotal()!=shardInfo.getSuccessful()){ System.out.println("处理成功的分片数少于总分片!"); } if(shardInfo.getFailed()>0){ ReplicationResponse.ShardInfo.Failure[] failures = shardInfo.getFailures(); for (ReplicationResponse.ShardInfo.Failure failure : failures) { String reason = failure.reason();//每一个错误的原因 System.out.println(reason); } } //异步执行 // ActionListener listener = new ActionListener() { // public void onResponse(IndexResponse indexResponse) { // System.out.println(indexResponse.getIndex()); // System.out.println(indexResponse.getType()); // System.out.println(indexResponse.getId()); // System.out.println(indexResponse.getVersion()); // System.out.println(indexResponse.getResult()); // System.out.println(indexResponse.getShardInfo()); // System.out.println(indexResponse.getSeqNo()); // System.out.println(indexResponse.getPrimaryTerm()); // System.out.println(indexResponse.status()); // System.out.println(indexResponse.toString()); // } // // public void onFailure(Exception e) { // e.printStackTrace(); // } // }; // client.indexAsync(indexRequest, RequestOptions.DEFAULT, listener); // try { // Thread.sleep(5000); // } catch (InterruptedException e) { // e.printStackTrace(); // } }3、测试文档修改【UPDATE】
1.REST API
POST /test_post/_doc/3/_update { "doc": { "user":"tomas Lee" } }2.代码实现:
@Test public void testUpdate() throws IOException { // POST /test_post/_doc/3/_update // { // "doc": { // "user":"tomas Lee" // } // } //1.构建请求 UpdateRequest updateRequest = new UpdateRequest("test_post", "3"); Map jsonMap = new HashMap(); jsonMap.put("user", "tomas Lee"); updateRequest.doc(jsonMap); //可选参数 updateRequest.timeout("1s"); updateRequest.retryOnConflict(3);//重试次数 //2.执行 //同步执行 UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT); //3.获取结果 System.out.println(updateResponse.getIndex()); System.out.println(updateResponse.getType()); System.out.println(updateResponse.getId()); System.out.println(updateResponse.getVersion()); System.out.println(updateResponse.getSeqNo()); System.out.println(updateResponse.getPrimaryTerm()); System.out.println(updateResponse.getResult()); if(updateResponse.getResult()== DocWriteResponse.Result.CREATED){//创建 System.out.println("CREATE:"+updateResponse.getResult()); }else if(updateResponse.getResult()== DocWriteResponse.Result.UPDATED){//更新 System.out.println("UPDATE:"+updateResponse.getResult()); }else if(updateResponse.getResult()== DocWriteResponse.Result.DELETED){//删除 System.out.println("DELETE:"+updateResponse.getResult()); }else if(updateResponse.getResult()== DocWriteResponse.Result.NOOP){//没有操作 System.out.println("NOOP:"+updateResponse.getResult()); }else { System.out.println(updateResponse.getResult()); } //异步执行 // ActionListener listener = new ActionListener() { // public void onResponse(UpdateResponse updateResponse) { // System.out.println(updateResponse.getIndex()); // System.out.println(updateResponse.getType()); // System.out.println(updateResponse.getId()); // System.out.println(updateResponse.getVersion()); // System.out.println(updateResponse.getResult()); // System.out.println(updateResponse.getShardInfo()); // System.out.println(updateResponse.getSeqNo()); // System.out.println(updateResponse.getPrimaryTerm()); // System.out.println(updateResponse.status()); // System.out.println(updateResponse.toString()); // } // // public void onFailure(Exception e) { // e.printStackTrace(); // } // }; // client.updateAsync(updateRequest, RequestOptions.DEFAULT, listener); // try { // Thread.sleep(5000); // } catch (InterruptedException e) { // e.printStackTrace(); // } }4、测试文档删除【DELETE】
1.REST API
DELETE /test_post/_doc/32.代码实现:
@Test public void testDelete() throws IOException { //1.创建请求 DeleteRequest deleteRequest = new DeleteRequest("test_post", "3"); //2.执行 DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT); //3.处理结果 System.out.println(deleteResponse.getIndex()); System.out.println(deleteResponse.getType()); System.out.println(deleteResponse.getId()); System.out.println(deleteResponse.getVersion()); System.out.println(deleteResponse.getSeqNo()); System.out.println(deleteResponse.getPrimaryTerm()); System.out.println(deleteResponse.getResult()); //异步执行 // ActionListener listener = new ActionListener() { // public void onResponse(DeleteResponse deleteResponse) { // System.out.println(deleteResponse.getIndex()); // System.out.println(deleteResponse.getType()); // System.out.println(deleteResponse.getId()); // System.out.println(deleteResponse.getVersion()); // System.out.println(deleteResponse.getSeqNo()); // System.out.println(deleteResponse.getPrimaryTerm()); // System.out.println(deleteResponse.getResult()); // } // // public void onFailure(Exception e) { // e.printStackTrace(); // } // }; // client.deleteAsync(deleteRequest, RequestOptions.DEFAULT, listener); // try { // Thread.sleep(5000); // } catch (InterruptedException e) { // e.printStackTrace(); // } }5、测试文档bulk【Bulk批量管理】
1.REST API
POST /_bulk {"action": {"metadata"}} {"data"}2.代码实现:
@Test public void testBulk() throws IOException { //1.创建请求 BulkRequest bulkRequest = new BulkRequest(); // bulkRequest.add(new IndexRequest("post").id("1").source(XContentType.JSON, "filed", "1")); // bulkRequest.add(new IndexRequest("post").id("2").source(XContentType.JSON, "filed", "2")); bulkRequest.add(new UpdateRequest("post","1").doc(XContentType.JSON, "filed", "3")); bulkRequest.add(new DeleteRequest("post").id("2")); //2.执行 //同步请求 BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT); //3.获取结果 for (BulkItemResponse bulkItemResponse : bulkResponse) { DocWriteResponse response = bulkItemResponse.getResponse(); switch (bulkItemResponse.getOpType()) { case INDEX: IndexResponse indexResponse = (IndexResponse) response; System.out.println("INDEX:" + indexResponse.getResult()); break; case CREATE: IndexResponse createResponse = (IndexResponse) response; System.out.println("CREATE:" + createResponse.getResult()); break; case UPDATE: UpdateResponse updateResponse = (UpdateResponse) response; System.out.println("UPDATE:" + updateResponse.getResult()); break; case DELETE: DeleteResponse deleteResponse = (DeleteResponse) response; System.out.println("DELETE:" + deleteResponse.getResult()); break; default: System.out.println("其他!"); } } //异步请求 // ActionListener listener = new ActionListener() { // public void onResponse(BulkResponse bulkItemResponses) { // //3.获取结果 // for (BulkItemResponse bulkItemResponse : bulkItemResponses) { // DocWriteResponse response = bulkItemResponse.getResponse(); // switch (bulkItemResponse.getOpType()) { // case INDEX: // IndexResponse indexResponse = (IndexResponse) response; // System.out.println("INDEX:" + indexResponse.getResult()); // break; // case CREATE: // IndexResponse createResponse = (IndexResponse) response; // System.out.println("CREATE:" + createResponse.getResult()); // break; // case UPDATE: // UpdateResponse updateResponse = (UpdateResponse) response; // System.out.println("UPDATE:" + updateResponse.getResult()); // break; // case DELETE: // DeleteResponse deleteResponse = (DeleteResponse) response; // System.out.println("DELETE:" + deleteResponse.getResult()); // break; // default: // System.out.println("其他!"); // } // } // } // // public void onFailure(Exception e) { // // } // }; // client.bulkAsync(bulkRequest, RequestOptions.DEFAULT, listener); // try { // Thread.sleep(5000); // } catch ( // InterruptedException e) { // e.printStackTrace(); // } }1、测试构建索引
1.REST API
PUT /my_index { "settings": { "number_of_shards": 1, "number_of_replicas": 1 }, "mappings": { "properties": { "filed1": { "type": "text" }, "filed2": { "type": "text" } } }, "aliases": { "default_index": {} } }2.代码实现:
/** * 同步创建索引 * * @throws IOException */ @Test public void testCreateIndex() throws IOException { /* PUT /my_index { "settings": { "number_of_shards": 1, "number_of_replicas": 1 }, "mappings": { "properties": { "filed1": { "type": "text" }, "filed2": { "type": "text" } } }, "aliases": { "default_index": {} } }*/ //1.获取请求 CreateIndexRequest createIndexRequest = new CreateIndexRequest("my_index"); //设置请求参数 createIndexRequest.settings(Settings.builder().put("number_of_shards", "1").put("number_of_replicas", "1").build()); //设置映射1 createIndexRequest.mapping("{\n" + " \"properties\": {\n" + " \"filed1\": {\n" + " \"type\": \"text\"\n" + " },\n" + " \"filed2\": {\n" + " \"type\": \"text\"\n" + " }\n" + " }\n" + " }", XContentType.JSON); //设置映射2 // Map<String, Object> filed1 = new HashMap<String, Object>(); // filed1.put("type", "text"); // filed1.put("analyzer", "standard"); // Map<String, Object> filed2 = new HashMap<String, Object>(); // filed2.put("type", "text"); // Map<String, Object> properties = new HashMap<String, Object>(); // properties.put("filed1", filed1); // properties.put("filed2", filed2); // Map<String, Object> mapping = new HashMap<String, Object>(); // mapping.put("properties", properties); // createIndexRequest.mapping(mapping); //设置映射3 // XContentBuilder xContentBuilder = XContentFactory.jsonBuilder(); // xContentBuilder.startObject(); // { // xContentBuilder.startObject("fild1"); // { // xContentBuilder.field("type","text"); // } // xContentBuilder.endObject(); // xContentBuilder.startObject("fild2"); // { // xContentBuilder.field("type","text"); // } // xContentBuilder.endObject(); // } // xContentBuilder.endObject(); // createIndexRequest.mapping(xContentBuilder); //设置别名 createIndexRequest.alias(new Alias("prod_index")); //==================可选参数================== //设置超时时间 createIndexRequest.setTimeout(TimeValue.timeValueSeconds(5)); //主节点超时时间 createIndexRequest.setMasterTimeout(TimeValue.timeValueSeconds(5)); //设置创建索引API返回响应之前等待活动分片的数量 createIndexRequest.waitForActiveShards(ActiveShardCount.from(1)); //2.执行 CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT); //3.处理结果 //索引 String index = createIndexResponse.index(); System.out.println("index:" + index); //得到响应(全部) boolean acknowledged = createIndexResponse.isAcknowledged(); System.out.println("acknowledged:" + acknowledged); //得到响应 指示是否在超时前为索引中的每个分片启动了所需数量的碎片副本 boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged(); System.out.println("shardsAcknowledged:" + shardsAcknowledged); } /** * 异步创建索引 * * @throws IOException */ @Test public void testCreateIndexAsync() throws IOException { //1.获取请求 CreateIndexRequest createIndexRequest = new CreateIndexRequest("my_index"); //设置请求参数 createIndexRequest.settings(Settings.builder().put("number_of_shards", "1").put("number_of_replicas", "1").build()); //设置映射1 createIndexRequest.mapping("{\n" + " \"properties\": {\n" + " \"filed1\": {\n" + " \"type\": \"text\"\n" + " },\n" + " \"filed2\": {\n" + " \"type\": \"text\"\n" + " }\n" + " }\n" + " }", XContentType.JSON); //设置别名 createIndexRequest.alias(new Alias("prod_index")); //==================可选参数================== //设置超时时间 createIndexRequest.setTimeout(TimeValue.timeValueSeconds(5)); //主节点超时时间 createIndexRequest.setMasterTimeout(TimeValue.timeValueSeconds(5)); //设置创建索引API返回响应之前等待活动分片的数量 createIndexRequest.waitForActiveShards(ActiveShardCount.from(1)); //2.执行 ActionListener<CreateIndexResponse> listener = new ActionListener<CreateIndexResponse>() { @Override public void onResponse(CreateIndexResponse createIndexResponse) { //3.处理结果 //索引 String index = createIndexResponse.index(); System.out.println("index:" + index); //得到响应(全部) boolean acknowledged = createIndexResponse.isAcknowledged(); System.out.println("acknowledged:" + acknowledged); //得到响应 指示是否在超时前为索引中的每个分片启动了所需数量的碎片副本 boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged(); System.out.println("shardsAcknowledged:" + shardsAcknowledged); } @Override public void onFailure(Exception e) { e.printStackTrace(); } }; client.indices().createAsync(createIndexRequest, RequestOptions.DEFAULT, listener); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } }2、测试删除索引
1.REST API
DELETE /my_index2.代码实现:
/** * 同步删除索引 * * @throws IOException */ @Test public void testDeleteIndex() throws IOException { //构建请求 DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("my_index"); deleteIndexRequest.timeout(TimeValue.timeValueSeconds(5)); deleteIndexRequest.masterNodeTimeout(TimeValue.timeValueSeconds(5)); //同步执行 AcknowledgedResponse acknowledgedResponse = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT); //处理响应 boolean acknowledged = acknowledgedResponse.isAcknowledged(); System.out.println(acknowledged); } /** * 异步删除索引 * * @throws IOException */ @Test public void testDeleteIndexAsync() throws IOException { //构建请求 DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("my_index"); deleteIndexRequest.timeout(TimeValue.timeValueSeconds(5)); deleteIndexRequest.masterNodeTimeout(TimeValue.timeValueSeconds(5)); //异步执行 ActionListener<AcknowledgedResponse> listener = new ActionListener<AcknowledgedResponse>() { @Override public void onResponse(AcknowledgedResponse acknowledgedResponse) { //处理响应 boolean acknowledged = acknowledgedResponse.isAcknowledged(); System.out.println(acknowledged); System.out.println(acknowledgedResponse.toString()); System.out.println("删除索引成功!!!"); } @Override public void onFailure(Exception e) { System.out.println("删除索引失败!!!"); e.printStackTrace(); } }; client.indices().deleteAsync(deleteIndexRequest, RequestOptions.DEFAULT, listener); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } }3、测试查看索引是否存在
代码实现:
/** * 同步获取索引 Index exists API * * @throws IOException */ @Test public void testExistsIndex() throws IOException { //构建请求 GetIndexRequest getIndexRequest = new GetIndexRequest("my_index"); //=========================参数========================= //从主节点返回本地索引信息状态 getIndexRequest.local(false); //以适合人类的格式返回 getIndexRequest.humanReadable(true); //是否返回每个索引的所有默认配置 getIndexRequest.includeDefaults(false); //执行 boolean exists = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT); //处理响应 System.out.println(exists); } /** * 异步获取索引 Index exists API * * @throws IOException */ @Test public void testExistsIndexAsync() throws IOException { //构建请求 GetIndexRequest getIndexRequest = new GetIndexRequest("my_index"); //=========================参数========================= //从主节点返回本地索引信息状态 getIndexRequest.local(false); //以适合人类的格式返回 getIndexRequest.humanReadable(true); //是否返回每个索引的所有默认配置 getIndexRequest.includeDefaults(false); //执行 ActionListener<Boolean> listener = new ActionListener<Boolean>() { @Override public void onResponse(Boolean exists) { //处理响应 System.out.println(exists); } @Override public void onFailure(Exception e) { e.printStackTrace(); } }; client.indices().existsAsync(getIndexRequest, RequestOptions.DEFAULT, listener); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } }4、测试关闭索引
代码实现:
/** * 同步关闭索引 */ @Test public void testCloseIndex() throws IOException { //构建请求 CloseIndexRequest closeIndexRequest = new CloseIndexRequest("my_index"); //执行 AcknowledgedResponse acknowledgedResponse = client.indices().close(closeIndexRequest, RequestOptions.DEFAULT); //处理响应 boolean acknowledged = acknowledgedResponse.isAcknowledged(); System.out.println(acknowledged); } /** * 异步关闭索引 */ @Test public void testCloseIndexAsync() { //构建请求 CloseIndexRequest closeIndexRequest = new CloseIndexRequest("my_index"); //执行 ActionListener<AcknowledgedResponse> listener = new ActionListener<AcknowledgedResponse>() { @Override public void onResponse(AcknowledgedResponse acknowledgedResponse) { boolean acknowledged = acknowledgedResponse.isAcknowledged(); System.out.println(acknowledged); } @Override public void onFailure(Exception e) { e.printStackTrace(); } }; client.indices().closeAsync(closeIndexRequest, RequestOptions.DEFAULT, listener); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } }5、测试打开索引
代码实现:
/** * 同步打开索引 * * @throws IOException */ @Test public void testOpenIndex() throws IOException { //构建请求 OpenIndexRequest openIndexRequest = new OpenIndexRequest("my_index"); //执行 OpenIndexResponse openIndexResponse = client.indices().open(openIndexRequest, RequestOptions.DEFAULT); //处理响应结果 boolean acknowledged = openIndexResponse.isAcknowledged(); System.out.println(acknowledged); } /** * 异步打开索引 * * @throws IOException */ @Test public void testOpenIndexAsync() { //构建请求 OpenIndexRequest openIndexRequest = new OpenIndexRequest("my_index"); ActionListener<OpenIndexResponse> listener = new ActionListener<OpenIndexResponse>() { @Override public void onResponse(OpenIndexResponse openIndexResponse) { //处理响应结果 boolean acknowledged = openIndexResponse.isAcknowledged(); System.out.println(acknowledged); } @Override public void onFailure(Exception e) { e.printStackTrace(); } }; //执行 client.indices().openAsync(openIndexRequest, RequestOptions.DEFAULT, listener); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } }