ElasticSearchClientUtil工具类和ElasticSearchConfig配置类

本文档介绍了一个名为 `ElasticSearchClientUtil` 的工具类,用于执行 Elasticsearch 的各种操作,如新增、批量新增、查询、更新和删除文档。类中包含了对索引、文档ID、查询条件等的操作方法,使用了阿里巴巴的 FastJSON 库进行 JSON 转换,并通过 `RestHighLevelClient` 进行与 Elasticsearch 服务器的交互。同时,提供了配置类 `ElasticSearchConfig` 来设置 Elasticsearch 的连接参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ES工具类

package org.bwf.study.util;

import com.alibaba.fastjson.JSON;
import org.bwf.study.config.ElasticSearchConfig;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.core.CountResponse;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class ElasticSearchClientUtil {

    @Resource(name = "restHighLevelClient")
    private RestHighLevelClient restHighLevelClient;

    /**
     * 新增一条文档
     *
     * @param indexName   索引名称
     * @param documentObj 文档对象
     */
    public IndexResponse addDocument(String indexName, Object documentObj) {
        try {
            IndexRequest indexRequest = new IndexRequest(indexName);
            indexRequest.source(JSON.toJSONString(documentObj), XContentType.JSON);
            //同步索引(同步 API 会导致阻塞,一直等待数据返回)
            return restHighLevelClient.index(indexRequest, ElasticSearchConfig.options);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 批量新增文档
     *
     * @param indexName    索引名称
     * @param documentObjs 文档集合
     * @return 是否报错
     */
    public BulkResponse bulkAddDocument(String indexName, List documentObjs) {
        try {
            BulkRequest bulkRequest = new BulkRequest();
            documentObjs.forEach(doc -> {
                bulkRequest.add(new IndexRequest(indexName).source(JSON.toJSONString(doc), XContentType.JSON));
            });
            return restHighLevelClient.bulk(bulkRequest, ElasticSearchConfig.options);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据id查询文档
     *
     * @param indexName  索引名称
     * @param documentId 文档id
     */
    public GetResponse getDocumentById(String indexName, String documentId) {
        try {
            GetRequest request = new GetRequest(indexName, documentId);
            return restHighLevelClient.get(request, ElasticSearchConfig.options);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 短语匹配
     *
     * @param indexName 索引名称
     * @param field     字段名(中文需加.keyword)
     * @param text      检索值
     * @param size      每页条数
     * @param from      查询起点
     * @return 所有命中的文档
     */
    public SearchHit[] MatchPhraseQueryDocument(String indexName, String field, String text, int size, int from) {
        try {
            SearchRequest searchRequest = new SearchRequest(indexName);
            /**
             * 例如: QueryBuilders.termQuery("name.keyword","张三");
             * termQuery:精确查询 matchQuery:模糊查询
             */
            searchRequest.source(getSearchSourceBuilder(size, from, QueryBuilders.termQuery(field, text)));
            SearchResponse search = restHighLevelClient.search(searchRequest, ElasticSearchConfig.options);
            return search.getHits().getHits();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

//    public Long MatchPhraseQueryDocumentCount(String indexName, String field, String text){
//
//    }

    /**
     * 模糊匹配文档
     *
     * @param indexName 索引名称
     * @param field     字段名称
     * @param text      检索值
     * @param size      每页条数
     * @param from      检索起点
     * @return 命中的文档
     */
    public SearchHit[] matchQueryDocument(String indexName, String field, String text, int size, int from) {
        try {
            SearchRequest searchRequest = new SearchRequest(indexName);
            searchRequest.source(getSearchSourceBuilder(size, from, QueryBuilders.matchQuery(field, text)));
            SearchResponse search = restHighLevelClient.search(searchRequest, ElasticSearchConfig.options);
            return search.getHits().getHits();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询文档是否存在
     *
     * @param indexName  索引名称
     * @param documentId 文档id
     * @return 是否存在
     */
    public boolean isExistsDocument(String indexName, String documentId) {
        try {
            GetRequest request = new GetRequest(indexName, documentId);
            //不获取source上下文
            request.fetchSourceContext(new FetchSourceContext(false));
            return restHighLevelClient.exists(request, ElasticSearchConfig.options);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 根据id更新文档
     *
     * @param indexName   索引名称
     * @param documentId  文档名称
     * @param documentObj 文档实体
     * @return 响应
     */
    public UpdateResponse updateDocumentById(String indexName, String documentId, Object documentObj) {
        try {
            UpdateRequest updateRequest = new UpdateRequest(indexName, documentId);
            updateRequest.doc(JSON.toJSONString(documentObj), XContentType.JSON);
            return restHighLevelClient.update(updateRequest, ElasticSearchConfig.options);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据id删除文档
     *
     * @param indexName  索引名称
     * @param documentId 文档id
     * @return 是否删除成功
     */
    public boolean deleteDocumentById(String indexName, String documentId) {
        try {
            DeleteRequest deleteRequest = new DeleteRequest(indexName, documentId);
            restHighLevelClient.delete(deleteRequest, ElasticSearchConfig.options);
            //删除后根据id查询是否存在
            return !this.isExistsDocument(indexName, documentId);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 聚合查询
     *
     * @param indexName    索引名称
     * @param size         每页条数
     * @param from         检索起点
     * @param queryBuilder 查询条件构建
     * @return 命中的所有文档
     */
    public SearchHit[] boolQueryDocuments(String indexName, int size, int from, QueryBuilder queryBuilder) {
        try {
            SearchRequest searchRequest = new SearchRequest(indexName);
            searchRequest.source(getSearchSourceBuilder(size, from, queryBuilder));
            SearchResponse search = restHighLevelClient.search(searchRequest, ElasticSearchConfig.options);
            return search.getHits().getHits();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /** 查询总记录数
     *
     * @param indexName    索引名称
     * @param queryBuilder 查询条件构建
     * @return 命中的所有文档
     */
    public Long boolQueryDocumentCount(String indexName, QueryBuilder queryBuilder){
        try {
            CountRequest searchRequest = new CountRequest(indexName);
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(queryBuilder);
            searchRequest.source(searchSourceBuilder);
            CountResponse countResponse = restHighLevelClient.count(searchRequest,ElasticSearchConfig.options);
            return countResponse.getCount();
        }catch(IOException e){
            e.printStackTrace();;
        }
        return  null;
    }



    /**
     * 构建查询条件
     */
    private SearchSourceBuilder getSearchSourceBuilder(int size, int from, QueryBuilder queryBuilder) {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //分页 size:每页几条(默认10) from从第几条开始查
        searchSourceBuilder.size(size);
        searchSourceBuilder.from(from);
        searchSourceBuilder.query(queryBuilder);
        //设置查询超时时间
        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        System.out.println("request:" + searchSourceBuilder.toString());
        return searchSourceBuilder;
    }

    /**
     * 对象转json,返回转换接口
     */
    private String ObjToJson(Object obj) {
        try {
            return JSON.toJSONString(obj);
        } catch (Exception e) {
            e.printStackTrace();
            return getErrorMsg("Json转换异常");
        }
    }

    /**
     * 错误信息
     */
    private String getErrorMsg(String msg) {
        HashMap<String, Object> errorMsg = new HashMap<>();
        errorMsg.put("error", StringUtils.isEmpty(msg) ? "请求处理失败" : msg);
        return JSON.toJSONString(errorMsg);
    }

}

ES配置类

package org.bwf.study.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticSearchConfig {
    @Value("${elasticsearch.hostname}")
    private String hostName;
    @Value("${elasticsearch.port}")
    private int port;

    public static RequestOptions options = null;

    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
        options = builder.build();
    }

    @Bean(name = "restHighLevelClient")
    public RestHighLevelClient restHighLevelClient() {

        return new RestHighLevelClient(
                RestClient.builder(new HttpHost(hostName, port, "http"))
        );
    }

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值