赞
踩
- @NoRepositoryBean
- public interface ElasticsearchRepository<T, ID extends Serializable> extends ElasticsearchCrudRepository<T, ID> {
-
- <S extends T> S index(S entity);
-
- Iterable<T> search(QueryBuilder query);
- //search方法需要QueryBuilder参数和springdata的Pageable分页参数
- //其中QueryBuilder是一个接口,常用的实现类有MatchQueryBuilder、RangeQueryBuilder范围、FuzzyQueryBuilder模糊、WildcardQueryBuilder模糊。
- Page<T> search(QueryBuilder query, Pageable pageable);
- //SearchQuery是一个查询接口,NativeSearchQuery是该接口的一个实现
- Page<T> search(SearchQuery searchQuery);
-
- Page<T> searchSimilar(T entity, String[] fields, Pageable pageable);
-
- void refresh();
-
- Class<T> getEntityClass();
- }
【注意】
(1)ElasticsearchRepository里面search(QueryBuilder query)方法,其中QueryBuilder是一个接口,常用的实现类有MatchQueryBuilder、RangeQueryBuilder范围、FuzzyQueryBuilder模糊、WildcardQueryBuilder模糊。
(2)ElasticsearchRepository里面search(SearchQuery searchQuery)方法,主要是看QueryBuilder和SearchQuery两个参数;其中SearchQuery是一个接口,有一个实现类叫NativeSearchQuery。
自定义方法的前提是我们需要继承ElasticsearchRepository接口,利用强大的Spring Data来实现。比如:你的方法名叫做:findByTitle,那么它就知道你是根据title查询,然后自动帮你完成,无需写实现类。
【自定义方法命名约定】:
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}}}} |
例如,我们来按照价格区间查询,定义这样的一个方法findByPriceBetween:
- /**
- * 需要继承ElasticsearchRepository接口
- * 由于Item实体类中id为Long类型
- */
- public interface ItemRepository extends ElasticsearchRepository<Item,Long> {
- /**
- * 方法名必须遵守SpringData的规范
- * 价格区间查询
- */
- List<Item> findByPriceBetween(double price1, double price2);
- }
在【https://mp.csdn.net/postedit/84323886】基础上我们进行以下操作,实体类和索引创建都是安装上述博客内容;下面只对Repository操作详细说明。
- /**
- * 需要继承ElasticsearchRepository接口
- * 由于Item实体类中id为Long类型
- */
- public interface ItemRepository extends ElasticsearchRepository<Item,Long> {
- /**
- * 方法名必须遵守SpringData的规范
- * 价格区间查询
- */
- List<Item> findByPriceBetween(double price1, double price2);
- }
添加单条文档数据使用的save()方法。
- @Autowired
- private ItemRepository itemRepository;
- @Test
- public void insert(){
- Item item = new Item(1L, "小米手机7", " 手机","小米", 3499.00, "http://image.leyou.com/13123.jpg");
- itemRepository.save(item);
- }
插入多条文档数据使用的saveAll()方法。
- @Autowired
- private ItemRepository itemRepository;
- @Test
- public void indexList() {
- List<Item> list = new ArrayList<>();
- list.add(new Item(1L, "小米手机7", "手机", "小米", 3299.00, "http://image.leyou.com/13123.jpg"));
- list.add(new Item(2L, "坚果手机R1", "手机", "锤子", 3699.00, "http://image.leyou.com/13123.jpg"));
- list.add(new Item(3L, "华为META10", "手机", "华为", 4499.00, "http://image.leyou.com/13123.jpg"));
- list.add(new Item(4L, "小米Mix2S", "手机", "小米", 4299.00, "http://image.leyou.com/13123.jpg"));
- list.add(new Item(5L, "荣耀V10", "手机", "华为", 2799.00, "http://image.leyou.com/13123.jpg"));
- // 接收对象集合,实现批量新增
- itemRepository.saveAll(list);
- }
修改文档数据也是使用的save()方法。
- @Autowired
- private ItemRepository itemRepository;
- @Test
- public void update(){
- //Optional是java8新添加的类
- Optional<Item> itemOptional= itemRepository.findById(1L);
- Item item = itemOptional.get();
- item.setTitle("修改后的小米8");
- itemRepository.save(item);
- }
【代码例子】
- /**
- * 查询全部,并安装价格降序排序
- */
- @Test
- public void testFind(){
- // Sort是包下的org.springframework.data类,Sort.Direction是个枚举类
- Iterable<Item> items = this.itemRepository.findAll(Sort.by(Sort.Direction.DESC, "price"));
- items.forEach(item-> System.err.println(item));
- }
我们在DAO层的ItemRepository接口中自定义按照价格区间查询方法:
- public interface ItemRepository extends ElasticsearchRepository<Item,Long> {
- /**
- * 方法名必须遵守SpringData的规范
- * 价格区间查询
- */
- List<Item> findByPriceBetween(double price1, double price2);
- }
【代码】
- @Autowired
- private ItemRepository itemRepository;
- /**
- * 自定义方法findByPriceBetween查询范围
- */
- @Test
- public void queryByPriceBetween(){
- List<Item> items = itemRepository.findByPriceBetween(3500, 4500);
- items.stream().forEach(item -> {
- System.err.println("item:"+item);
- });
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。