我们这里使用的是ES7.5.1

第一步:引入pom依赖


        <!-- Spring Boot Elasticsearch 依赖 start -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.5.1</version>
            <exclusions>
                <exclusion>
                    <groupId>org.elasticsearch</groupId>
                    <artifactId>elasticsearch</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.elasticsearch.client</groupId>
                    <artifactId>elasticsearch-rest-client</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>7.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.5.1</version>
        </dependency>
        <!-- Spring Boot Elasticsearch 依赖 end -->
       <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3</version>
        </dependency>

第二部:配置文件

spring:
  es:
    host: 172.16.1.61
    port: 9200
    scheme: http

第三步:配置类


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

@Configuration
public class ElasticConfig {

    @Value("${spring.es.host}")
    public String host;

    @Value("${spring.es.port}")
    public int port;

    @Value("${spring.es.scheme}")
    public String scheme;

    @Bean
    public RestClientBuilder restClientBuilder() {
        return RestClient.builder(makeHttpHost());
    }

    @Bean
    public RestClient restClient(){
        return RestClient.builder(new HttpHost(host, port, scheme)).build();
    }

    private HttpHost makeHttpHost() {
        return new HttpHost(host, port, scheme);
    }

    @Bean
    public RestHighLevelClient restHighLevelClient(@Autowired RestClientBuilder restClientBuilder){
        return new RestHighLevelClient(restClientBuilder);
    }
}

第四步:公共类


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdas.integral.record.common.base.BaseQueryVO;
import com.fdas.integral.record.common.util.StrKit;
import com.fdas.integral.record.common.util.StringUtil;
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.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.security.sasl.SaslException;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @author
 * @date
 */
@Service
public class BaseEsService {

    @Resource
    private RestHighLevelClient restHighLevelClient;

    private String getEntityTableIndex(Object entity){
        TableName annotation = entity.getClass().getAnnotation(TableName.class);
        return annotation.value();
    }

    /**
     * ES 保存,如果 id 相同,则更新
     * @param entity 保存的实体类,实体类需要有id字段作为主键 并需要有 TableName 注解,这里取表名称作为文档名称
     * @return
     * @throws IOException
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    public boolean saveEntity(Object entity) throws IOException, NoSuchFieldException, IllegalAccessException {
        IndexRequest request = new IndexRequest();
        request.index(getEntityTableIndex(entity));
        Field idField = entity.getClass().getDeclaredField("id");
        idField.setAccessible(true);
        Object idValue = idField.get(entity);
        if(idValue == null){
            throw new SaslException("主键不能为空");
        }
        request.id(idValue.toString());
        request.source(JSONUtil.parse(entity).toStringPretty(), XContentType.JSON);
        IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
        return response.isFragment();
    }

    /**
     * 查询类 查询条件组装
     * @param sourceBuilder 查询条件构造器
     * @param vo 查询条件
     * @param <T> 返回的数据类型
     */
    private <T extends BaseQueryVO> void buildQueryBuilder(SearchSourceBuilder sourceBuilder,T vo){
        BoolQueryBuilder builder = QueryBuilders.boolQuery();
        String orderField = vo.getOrderField();
        if(!StrKit.isEmpty(orderField)){
            SortOrder sortOrder;
            if(vo.getOrderType() == 1){
                sortOrder = SortOrder.DESC;
            }else{
                sortOrder = SortOrder.ASC;
            }
            sourceBuilder.sort(orderField,sortOrder);
        }
        Class<? extends BaseQueryVO> aClass = vo.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        for(Field field: declaredFields){
            field.setAccessible(true);
            try {
                String name = field.getName();
                Object queryFieldVal = field.get(vo);
                if(StringUtil.isNotEmptyOrNull(queryFieldVal)){
                    builder.must(QueryBuilders.matchQuery(name, queryFieldVal).minimumShouldMatch("100%"));
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        sourceBuilder.query(builder);
    }

    /**
     *
     * @param vo 查询实体类,需要继承公共的类 BaseQueryVO 包含分页信息
     * @param clazz 返回数据类型
     * @param <T> 查询类
     * @param <D> 返回类
     * @return
     * @throws IOException
     */
    public <T extends BaseQueryVO, D> IPage<D> queryPage(T vo, Class<D> clazz) throws IOException {
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.from((vo.getCurPage() - 1) * vo.getPageSize());
        sourceBuilder.size(vo.getPageSize());
        buildQueryBuilder(sourceBuilder,vo);

        SearchRequest rq = new SearchRequest();
        rq.indices(getEntityTableIndex(vo));
        //各种组合条件
        rq.source(sourceBuilder);
        //请求
        SearchResponse rp = restHighLevelClient.search(rq, RequestOptions.DEFAULT);
        SearchHits hits = rp.getHits();
        List<D> collect = Arrays.stream(hits.getHits()).map(v->{
            D returnObj = null;
            try {
                returnObj = clazz.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            JSONObject parse = JSONUtil.parseObj(v.getSourceAsString());
            BeanUtil.copyProperties(parse,returnObj);
            return returnObj;
        }).collect(Collectors.toList());
        long total = hits.getTotalHits().value;
        IPage<D> page = new Page<>();
        page.setTotal( total);
        page.setRecords(collect);
        return page;
    }

}

第五步:查询类父类


import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: louyz
 * @date: 2019-10-23 17:50
 * @description:
 */
@Data
public class BaseQueryVO {
	//页面显示长度
    @ApiModelProperty(value = "页面长度",example = "10",required = true)
    private Integer pageSize;

    //显示当前页
    @ApiModelProperty(value = "页码",example = "1",required = true)
    private Integer curPage;

    //显示当前页
    @ApiModelProperty(value = "排序字段")
    private String orderField;

    //显示当前页
    @ApiModelProperty(value = "排序类型,1正序,2倒序")
    private Integer orderType = 2;

    /**
     * 查询条件
     * 例如:key:name value: like name字段 左右模糊查询
     *      key:name value: likeRight 右边模糊查询
     *      key:name value: likeLeft 左边模糊查询
     */
    @ApiModelProperty(value = "查询条件,key:查询的字段,value:查询的方式")
    private Map<String,String> conditions = new HashMap();

}

 

最后修改于 2022-05-13 18:32:02
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付
上一篇