springboot+ElasticSearch8开发,实现简单查询

第一次用springboot连接es8,先用了springboot自带的jar做连接,结果失败了,后来才知道es7以后就不支持template的连接方式,自己踩了不少坑,这里参考官方的api做了一个demo,供各位大佬参考,有哪里不对的欢迎各路大神批评指正

api地址:

Connecting | Elasticsearch Java API Client [8.1] | Elastic

版本说明:

springboot:2.1.8-RELEASE

ElasticSearch: 8.1(非集群)

JDK: 1.8

开始代码

springboot和ElasticSearch搭建步骤就省略了

maven依赖:

springboot的2.1.8-RELEASE版本不支持8.1版本的es,因此直接使用官方推荐的maven依赖包

            
				co.elastic.clients
				elasticsearch-java
				8.1.2
			
			
				org.elasticsearch.client
				elasticsearch-rest-client
				8.1.2
			
			
				org.glassfish
				jakarta.json
				2.0.1
			

配置yml:

因为后面要自己写代码连接es,这里配置自己加了一些东西,es再搭建的时候设置了登陆认证

spring:
  elasticsearch:
    rest:
      # 是否启用es
      enable: false
      uris: 127.0.0.1:9200
      host: 127.0.0.1
      port: 9200
      username: elastic
      password: 123456

连接es配置文件:

@Configuration
public class ElasticSearchConfig {

    @Value("${spring.elasticsearch.rest.host}")
    private String host;
    @Value("${spring.elasticsearch.rest.enable:true}")
    private boolean enable;

    @Value("${spring.elasticsearch.rest.port}")
    private int port;
    @Value("${spring.elasticsearch.rest.username}")
    private String userName;

    @Value("${spring.elasticsearch.rest.password}")
    private String passWord;


    //注入IOC容器
    @Bean
    public ElasticsearchClient elasticsearchClient(){
        ElasticsearchClient client = new ElasticsearchClient(null);
        if (enable){
            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            //设置账号密码
            credentialsProvider.setCredentials(
                    AuthScope.ANY, new UsernamePasswordCredentials(userName, passWord));

//        RestClients restClients =
            RestClient restClient = RestClient.builder(new HttpHost(host, port))
                    .setHttpClientConfigCallback(httpClientBuilder->httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)).build();

            ElasticsearchTransport transport = new RestClientTransport(restClient,new JacksonJsonpMapper());
            // And create the API client
            client = new ElasticsearchClient(transport);
        }
        return client;

    }
}

这时候可以启动代码监控下日志,看是否连接成功es

开发查询接口

新增对象

业务对象实体:

@Data
@Accessors(chain = true)
public class HdiotDecodeDTO {


    private String appid;

    private String pid;

    private String drvid;

    private String map_devid;

    private String pcode;

    private String protocol;

    private String devid;

    private String sn;

    private String imei;

    private String messageid;

    private Object data;

    private String session;

    private Date created;

    private Date changed;

    @Override
    public String toString() {
        return "HdiotDecodeBean{" +
                "appid='" + appid + '\'' +
                ", pid='" + pid + '\'' +
                ", drvid='" + drvid + '\'' +
                ", devid='" + devid + '\'' +
                ", sn='" + sn + '\'' +
                ", imei='" + imei + '\'' +
                ", messageid='" + messageid + '\'' +
                ", data=" + data +
                ", session='" + session + '\'' +
                ", created=" + created +
                ", changed=" + changed +
                '}';
    }
}

查询对象实体:

@Data
public class EsQueryDTO {
    @ApiModelProperty("索引名称")
    private String indexName;
    @ApiModelProperty("关键字属性")
    private String field;
    @ApiModelProperty("关键字")
    private String word;
    @ApiModelProperty("起始行")
    private Integer from;
    @ApiModelProperty("页数")
    private Integer size;

    public Integer getSize() {
        return size==0?30:size;
    }
}

新增查询工具类


import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.CountResponse;
import co.elastic.clients.elasticsearch.core.GetResponse;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import com.alibaba.fastjson.JSON;
import com.hdkj.hdiot.configure.common.PageData;
import com.hdkj.hdiot.configure.es.bean.EsQueryDTO;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.poi.ss.formula.functions.T;

import java.io.IOException;
import java.util.*;

/**
 * @author liuch
 * @title: ElasticClientUtils
 * @description: TODO
 * @date 2022/4/2 9:50
 */
public class ElasticClientUtils {

