草庐IT

【ElasticSearch8】springboot整合es8(一),实现简单查询

我要4级了 2024-05-23 原文

目录

版本说明:

开始代码

maven依赖:

配置yml:

连接es配置文件:

开发查询接口

新增对象

新增查询工具类

新增查询service

新增查询接口

测试结果​编辑


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

api地址:

Connecting | Elasticsearch Java API Client [8.1] | Elastichttps://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/connecting.html

版本说明:

springboot:2.1.8-RELEASE

ElasticSearch: 8.1(非集群)

JDK: 1.8

开发工具:IDEA

框架:springboot+mybatisplus

数据库:mysql 5.7

开始代码

springboot和ElasticSearch搭建步骤就省略了

maven依赖:

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

            <dependency>
				<groupId>co.elastic.clients</groupId>
				<artifactId>elasticsearch-java</artifactId>
				<version>8.1.2</version>
			</dependency>
			<dependency>
				<groupId>org.elasticsearch.client</groupId>
				<artifactId>elasticsearch-rest-client</artifactId>
				<version>8.1.2</version>
			</dependency>
			<dependency>
				<groupId>org.glassfish</groupId>
				<artifactId>jakarta.json</artifactId>
				<version>2.0.1</version>
			</dependency>

配置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

开发查询接口

统一返回结果对象


/**
 * 响应数据
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0
 */

@ApiModel(value = "响应")
public class Result<T> implements Serializable {


    private static final long serialVersionUID = 1L;
    /**
     * 编码:0表示成功,其他值表示失败
     */
    @ApiModelProperty(value = "编码:0表示成功,其他值表示失败 2表示有弱提醒")
    @JsonProperty("Code")
    public int Code = 0;
    /**
     * 消息内容
     */
    @ApiModelProperty(value = "消息内容")
    @JsonProperty("Message")
    public String Message = "操作成功";

    /**
     * 是否成功
     */
    @ApiModelProperty(value = "是否成功")
    @JsonProperty("Success")
    public boolean Success = true;
    /**
     * 响应数据
     */
    @ApiModelProperty(value = "响应数据")
    public T Response;

    public Result<T> ok(T Response) {
        this.Response = Response;
        return this;
    }
    public Result<T> ok() {
        this.Response = null;
        return this;
    }
    public Result<T> ok(T Response,String message) {
        this.Response = Response;
        this.Message = message;
        return this;
    }

    public boolean success() {
        return Code == 0 ? true : false;
    }

    public Result<T> error() {
        this.Success = false;
        this.Code = 500;
        this.Message = "系统错误";
        return this;
    }

    public Result<T> error(int code) {
        this.Success = false;
        this.Code = code;
        return this;
    }

    public Result<T> error(int code, String Message) {
        this.Success = false;
        this.Code = code;
        this.Message = Message;
        return this;
    }

    public Result<T> error(String Message) {
        this.Success = false;
        this.Code = 500;
        this.Message = Message;
        return this;
    }

    public static Result OK() {
        return new Result(0,"操作成功",null);
    }
    public static Result OK(Object rtnData) {
        return new Result(0,"操作成功",rtnData);
    }
    public static Result TIP(Object rtnData,String message) {
        return new Result(2,message,rtnData);
    }
    public static Result OK(Object rtnData,String message) {
        return new Result(0,message,rtnData);
    }


    public static Result rtn(Integer code, String Message, Object rtnData) {
        return new Result(1,Message,rtnData);
    }


    public static Result ERROR(String Message) {
        return new Result(1,Message,null);
    }
    public static Result ERROR(Integer code,String Message) {
        return new Result(code,Message,null);
    }


    public Result(int code, String message, T response) {
        Code = code;
        Message = message;
        Response = response;
        Success = code == 0?true:false;
    }

    public Result() {
    }
}

新增对象

业务对象实体:

