Elasticsearch rest-high-level-client 基本操作( 二 )


@Testpublic void test1() {CreateIndexRequest request = new CreateIndexRequest("blog1");try {CreateIndexResponse createIndexResponse =client.indices().create(request, RequestOptions.DEFAULT);boolean acknowledged = createIndexResponse.isAcknowledged();log.info("[create index blog :{}]", acknowledged);} catch (IOException e) {e.printStackTrace();}}4.删除索引 client.indices().delete构建 DeleteIndexRequest 对象
@Testpublic void testDeleteIndex(){DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("blog1");try {AcknowledgedResponse response = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);log.info("[delete index response: {}", response.isAcknowledged());} catch (IOException e) {e.printStackTrace();}}4.查询索引 client.indices().get构建 GetIndexRequest 对象
@Testpublic void testSearchIndex() {GetIndexRequest request = new GetIndexRequest("blog1");try {GetIndexResponse getIndexResponse =client.indices().get(request, RequestOptions.DEFAULT);Map<String, List<AliasMetaData>> aliases = getIndexResponse.getAliases();Map<String, MappingMetaData> mappings = getIndexResponse.getMappings();Map<String, Settings> settings = getIndexResponse.getSettings();log.info("[aliases: {}]", aliases);log.info("[mappings: {}]", mappings);log.info("[settings: {}", settings);} catch (IOException e) {e.printStackTrace();}}

Elasticsearch rest-high-level-client 基本操作

文章插图
可以根据 response 获取 aliases , mappings , settings 等等 和 Kibana 中返回的一样
Elasticsearch rest-high-level-client 基本操作

文章插图
5.插入文档client.index插入文档 需要使用 IndexRequest 对象 , 注意 不是 InsertRequest , 不知道为什么不这样定义 感觉会更加好理解
request.source(blogInfoJsonStr, XContentType.JSON);
@Testpublic void insertDoc() {IndexRequest request = new IndexRequest();request.index("blog1").id("1");BlogInfo blogInfo =new BlogInfo().setBlogName("Elasticsearch 入门第一章").setBlogType("Elasticsearch").setBlogDesc("本篇主要介绍了Elasticsearch 的基本client操作");try {//提供java 对象的 json strString blogInfoJsonStr = objectMapper.writeValueAsString(blogInfo);request.source(blogInfoJsonStr, XContentType.JSON);// 这里会抛错 原因是 我的 Elasticsearch 版本8.x 而 使用的 restHighLevel 已经解析不了,因为新的es已经不推荐使用// restHighLevel,而使用 Elasticsearch Java API ClientIndexResponse index = client.index(request, RequestOptions.DEFAULT);log.info("[Result insert doc :{} ]", index);} catch (IOException e) {}}6.查询文档 client.get注意getResponse.getSourceAsString() 返回文档数据
@Testpublic void testSelectDoc() {GetRequest getRequest = new GetRequest();getRequest.index("blog1").id("1");try {GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);BlogInfo blogInfo =objectMapper.readValue(getResponse.getSourceAsString(), BlogInfo.class);log.info("[get doc :{}] ", blogInfo);} catch (IOException e) {e.printStackTrace();}}
Elasticsearch rest-high-level-client 基本操作

文章插图
7.删除文档 client.delete注意 删除文档 的response 也解析不了 Elasticsearch 8.x 版本
@Testpublic void testDeleteDoc() {DeleteRequest deleteRequest = new DeleteRequest();deleteRequest.index("blog1").id("1");try {// 这里也会抛错 和上面的一样DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);log.info("[delete response:{} ]", deleteResponse);} catch (IOException e) {}}总结本篇主要介绍了 java 操作Elasticsearch 的客户端 rest-high-level-client 的基本使用 , 如果你是使用springboot 需要注意jar 冲突问题, 后续操作 Elasticsearch 客户端 逐渐变成 Elasticsearch Java API Client,不过目前大部分还是使用 rest-high-level-client

经验总结扩展阅读