Java Elasticsearch 使用

概览

基于官方教程,提供Elasticsearch的基本操作,并编写单元测试:

  • 客户端连接
  • 客户端关闭
  • 建立索引
  • 删除索引
  • 插入文档
    • 单个文档的同步和异步插入
    • 多个文档的同步和异步插入
  • 查询文档

本地Docker的Elasticsearch搭建可参考:https://juejin.cn/post/6965321555475693582/

代码细节

Elasticsearch封装类

import lombok.SneakyThrows;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.*;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortBuilder;

import java.io.IOException;
import java.util.List;

/**
 * official document: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.12/java-rest-high-document-bulk.html
 *
 * @author liuwei
 *
 * 自动重连说明:经过测试,在ES重启完成后,相关的操作便可恢复正常,无序额外操作
 */
public class ElasticClient {
   

    final private RestHighLevelClient client;

    /**
     * 构造Elasticsearch客户端
     * @param userName 用户名
     * @param password 密码
     * @param hosts 集群主机地址
     */
    public ElasticClient(final String userName, final String password, final HttpHost[] hosts) {
   
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userName, password);
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, credentials);

        client = new RestHighLevelClient(
                RestClient.builder(hosts)
                        .setHttpClientConfigCallback(builder -> builder.setDefaultCredentialsProvider(credentialsProvider)));
    }

    /**
     * 关闭客户端连接
     * @throws IOException IOException
     */
    public void close() throws IOException {
   
        client.close();
    }

    /**
     * 检测索引是否已建立
     * @param indexNames 索引名
     * @return bool
     */
    @SneakyThrows
    public boolean checkIndexExist(final String... indexNames) {
   
        GetIndexRequest request = new GetIndexRequest(indexNames);
        return client.indices().exists(request, RequestOptions.DEFAULT);
    }

    /**
     * 新建索引
     * @param indexName 索引名称
     * @param setting 索引配置内容(Json格式)
     * @return bool
     */
    @SneakyThrows
    public boolean createIndex(String indexName, String setting) {
   
        CreateIndexRequest request = new CreateIndexRequest(indexName);
        request.source(setting, XContentType.JSON);
        CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
        return createIndexResponse.isAcknowledged();
    }

    /**
     * 删除索引
     * @param indexName 删除索引
     * @return bool
     */
    @SneakyThrows
    public boolean deleteIndex(String indexName) {
   
        DeleteIndexRequest request = new DeleteIndexRequest(indexName)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值