    /**
     * @author liuch
     * @description 根据关键字分页查询
     * @date 2022/4/2 17:15
     * @param 
     * @param client
     * @param dto
     * @param target 
     * @return java.util.List
     */
    public List queryByFiled(ElasticsearchClient client, EsQueryDTO dto,Class target) throws Exception {
        List result = new ArrayList<>();
        SearchResponse search = client.search(s -> s
                        .index(dto.getIndexName())
                        .query(q -> q.term(t -> t
                                .field(dto.getField())
                                .value(dto.getWord())
                        )).from(dto.getFrom()).size(dto.getSize()),
                HashMap.class);
        List> hits = search.hits().hits();
        Iterator> iterator = hits.iterator();
        while (iterator.hasNext()){
            Hit decodeBeanHit = iterator.next();
            Map docMap = decodeBeanHit.source();
            String json = JSON.toJSONString(docMap);
            T obj  = JSON.parseObject(json,target);
            result.add(obj);
        }
        return result;
    }


    /**
     * @author liuch
     * @description 根据关键字查询总条数
     * @date 2022/4/2 17:15
     * @param 
     * @param client
     * @param dto 
     * @return long
     */
    public long queryCountByFiled(ElasticsearchClient client, EsQueryDTO dto) throws Exception {
        CountResponse count = client.count(c -> c.index(dto.getIndexName()).query(q -> q.term(t -> t
                .field(dto.getField())
                .value(dto.getWord())
        )));
        long total = count.count();
        return total;
    }
    /**
     * @author liuch
     * @description 查询分页信息
     * @date 2022/4/2 17:16
     * @param 
     * @param client
     * @param dto
     * @param target 
     * @return com.hdkj.hdiot.configure.common.PageData
     */
    public PageData queryPageByFiled(ElasticsearchClient client, EsQueryDTO dto,Class target) throws Exception {
        long total = queryCountByFiled(client,dto);
        List result = queryByFiled(client,dto,target);
        PageData pageData = new PageData<>(result,total);
        return pageData;
    }

    /**
     * @author liuch
     * @description 根据文档id查询
     * @date 2022/4/2 17:16
     * @param 
     * @param client
     * @param dto
     * @param target 
     * @return java.lang.Object
     */
    public Object queryByDocumentId(ElasticsearchClient client, EsQueryDTO dto,Class target) throws Exception {
        GetResponse getResponse = client.get(s -> s
                        .index(dto.getIndexName()).id(dto.getWord()),
                HashMap.class);
        getResponse.source();
        Map docMap = getResponse.source();
        String json = JSON.toJSONString(docMap);
        T obj  = JSON.parseObject(json,target);
        return obj;
    }

}

新增查询service

public interface HdiotDecodeService {

    Result getDecodeMsg(EsQueryDTO dto);
}

实现类:

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import com.hdkj.hdiot.configure.common.PageData;
import com.hdkj.hdiot.configure.common.Result;
import com.hdkj.hdiot.configure.es.bean.EsQueryDTO;
import com.hdkj.hdiot.configure.es.bean.HdiotDecodeDTO;
import com.hdkj.hdiot.configure.es.bean.HdiotOriginalDTO;
import com.hdkj.hdiot.configure.es.service.HdiotDecodeService;
import com.hdkj.hdiot.configure.es.utils.ElasticClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author liuch
 * @title: HdiotDecodeServiceImpl
 * @description: TODO
 * @date 2022/4/1 11:39
 */
@Service
public class HdiotDecodeServiceImpl implements HdiotDecodeService {

    @Autowired
    private ElasticsearchClient client;

    /**
     * @author liuch
     * @description 查询原始数据
     * @date 2022/4/2 10:56
     * @param
     * @param dto
     * @return com.hdkj.hdiot.configure.common.Result
     */
    @Override
    public Result getDecodeMsg(EsQueryDTO dto) {
        // decode_data  origin_data
        try {
            dto.setIndexName("decode_data");
            dto.setField("devid");
            PageData pageData = new ElasticClientUtils().queryPageByFiled(client,dto,HdiotDecodeDTO.class);
            return new Result().ok(pageData);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result().error(e.getMessage());
        }
    }

}

新增查询接口

    @Autowired
    HdiotDecodeService decodeService;

    @PostMapping(value = "/decodeMsg")
    public Result getDecodeMsg(@RequestBody EsQueryDTO dto){
        return decodeService.getDecodeMsg(dto);
    }

测试结果springboot+ElasticSearch8开发,实现简单查询_第1张图片

你可能感兴趣的:(微服务,elasticsearch,spring,elasticsearch)