zoukankan      html  css  js  c++  java
  • springboot 配置elasticsearch Java High Rest Client

    前提声明

    在新版本的spring boot中逐渐放弃了对Spring Data Elasticsearch的支持,所以不推荐使用,使用ES官方推出的Java High Rest Client.

    引入依赖

    <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-high-level-client</artifactId>
                <version>7.3.2</version>
            </dependency>
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-client</artifactId>
                <version>7.3.2</version>
            </dependency>
            <dependency>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch</artifactId>
                <version>7.3.2</version>
            </dependency>
    

    我们用到的只是elasticsearch-rest-high-level-client,但这三个都要有,不然就会报错。我最初没有加入elasticsearch-rest-client,提示

    Caused by: java.lang.NoClassDefFoundError: org/elasticsearch/action/admin/indices/analyze/AnalyzeRequest
    

    查阅官方文档发现了它们之间有依赖关系:

    Dependenciesedit
    The High Level Java REST Client depends on the following artifacts and their transitive dependencies:
    
    org.elasticsearch.client:elasticsearch-rest-client
    org.elasticsearch:elasticsearch
    

    而且版本号尽量一致,不一致可能会导致某些api不可用。

    说明一下,这个项目我在用的时候版本号最新的是8.0了,但因为我docker安装的版本是7.3.2,所以都改成了一样的,大家有个参考。

    application.yml配置

    spring
      elasticsearch:
        rest:
          uris: http://192.168.0.1:9200
    

    简单使用

    贴一段代码给大家看一下吧,官网都有的,相应转换一下就可以了,我的只是参考一下,欢迎指正。

    /**
     * @author Gyyyang
     * @date 2019/7/23
     */
    @Repository
    class VehicleAlarmSaveRepositoryImpl @Autowired constructor(
            private var client: RestHighLevelClient
    ):VehicleAlarmSaveRepository {
    
        private val logger = LoggerFactory.getLogger("saveAlarm")
    
        private val response = object : ActionListener<IndexResponse> {
            override fun onFailure(exception: Exception) {
                exception.printStackTrace()
            }
    
            override fun onResponse(response: IndexResponse) {
                logger.info("CH:报警索引建立成功 EN:alarm Index create success")
            }
        }
    
        override fun saveAlarm(alarmMessage: VehicleAlarmMessage) {
            val message = JSON.toJSONString(alarmMessage)
            val indexRequest = IndexRequest("vehicle-alarm").source(message,XContentType.JSON)
            client.indexAsync(indexRequest, RequestOptions.DEFAULT,response)
        }
    }
    // 懒得截取就全贴出来吧~~
    

    个人理解,不作为教程,欢迎交流指正!

  • 相关阅读:
    数据库无限分级(分类表)
    从SQLserver中导出表数据到Access
    C# 对JS解析AJX请求JSON并绑定到html页面的一些心得
    高效的SQLSERVER分页查询(转载)
    Entity Framework自用的DataBase基类
    简单SVN使用方法
    CMD终端关于pip报错,scrapy报错的一种处理方法
    [原创]使用python对视频/音频文件进行详细信息采集,并进行去重操作
    Primer C++第五版 读书笔记(一)
    分享一个编程学习网站:https://github.com/justjavac/free-programming-books-zh_CN
  • 原文地址:https://www.cnblogs.com/gyyyblog/p/11756765.html
Copyright © 2011-2022 走看看