一、前言
注:本文实现了Java对Elasticseach的分页检索/不分页检索的封装
ES就不用过多介绍了,直接上代码:
二、实现步骤: 创建Store类(与ES字段对应,用于接收ES数据) import com.alibaba.fastjson2.annotation.JSONField; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Field; /** * indexName 索引名称 理解为数据库名 限定小写 * type 理解为数据库的表名称 * shards 默认分区数 * replicas 每个分区默认的备份数 * refreshInterval 刷新间隔 * indexStoreType 索引文件存储类型 **/ @Data @Builder @NoArgsConstructor @AllArgsConstructor @Document(indexName = yourIndex, shards = n, replicas = n, refreshInterval = n) public class Store implements Serializable { /** * 属性,与ES字段对应 * 注:@Field注解: * 若Java属性名与ES字段名不同,则根据@Field的name属性匹配ES字段 * 若Java属性名与ES字段名完全相同,则不用加该注解或name属性 */ @Field(name ="yourESfield") private String yourAttribute; } Elasticsearch全文检索接口:不分页检索 import com.alibaba.fastjson2.JSON; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.sort.SortOrder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @Slf4j @Component public class Elasticsearch{ @Autowired private RestHighLevelClient restHighLevelClient; /** * 不分页检索 * * @param yourIndex 索引 * @param builder 搜索条件 * @param sortField 排序属性 * @param sortOrder 排序方式 * @param clazz 返回类 * @param
泛型 * @return 分页表格 */ @SuppressWarnings("checkstyle:ParameterNumber") public List searchListByCond(String yourIndex, BoolQueryBuilder builder, String sortField, SortOrder sortOrder, Class clazz) { SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(builder); if (StringUtils.isNotBlank(sortField)) { sourceBuilder.sort(sortField, sortOrder); } SearchRequest request = new SearchRequest(yourIndex); request.source(sourceBuilder); SearchResponse response = null; try { response = restHighLevelClient.search(request, RequestOptions.DEFAULT); } catch (Exception e) { log.error("Search Elasticsearch Error!", e); } if (response == null) { return Collections.emptyList(); } return Arrays.stream(response.getHits().getHits()).map(v -> JSON.parseObject(v.getSourceAsString(), clazz)).collect(Collectors.toList()); } } Elasticsearch全文检索接口:分页检索 /** * 分页检索 * * @param yourIndex 索引 * @param builder 搜索条件 * @param page 分页参数(自己封装一个分页类,网上很多,easy) * @param sortField 排序属性 * @param sortOrder 排序方式 * @param clazz 返回类 * @param 泛型 * @return 分页表格 */ @SuppressWarnings("checkstyle:ParameterNumber") public Page searchPageByCond(String yourIndex, BoolQueryBuilder builder, Page page, String sortField, SortOrder sortOrder, Class clazz) { SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(builder); sourceBuilder.from(Math.max(page.getOffset(), 0)); sourceBuilder.size(page.getLimit()); if (StringUtils.isNotBlank(sortField)) { sourceBuilder.sort(sortField, sortOrder); } SearchRequest request = new SearchRequest(yourIndex); request.source(sourceBuilder); SearchResponse response = null; try { response = restHighLevelClient.search(request, RequestOptions.DEFAULT); } catch (Exception e) { log.error("Search Elasticsearch Error!", e); } if (response == null) { page.setRows(Collections.emptyList()); page.setTotal(0); return page; } page.setRows(Arrays.stream(response.getHits().getHits()).map(v -> JSON.parseObject(v.getSourceAsString(), clazz)).collect(Collectors.toList())); page.setTotal((int) response.getHits().getTotalHits().value); return page; } 三、总结 本文实现了Java对Elasticsearch搜索引擎全文检索的封装
传入索引、BoolQueryBuilder查询条件(选传)、排序方式(选传)、Store数据接收类
即可完成对Elasticsearch的全文分页检索、不分页检索