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)
        }
    }
    // 懒得截取就全贴出来吧~~
    

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

  • 相关阅读:
    基于cocos2d-x的跑酷游戏,不同高度地面的碰撞检測demo,有兴趣能够看一看
    Dynamics CRM Form表单中通过javascript抓取触发change事件字段的属性名
    【项目实战】---使用ajax完毕username是否存在异步校验
    任务调度(三)——Timer的替代品ScheduledExecutorService简单介绍
    UML——用例图
    frameset怎样实现整个页面的跳转
    linux下怎样用c语言调用shell命令
    替换谷歌自带的安卓开发虚拟机?何不试试以下的虚拟机
    android环境下两种md5加密方式
    IOS-Storyboard控制器切换之Modal(1)
  • 原文地址:https://www.cnblogs.com/gyyyblog/p/11756765.html
Copyright © 2011-2022 走看看