版本
springboot 2.3.12.release elasticsearch 7.6.2
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
配置
spring:
elasticsearch:
rest:
uris: 192.168.176.131:9200
mapping
package com.qi.demo1.entity;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
/**
* @author qyb
* @version 1.0
* @date 2022/7/12-16:59
*/
@Data
@Document(indexName = "my_es_test")
public class MyEs {
@Id
private Long id;
@Field(type = FieldType.Text)
private String username;
@Field(type = FieldType.Integer)
private Integer age;
@Field(type = FieldType.Boolean)
private Boolean sex;
@Field(type = FieldType.Date,format = DateFormat.date)
private LocalDate birth;
@Field(type = FieldType.Nested)
private List<Son> children;
@Data
public static class Son {
private String username;
private Integer age;
}
}
使用
使用ElasticsearchRestTemplate ,elasticesearchTemplate已经过期
@Autowired
ElasticsearchRestTemplate restTemplate;
@Test
public void testEs(){
MyEs myEs = new MyEs();
myEs.setUsername("李四");
myEs.setSex(false);
myEs.setAge(18);
myEs.setBirth(LocalDate.now());
myEs.setId(1L);
ArrayList<MyEs.Son> sons = new ArrayList<>();
MyEs.Son son = new MyEs.Son();
son.setAge(1);
son.setUsername("礼物");
sons.add(son);
MyEs.Son son1 = new MyEs.Son();
son1.setAge(2);
son1.setUsername("理想");
sons.add(son1);
myEs.setChildren(sons);
restTemplate.save(myEs);
}
查询 聚合、分页、排序
@Test
void test2() {
TermsAggregationBuilder aggId = AggregationBuilders.terms("aggId").field("id");
aggId.subAggregation(AggregationBuilders.terms("aggAge").field("age"));
NativeSearchQuery query = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.matchQuery("username", "李四"))
.addAggregation(aggId)
.withSort(SortBuilders.fieldSort("id").order(SortOrder.DESC))
.withHighlightBuilder(new HighlightBuilder().field("username")
.preTags("<span style='color:red;'>").postTags("</span>"))
.withPageable(PageRequest.of(1,5))
.build();
SearchHits<MyEs> search = restTemplate.search(query, MyEs.class);
List<MyEs> myess = search.getSearchHits().stream().map(item -> {
MyEs myEs = item.getContent();
myEs.setUsername(item.getHighlightField("username").get(0));
return myEs;
}).collect(Collectors.toList());
System.out.println(myess);
Spring Data Elasticsearch 4.0 中的新功能
使用 Spring 5.2。
升级到 Elasticsearch 7.6.2。
弃用TransportClient使用。
实现大多数可用于索引映射的映射类型。
移除 Jackson ObjectMapper,现在使用MappingElasticsearchConverter
清理*Operations接口中的 API,对方法进行分组和重命名,使其与 Elasticsearch API 匹配,弃用旧方法,与其他 Spring Data 模块保持一致。
引入SearchHit类来表示找到的文档以及该文档的相关结果元数据(即sortValues)。
引入SearchHits类来表示整个搜索结果以及完整搜索结果的元数据(即max_score)。
引入SearchPage类来表示包含SearchHits实例的分页结果。
引入GeoDistanceOrder能够按地理距离进行排序的类
审计支持的实施
生命周期实体回调的实现