ES: spring boot中使用ElasticsearchClient
其中ES服务器的地址复用spring data ES 的spring.elasticsearch.uris。一、依赖:(要根据不同版本的ES来调整依赖,否则会报错,不支持太低版本的ES,比如7.6以下的)四、使用ElasticsearchClient操作索引。三、配置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"
}
}
}
}
}
更多推荐
已为社区贡献8条内容
所有评论(0)