JAVA全系列 教程
3762个小节阅读:7094.7k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
Java原生代码可以操作Elasticsearch,但操作比较繁琐,类似于数据库中的JDBC,我们还需要将ES文档手动封装为Java对象。所以开发中我们一般使用框架操作Elasticsearch。
Spring Data ElasticSearch是JAVA操作Elasticsearch的框架。它通过对原生API的封装,使得JAVA程序员可以简单的对Elasticsearch进行操作。
创建SpringBoot项目,加入Spring Data Elasticsearch起步依赖:
xxxxxxxxxx
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
编写配置文件,连接elasticsearch
xxxxxxxxxx
spring:
elasticsearch:
uris: https://192.168.0.187:9200
username: elastic
password: elastic
#日志格式
logging:
pattern:
console: '%d{HH:mm:ss.SSS} %clr(%-5level) --- [%-15thread] %cyan(%-50logger{50}):%msg%n'
创建配置类,跳过SSL证书检查。
从ES8开始,访问ES的协议从http变成了https,访问https请求需要SSL证书,在开发环境下我们不需要配置该证书,在项目中添加一个配置类,跳过SSL证书检查即可。
xxxxxxxxxx
@Component
public class RestClientBuilderCustomizerImpl implements RestClientBuilderCustomizer {
@Override
public void customize(RestClientBuilder builder) {
}
/**
* 跳过SSL的证书检查
*/
@Override
public void customize(HttpAsyncClientBuilder builder) {
SSLContextBuilder sscb = SSLContexts.custom();
try {
sscb.loadTrustMaterial((chain, authType) -> {
return true;
});
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (KeyStoreException e) {
throw new RuntimeException(e);
}
try {
builder.setSSLContext(sscb.build());
} catch (KeyManagementException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
}
创建实体类:
xxxxxxxxxx
// 一个实体类的所有对象都会存入ES的一个索引中,所以我们在创建实体类时关联ES索引
@Document(indexName = "product",createIndex = true)
public class Product {
@Id
@Field(type = FieldType.Integer,store = true,index = true)
private Integer id;
@Field(type = FieldType.Text,store = true,index = true,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
private String productName;
@Field(type = FieldType.Text,store = true,index = true,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
private String productDesc;
}
@Document:标记在类上,标记实体类为文档对象,一般有如下属性:
indexName:对应索引的名称
createIndex:是否自动创建索引
@Id:标记在成员变量上,标记一个字段为主键,该字段的值会同步到ES该文档的id值。
@Field:标记在成员变量上,标记为文档中的域,一般有如下属性:
type:域的类型
index:是否创建索引,默认是 true
store:是否单独存储,默认是 false
analyzer:分词器
searchAnalyzer:搜索时的分词器
创建Repository接口
xxxxxxxxxx
// Repository接口继承ElasticsearchRepository,该接口提供了文档的增删改查方法
@Repository
public interface ProductRepository extends ElasticsearchRepository<Product,Integer> {
}
测试Repository接口的方法
xxxxxxxxxx
@SpringBootTest
public class ProductRepositoryTest {
@Autowired
private ProductRepository repository;
@Test
public void addProduct(){
Product product = new Product(1, "iphone30", "iphone30是苹果最新手机");
repository.save(product);
}
@Test
public void updateProduct(){
Product product = new Product(1, "iphone31", "iphone31是苹果最新手机");
repository.save(product);
}
@Test
public void findAllProduct(){
Iterable<Product> all = repository.findAll();
for (Product product : all) {
System.out.println(product);
}
}
@Test
public void findProductById(){
Optional<Product> product = repository.findById(1);
System.out.println(product.get());
}
@Test
public void deleteProduct(){
repository.deleteById(1);
}
}
实时效果反馈
1. 在Spring Data ElasticSearch中,实体类上方添加的注解为
A @Entity
B @Bean
C @Document
D @Repository
2. 在Spring Data ElasticSearch中,实体类成员变量上方添加的注解为
A @Field
B @Bean
C @Document
D @Id
答案
1=>C 2=>A