@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<T> {

    /**
     * @author liuch
     * @description 根据关键字分页查询
     * @date 2022/4/2 17:15
     * @param 
     * @param client
     * @param dto
     * @param target 
     * @return java.util.List<T>
     */
    public List<T> queryByFiled(ElasticsearchClient client, EsQueryDTO dto,Class<T> target) throws Exception {
        List<T> result = new ArrayList<>();
        SearchResponse<HashMap> 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<Hit<HashMap>> hits = search.hits().hits();
        Iterator<Hit<HashMap>> iterator = hits.iterator();
        while (iterator.hasNext()){
            Hit<HashMap> decodeBeanHit = iterator.next();
            Map<String,Object> 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<T>
     */
    public PageData<T> queryPageByFiled(ElasticsearchClient client, EsQueryDTO dto,Class<T> target) throws Exception {
        long total = queryCountByFiled(client,dto);
        List<T> result = queryByFiled(client,dto,target);
        PageData<T> 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<T> target) throws Exception {
        GetResponse<HashMap> getResponse = client.get(s -> s
                        .index(dto.getIndexName()).id(dto.getWord()),
                HashMap.class);
        getResponse.source();
        Map<String,Object> 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<HdiotDecodeDTO> pageData = new ElasticClientUtils<HdiotDecodeDTO>().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 + mybatis启动时执行sql脚本_大师兄小灰灰的博客-CSDN博客springboot + mybatis启动时执行sql脚本https://blog.csdn.net/qq_24473507/article/details/126094224

linux安装nginx_大师兄小灰灰的博客-CSDN博客系统环境:CentOs7.9nginx下载地址:nginx: download前期准备安装依赖程序包:1.先安装gcc-c++编译器yum install -y gcc-c++ yum install -y openssl openssl-devel 2.再安装pcre包yum install -y pcre pcre-devel3.再安装zlib包yum install -y zlib zlib-devel下面进行nginx的安装下载并解压.https://blog.csdn.net/qq_24473507/article/details/121973007

发文助手提醒我:

文章质量提示

  • 此文章质量较低,不会获得较多流量扶持! 可能的原因为:篇幅太短,广告涉嫌违规,外链过多,缺少代码,图片涉嫌违规。

请问要怎么样才能满足csdn的要求呢?搞一万字?

————————————————
版权声明:本文为CSDN博主「大师兄小灰灰」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_24473507/article/details/123924463

有关【ElasticSearch8】springboot整合es8(一),实现简单查询的更多相关文章

  1. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  2. ruby - 如何根据特征实现 FactoryGirl 的条件行为 - 2

    我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden

  3. ruby-on-rails - 在 Rails 和 ActiveRecord 中查询时忽略某些字段 - 2

    我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr

  4. ruby - 简单获取法拉第超时 - 2

    有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url

  5. ruby - 用 Ruby 编写一个简单的网络服务器 - 2

    我想在Ruby中创建一个用于开发目的的极其简单的Web服务器(不,不想使用现成的解决方案)。代码如下:#!/usr/bin/rubyrequire'socket'server=TCPServer.new('127.0.0.1',8080)whileconnection=server.acceptheaders=[]length=0whileline=connection.getsheaders想法是从命令行运行这个脚本,提供另一个脚本,它将在其标准输入上获取请求,并在其标准输出上返回完整的响应。到目前为止一切顺利,但事实证明这真的很脆弱,因为它在第二个请求上中断并出现错误:/usr/b

  6. ruby-on-rails - 简单的 Ruby on Rails 问题——如何将评论附加到用户和文章? - 2

    我意识到这可能是一个非常基本的问题,但我现在已经花了几天时间回过头来解决这个问题,但出于某种原因,Google就是没有帮助我。(我认为部分问题在于我是一个初学者,我不知道该问什么......)我也看过O'Reilly的RubyCookbook和RailsAPI,但我仍然停留在这个问题上.我找到了一些关于多态关系的信息,但它似乎不是我需要的(尽管如果我错了请告诉我)。我正在尝试调整MichaelHartl'stutorial创建一个包含用户、文章和评论的博客应用程序(不使用脚手架)。我希望评论既属于用户又属于文章。我的主要问题是:我不知道如何将当前文章的ID放入评论Controller。

  7. ruby - 使用 Ruby 通过 Outlook 发送消息的最简单方法是什么? - 2

    我的工作要求我为某些测试自动生成电子邮件。我一直在四处寻找,但未能找到可以快速实现的合理解决方案。它需要在outlook而不是其他邮件服务器中,因为我们有一些奇怪的身份验证规则,我们需要保存草稿而不是仅仅发送邮件的选项。显然win32ole可以做到这一点,但我找不到任何相当简单的例子。 最佳答案 假设存储了Outlook凭据并且您设置为自动登录到Outlook,WIN32OLE可以很好地完成此操作:require'win32ole'outlook=WIN32OLE.new('Outlook.Application')message=

  8. 华为OD机试用Python实现 -【明明的随机数】 2023Q1A - 2

    华为OD机试题本篇题目:明明的随机数题目输入描述输出描述:示例1输入输出说明代码编写思路最近更新的博客华为od2023|什么是华为od,od薪资待遇,od机试题清单华为OD机试真题大全,用Python解华为机试题|机试宝典【华为OD机试】全流程解析+经验分享,题型分享,防作弊指南华为o

  9. 使用canal同步MySQL数据到ES - 2

    文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co

  10. 基于C#实现简易绘图工具【100010177】 - 2

    C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.

随机推荐