资讯详情

springboot 2.3.12.release 整合elasticsearch

版本

springboot2.3.12.releaseelasticsearch7.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); 

SpringDataElasticsearch4.0中的新功能

使用Spring5.2。

升级到Elasticsearch7.6.2。

弃用TransportClient使用。

实现大多数可用于索引映射的映射类型。

移除JacksonObjectMapper,现在使用MappingElasticsearchConverter

清理*Operations接口中的API,对方法进行分组和重命名,使其与ElasticsearchAPI匹配,弃用旧方法,与其他方法SpringData模块保持一致。

引入SearchHit类表示文档和文档的相关结果元数据(即sortValues)。

引入SearchHits类表示整个搜索结果和完整搜索结果的元数据(即max_score)。

引入SearchPage类表示包含SearchHits实例分页结果。

引入GeoDistanceOrder能按地理距离排序的类

实施审计支持

实现生命周期实体回调

标签: qyb102a压力变送器qyb102智能数显压力变送器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台