一、实现ElasticsearchRepository接口:
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
/**
* @author WuSong
* @version 1.0
* @date 2022/10/12 16:44
* @description
*/
public interface EsRepository extends ElasticsearchRepository<Product4ES,String> {
}
二、定义方法
/**
* @author WuSong
* @version 1.0
* @date 2022/10/12 16:44
* @description
*/
public interface EsRepository extends ElasticsearchRepository<Product4ES,String> {
/**
* 自定义方法里By后面的字符串,必须存在泛型类Product4ES里,而且可以用and连接;
* 比如 findByProductName、findByProductNameAndProductSkuName
*
* List<Product4ES> findByP(String productName);//会报错
* 否则报如下错:
* Caused by: org.springframework.data.mapping.PropertyReferenceException:
* No property p found for type ArticleEsDto! Did you mean 'id'?
*
* List<Product4ES> findByProductName(String productName);// 才是对的 代理类会自动解析findByProductName,然后拼接查询es的条件进行查询
*/
List<Product4ES> findByProductName(String productName);
Page<Product4ES> findByProductName(String productName, Pageable pageable);
Page<Product4ES> findByProductNameOrProductSkuName(String productName, String productSkuName, Pageable pageable);
/**自定义方法:
* 根据价格区间查询
* Price: 是实体类属性名,这里是价格属性
* @param from 开始价格
* @param to 结束价格
* @return 符合条件的goods
*/
List<Product4ES> findByPriceBetween(Double from, Double to);
}
三、支持的一些语法对应表
Keyword | Sample | Elasticsearch Query String |
And | findByNameAndPrice |
{“bool” : {“must” : [ {“field” : {“name” : “?”}}, {“field” : {“price” : “?”}} ]}} |
Or | findByNameOrPrice |
{“bool” : {“should” : [ {“field” : {“name” : “?”}}, {“field” : {“price” : “?”}} ]}} |
Is | findByName | {“bool” : {“must” : {“field” : {“name” : “?”}}}} |
Not | findByNameNot | {“bool” : {“must_not” : {“field” : {“name” : “?”}}}} |
Between | findByPriceBetween |
{“bool” : {“must” : {“range” : {“price” : {“from” : ?,”to” : ?,”include_lower” : true,”include_upper” : true}}}}} |
LessThanEqual | findByPriceLessThan |
{“bool” : {“must” : {“range” : {“price” : {“from” : null,”to” : ?,”include_lower” : true,”include_upper” : true}}}}} |
GreaterThanEqual | findByPriceGreaterThan |
{“bool” : {“must” : {“range” : {“price” : {“from” : ?,”to” : null,”include_lower” : true,”include_upper” : true}}}}} |
Before | findByPriceBefore |
{“bool” : {“must” : {“range” : {“price” : {“from” : null,”to” : ?,”include_lower” : true,”include_upper” : true}}}}} |
After | findByPriceAfter |
{“bool” : {“must” : {“range” : {“price” : {“from” : ?,”to” : null,”include_lower” : true,”include_upper” : true}}}}} |
Like | findByNameLike |
{“bool” : {“must” : {“field” : {“name” : {“query” : “?*”,”analyze_wildcard” : true}}}}} |
StartingWith | findByNameStartingWith |
{“bool” : {“must” : {“field” : {“name” : {“query” : “?*”,”analyze_wildcard” : true}}}}} |
EndingWith | findByNameEndingWith |
{“bool” : {“must” : {“field” : {“name” : {“query” : “*?”,”analyze_wildcard” : true}}}}} |
Contains/Containing | findByNameContaining |
{“bool” : {“must” : {“field” : {“name” : {“query” : “**?**”,”analyze_wildcard” : true}}}}} |
In | findByNameIn(Collection<String>names) |
{“bool” : {“must” : {“bool” : {“should” : [ {“field” : {“name” : “?”}}, {“field” : {“name” : “?”}} ]}}}} |
NotIn | findByNameNotIn(Collection<String>names) |
{“bool” : {“must_not” : {“bool” : {“should” : {“field” : {“name” : “?”}}}}}} |
Near | findByStoreNear | Not Supported Yet ! |
TRUE | findByAvailableTrue |
{“bool” : {“must” : {“field” : {“available” : true}}}} |
FALSE | findByAvailableFalse |
{“bool” : {“must” : {“field” : {“available” : false}}}} |
OrderBy | findByAvailableTrueOrderByNameDesc |
{“sort” : [{ “name” : {“order” : “desc”} }], “bool” : {“must” : {“field” : {“available” : true}}}} |
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/105976.html