网站建设 业务走下坡,高端轻奢品牌,在线制作app平台,做无障碍浏览网站目录
一、使用ElasticsearchRestTemplate类 1.引用Maven类库
2. 配置文件application.yml
3.创建实体类#xff08;用于JSON文档对象的转换#xff09;
二、使用ElasticsearchRepository 类
1.引用Maven类库
2. 配置文件application.yml
3. ElasticsearchRepository接…目录
一、使用ElasticsearchRestTemplate类 1.引用Maven类库
2. 配置文件application.yml
3.创建实体类用于JSON文档对象的转换
二、使用ElasticsearchRepository 类
1.引用Maven类库
2. 配置文件application.yml
3. ElasticsearchRepository接口的源码
4.CrudRepository 源码
5. 查询查找策略
5.1存储库方法可以定义为具有以下返回类型用于返回多个元素
5.2 使用Query注解
6.开发实例 操作ElasticSearch的数据有两种方式一种是 ElasticsearchRepository 接口另一种是ElasticsearchTemplate接口。SpringData对ES的封装ElasticsearchRestTemplate类可直接使用此类在ElasticsearchRestTemplate基础上进行性一定程度的封装使用起来更方便灵活拓展性更强。ElasticsearchRepository可以被继承操作ES是SpringBoot对ES的高度封装操作最为方便但牺牲了灵活性。 Spring boot 和Elasticsearch版本关系 一、使用ElasticsearchRestTemplate类 1.引用Maven类库
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-elasticsearch/artifactId
/dependency 2. 配置文件application.yml
spring:elasticsearch:rest:uris: http://192.168.10.202:9200connection-timeout: 1sread-timeout: 1musername: elasticpassword: elastic
注意如果es资源没有开启x-pack安全插件的话可以不加username和password因为默认是没有的。
3.创建实体类用于JSON文档对象的转换
import com.fasterxml.jackson.annotation.JsonFormat;
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.time.LocalDateTime;
/*** author Sinbad* description: 测试ES对象br /* date 2022/8/26 17:12*/
Document(indexName mysql-test)
Data
public class TestEsEntity {Id Long id;Field(type FieldType.Text, name addr)String addr;Field(type FieldType.Text, name name)String name;Field(type FieldType.Date, name birthday, pattern yyyy-MM-dd)LocalDate birthday;JsonFormat(pattern yyyy-MM-dd HH:mm:ss, timezone GMT8,locale zh_CN)Field(type FieldType.Date, name create_time, pattern yyyy-MM-dd HH:mm:ss,format DateFormat.custom )LocalDateTime createTime;
} Document注解表示对应一个索引名相关的文档 Data注解lombok的为类提供读写属性, 此外还提供了 equals()、hashCode()、toString() 方法 Id 注解表示文档的ID字段 Field注解文档字段的注解对于日期含时间的字段要写patten和format不然会无法更新文档对象 JsonFormat注解将文档转换成JSON返回给前端时用到 注意日期类型字段不要用java.util.Date类型要用java.time.LocalDate或java.time.LocalDateTime类型。 测试实例
import com.hkyctech.commons.base.entity.JsonResult;
import com.hkyctech.tu.core.vo.TestEsEntity ;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;Slf4j
Service
public class ElasticSearchServiceImpl {ResourceElasticsearchRestTemplate elasticsearchTemplate; //直接注入就可以用了/**** description 查询全部数据*/public Object testSearchAll(){Query queryelasticsearchTemplate.matchAllQuery();return elasticsearchTemplate.search(query, TestEsEntity .class);}/**** description 精确查询地址字段* param keyword 搜索关键字*/public Object testSearchAddr(String keyword) {NativeSearchQuery nativeSearchQuery new NativeSearchQueryBuilder()//查询条件.withQuery(QueryBuilders.queryStringQuery(keyword).defaultField(addr))//分页.withPageable(PageRequest.of(0, 10))//高亮字段显示.withHighlightFields(new HighlightBuilder.Field(keyword)).build();return elasticsearchTemplate.search(nativeSearchQuery, TestEsEntity .class);}/**** description 组合查询查询关键词不分词关系and*/public Object testComboSearchAnd(){BoolQueryBuilder esQueryQueryBuilders.boolQuery().must(QueryBuilders.termQuery(addr, 深圳)).must(QueryBuilders.termQuery(addr, 广东));NativeSearchQuery nativeSearchQuery new NativeSearchQueryBuilder()//查询条件.withQuery(esQuery)//分页.withPageable(PageRequest.of(0, 10)).build();return elasticsearchTemplate.search(nativeSearchQuery, TestEsEntity .class);}/**** description 组合查询查询关键词不分词关系or*/public Object testComboSearchOr(){BoolQueryBuilder esQueryQueryBuilders.boolQuery().should(QueryBuilders.termQuery(addr, 深圳)).should(QueryBuilders.termQuery(addr, 广东));NativeSearchQuery nativeSearchQuery new NativeSearchQueryBuilder()//查询条件.withQuery(esQuery)//分页.withPageable(PageRequest.of(0, 10)).build();return elasticsearchTemplate.search(nativeSearchQuery, TestEsEntity .class);}/**** description 索引或更新文档* param vo 文档对象*/public JsonResult testPutDocument(TestEsEntity vo){try {Object data elasticsearchTemplate.save(vo);return JsonResult.getSuccessResult(data,更新成功);}catch (Exception e){// 看http请求响应日志其实操作成功了但是会报解析出错可能是spring的bug这里拦截一下String messagee.getMessage();if(message.indexOf(responseHTTP/1.1 200 OK)0 || message.indexOf(responseHTTP/1.1 201 Created)0){return JsonResult.getSuccessResult(更新成功);}return JsonResult.getFailResult(e.getStackTrace(),e.getMessage());}}/**** description 删除文档* param id 文档ID*/public JsonResult deleteDocument(String id){try {elasticsearchTemplate.delete(id, TestEsEntity .class);return JsonResult.getSuccessResult(删除成功);}catch (Exception e){String messagee.getMessage();// 看http请求响应日志其实操作成功了但是会报解析出错可能是spring的bug这里拦截一下if(message.indexOf(responseHTTP/1.1 200 OK)0 ){return JsonResult.getSuccessResult(删除成功);}return JsonResult.getFailResult(e.getStackTrace(),e.getMessage());}}
}
二、使用ElasticsearchRepository 类 1.引用Maven类库 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-elasticsearch/artifactId/dependency 2. 配置文件application.yml
spring:elasticsearch:rest:uris: http://192.168.10.202:9200connection-timeout: 1sread-timeout: 1musername: elasticpassword: elastic 3. ElasticsearchRepository接口的源码
package org.springframework.data.elasticsearch.repository;import java.io.Serializable;import org.elasticsearch.index.query.QueryBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.data.repository.NoRepositoryBean;NoRepositoryBean
public interface ElasticsearchRepositoryT, ID extends Serializable extends ElasticsearchCrudRepositoryT, ID {S extends T S index(S entity);IterableT search(QueryBuilder query);PageT search(QueryBuilder query, Pageable pageable);PageT search(SearchQuery searchQuery);PageT searchSimilar(T entity, String[] fields, Pageable pageable);void refresh();ClassT getEntityClass();
} 4.CrudRepository 源码
package org.springframework.data.repository;import java.util.Optional;/*** Interface for generic CRUD operations on a repository for a specific type.** author Oliver Gierke* author Eberhard Wolff*/
NoRepositoryBean
public interface CrudRepositoryT, ID extends RepositoryT, ID {/*** Saves a given entity. Use the returned instance for further operations as the save operation might have changed the* entity instance completely.** param entity must not be {literal null}.* return the saved entity will never be {literal null}.*/S extends T S save(S entity);/*** Saves all given entities.** param entities must not be {literal null}.* return the saved entities will never be {literal null}.* throws IllegalArgumentException in case the given entity is {literal null}.*/S extends T IterableS saveAll(IterableS entities);/*** Retrieves an entity by its id.** param id must not be {literal null}.* return the entity with the given id or {literal Optional#empty()} if none found* throws IllegalArgumentException if {code id} is {literal null}.*/OptionalT findById(ID id);/*** Returns whether an entity with the given id exists.** param id must not be {literal null}.* return {literal true} if an entity with the given id exists, {literal false} otherwise.* throws IllegalArgumentException if {code id} is {literal null}.*/boolean existsById(ID id);/*** Returns all instances of the type.** return all entities*/IterableT findAll();/*** Returns all instances of the type with the given IDs.** param ids* return*/IterableT findAllById(IterableID ids);/*** Returns the number of entities available.** return the number of entities*/long count();/*** Deletes the entity with the given id.** param id must not be {literal null}.* throws IllegalArgumentException in case the given {code id} is {literal null}*/void deleteById(ID id);/*** Deletes a given entity.** param entity* throws IllegalArgumentException in case the given entity is {literal null}.*/void delete(T entity);/*** Deletes the given entities.** param entities* throws IllegalArgumentException in case the given {link Iterable} is {literal null}.*/void deleteAll(Iterable? extends T entities);/*** Deletes all entities managed by the repository.*/void deleteAll();
}
5. 查询查找策略
5.1存储库方法可以定义为具有以下返回类型用于返回多个元素 ListT StreamT SearchHitsT ListSearchHitT StreamSearchHitT SearchPageT
5.2 使用Query注解 使用query注释对方法声明query。
interface BookRepository extends ElasticsearchRepositoryBook, String {Query({\match\: {\name\: {\query\: \?0\}}})PageBook findByName(String name,Pageable pageable);
}
设置为注释参数的字符串必须是有效的 Elasticsearch JSON 查询。 它将作为查询元素的值发送到Easticsearch;例如如果使用参数 John 调用函数它将生成以下查询正文
{query: {match: {name: {query: John}}}
}对Query采用集合参数的方法进行注释
Query({\ids\: {\values\: ?0 }})
ListSampleEntity getByIds(CollectionString ids);
将进行ID查询以返回所有匹配的文档。因此调用List为[“id1”、“id2”、“id3”]的方法将生成查询主体
{query: {ids: {values: [id1, id2, id3]}}
}6.开发实例
public interface LogRepository extends ElasticsearchRepositoryLog, String {/*** 定义一个方法查询根据title查询es** 原因 ElasticsearchRepository会分析方法名,参数对应es中的field这就是灵活之处* param title*/ListLog findBySummary(String summary);ListLog findByTitle(String title);/*** 定义一个方法查询 根据titlecontent查询es*/ListLog findByTitleAndContent(String title, String content);} PostMapping(save)public void save(Validated RequestBody Log req){Log dto new Log();dto.setTitle(req.getTitle());dto.setSummary(req.getSummary());dto.setContent(req.getContent());dto.setCreateTime(new Date());dto.setId(req.getId()); LogRepository.save(dto);return ;} PostMapping(testTitle)public void testSearchTitle(Validated RequestBody Log req){ListLog searchResult logRepository.findByTitle(req.getMobileType());IteratorLog iterator searchResult.iterator();while(iterator.hasNext()){System.out.println(iterator.next());}System.out.println(sa);return;}
官网Spring Data Elasticsearch - Reference Documentation