企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 一、概述 虽然Spring Data Elasticsearch提供了两套接口机制,平台使用的是Elasticsearch Repositories接口; 这里,将以一个实际的例子,来展示如何基于Elasticsearch Repositories接口实现自己的业务需求; ## 二、模型定义 ``` package com.ray.mall.search.model; import org.springframework.data.annotation.Id; import org.springframework.data.domain.Persistable; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.FieldType; /** * @author fangwei * @version 3.0.9 * @date 2021年10月28日 */ @Document(indexName = "rayphonenumber") public class PhoneNumber implements Persistable<String> { @Id private String id; @Field(value = "area", type = FieldType.Text, analyzer = "ik_smart", searchAnalyzer = "ik_smart") private String area; @Field(value = "service-provider", type = FieldType.Text, analyzer = "ik_smart", searchAnalyzer = "ik_smart") private String serviceProvider; @Field(value = "number", type = FieldType.Text) private String number; @Field(value = "full-number", type = FieldType.Keyword) private String fullNumber; @Field(value = "sku-id", type = FieldType.Long) private long skuId; @Field(value = "spu-id", type = FieldType.Long) private long spuId; @Field(value = "price", type = FieldType.Long) private long price; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getArea() { return area; } public void setArea(String area) { this.area = area; } public String getServiceProvider() { return serviceProvider; } public void setServiceProvider(String serviceProvider) { this.serviceProvider = serviceProvider; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getFullNumber() { return fullNumber; } public void setFullNumber(String fullNumber) { this.fullNumber = fullNumber; } public long getSkuId() { return skuId; } public void setSkuId(long skuId) { this.skuId = skuId; } public long getSpuId() { return spuId; } public void setSpuId(long spuId) { this.spuId = spuId; } public long getPrice() { return price; } public void setPrice(long price) { this.price = price; } @Override public boolean isNew() { return id == null; } } ``` >[danger] 特别注意: > 1、ElasticSearch中的字段名称必须为小写; ## 三、仓库定义 主要用接口内置的方法实现实体的CUD; ``` public interface PhoneNumberRepository extends ElasticsearchRepository<PhoneNumber, String> { PhoneNumber findByNumber(String number); } ``` ## 四、业务操作 ``` @Autowired private PhoneNumberRepository phoneNumberRepository; //新增/更新索引文档 PhoneNumber ph = phoneNumberRepository.findByNumber(sku.getSkuVirtualData()); if (ph == null) { ph = new PhoneNumber(); ph.setNumber(sku.getSkuVirtualData()); } ph.setArea(sku.getSkuCategoryTitle()); ph.setServiceProvider("中国移动"); phoneNumberRepository.save(ph); ``` ## 五、搜索 ``` Pageable pageable = PagableBuilder.getPageable(curPage, pageSize, orderFieldName, orderDirection); Query query = new NativeSearchQueryBuilder()// .withQuery(QueryBuilders.matchQuery("area", area))// .withPageable(pageable).build(); SearchPage<PhoneNumber> searchPage = SearchHitSupport.searchPageFor(elasticsearchOperations.search(query, PhoneNumber.class), query.getPageable()); model.setPageobject(searchPage); ``` ``` QueryBuilders.boolQuery() .should(QueryBuilders.termQuery("area",area)) .should(QueryBuilders.matchQuery("number", number)) .mustNot(QueryBuilders.prefixQuery("service-provider", service-provider)) ``` >[danger] > 1、简单的来说,可以把搜索的工作,集中到如何构造一个合适的Query对象; > 2、QueryBuilders可以构造各种查询方式,termQuery精确查询、matchQuery分词精确查询;fuzzyQuery左右模糊查询、prefixQuery前缀模糊查询、wildcardQuery通配符模糊查询;rangeQuery范围查询;以及把以上集中查询组合在一起的boolQuery多关键字组合查询等等;