ES: spring boot中使用ElasticsearchClient

一、依赖:(要根据不同版本的ES来调整依赖,否则会报错,不支持太低版本的ES,比如7.6以下的)

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            <version>3.2.3</version>
        </dependency>
        <dependency>
            <groupId>co.elastic.clients</groupId>
            <artifactId>elasticsearch-java</artifactId>
            <version>7.16.0</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.12.3</version>
        </dependency>

二、定义实体类:

package cn.edu.tju.test4;

public class Product {
    private String name;
    private String desc;
    private double price;

    public Product() {
    }

    public Product(String name, String desc, double price) {
        this.name = name;
        this.desc = desc;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Product{" +
                "name='" + name + '\'' +
                ", desc='" + desc + '\'' +
                ", price=" + price +
                '}';
    }
}

三、配置ElasticsearchClient

package cn.edu.tju.config;


import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticsearchClientConfig {

    @Value("${spring.elasticsearch.uris}")
    private String serverAddress;


    @Bean
    public ElasticsearchClient getElasticsearchClient(){
        RestClient restClient = RestClient
                .builder(HttpHost.create(serverAddress))
                .build();
        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());

        ElasticsearchClient esClient = new ElasticsearchClient(transport);
        return esClient;
    }
}

其中ES服务器的地址复用spring data ES 的spring.elasticsearch.uris

四、使用ElasticsearchClient操作索引

package cn.edu.tju.controller;

import cn.edu.tju.test4.Product;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.GetResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EsDemoController5 {
    @Autowired
    private ElasticsearchClient esClient;
    @RequestMapping("/test6")
    public String test6() throws Exception{
        GetResponse<Product> productGetResponse = esClient.get(g -> g
                        .index("demo7")
                        .id("1"),
                Product.class
        );
        String result = productGetResponse.source().toString();
        return result;
    }
}

其中ES 7.16.2中demo7这个索引的mapping如下:

{
  "demo7" : {
    "mappings" : {
      "properties" : {
        "desc" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "price" : {
          "type" : "float"
        }
      }
    }
  }
}

相关推荐

  1. ES: spring boot使用ElasticsearchClient

    2024-03-20 06:32:06       39 阅读
  2. C++使用汇编

    2024-03-20 06:32:06       45 阅读
  3. Express使用Swagger

    2024-03-20 06:32:06       65 阅读
  4. ElasticSeach--springboot使用

    2024-03-20 06:32:06       59 阅读
  5. Linuxsystemctl使用

    2024-03-20 06:32:06       63 阅读
  6. React使用WebRTC

    2024-03-20 06:32:06       60 阅读
  7. typescript使用 ?. ?? ??= 运算符

    2024-03-20 06:32:06       52 阅读
  8. gin使用JWT

    2024-03-20 06:32:06       65 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-20 06:32:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-20 06:32:06       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-20 06:32:06       82 阅读
  4. Python语言-面向对象

    2024-03-20 06:32:06       91 阅读

热门阅读

  1. nmon监控工具功能全解析

    2024-03-20 06:32:06       42 阅读
  2. AIGC从入门到精通

    2024-03-20 06:32:06       36 阅读
  3. 图像处理学习笔记(一)

    2024-03-20 06:32:06       39 阅读
  4. C# Winform实现数据双向绑定

    2024-03-20 06:32:06       45 阅读
  5. GPT系列模型的特点

    2024-03-20 06:32:06       44 阅读
  6. 抖音商城小店电话采集使用教程

    2024-03-20 06:32:06       125 阅读
  7. Vue 3 之 嵌套路由

    2024-03-20 06:32:06       37 阅读
  8. C/C++蓝桥杯之卡片问题

    2024-03-20 06:32:06       41 阅读