ElasticsearchRepository这个接口,只要继承了这个类就可以实现基本的增删改查
打开这个类我们发现:
@NoRepositoryBean public interface ElasticsearchRepository<T, ID extends Serializable> extends ElasticsearchCrudRepository<T, ID> { <S extends T> S index(S var1); Iterable<T> search(QueryBuilder var1); Page<T> search(QueryBuilder var1, Pageable var2); Page<T> search(SearchQuery var1); Page<T> searchSimilar(T var1, String[] var2, Pageable var3); void refresh(); Class<T> getEntityClass(); } 复制代码
关键字
关键字 | 使用示例 | 等同于的ES查询 |
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(Collectionnames) | {“bool” : {“must” : {“bool” : {“should” : [ {“field” : {“name” : “?”}}, {“field” : {“name” : “?”}} ]}}}} |
NotIn | findByNameNotIn(Collectionnames) | {“bool” : {“must_not” : {“bool” : {“should” : {“field” : {“name” : “?”}}}}}} |
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}}}} |
再来一个分页的例子
QuestionElasticsearchRepository
package com.gf.repository; import com.gf.entity.FQuestionElasticssearch; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.annotations.Query; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; public interface QuestionElasticsearchRepository extends ElasticsearchRepository<FQuestionElasticssearch, Long> { Page<FQuestionElasticssearch> findByCatory(String catory, Pageable pageable); @Query("{ \"bool\":{ \"must\":[ { \"multi_match\": { \"query\": \"?0\", \"type\": \"most_fields\", \"fields\": [ \"title\", \"content\" ] } }, { \"match\": { \"catory\": \"?1\" } } ] } } ") Page<FQuestionElasticssearch> searchByKeyWordsAndCatory(String keyword, String catory, Pageable pageable); } 复制代码
实体 FQuestionElasticssearch
package com.gf.entity; import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; @Document(indexName = "pomit", type = "issue", createIndex = false) public class FQuestionElasticssearch { @Id private Long id; private String title; private String catory; private String content; public FQuestionElasticssearch() { } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public void setCatory(String catory) { this.catory = catory; } public String getCatory() { return catory; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } } 复制代码
Service
package com.gf.service; import com.gf.entity.FQuestionElasticssearch; import com.gf.repository.QuestionElasticsearchRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; @Service public class QuestionElasticsearchService { @Autowired QuestionElasticsearchRepository questionElasticsearchRepository; public Page<FQuestionElasticssearch> pageByOpenAndCatory(Integer page, Integer size, String catory, String keyWord) { Pageable pageable = PageRequest.of(page, size); if (StringUtils.isEmpty(keyWord)) { return questionElasticsearchRepository.findByCatory(catory, pageable); } else { return questionElasticsearchRepository.searchByKeyWordsAndCatory(keyWord, catory, pageable); } } } 复制代码
Controller
package com.gf.controller; import com.gf.entity.FQuestionElasticssearch; import com.gf.service.QuestionElasticsearchService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("/elsearch") public class ElasticsearchRest { @Autowired QuestionElasticsearchService questionElasticsearchService; @RequestMapping(value = "/test", method = { RequestMethod.GET }) public List<FQuestionElasticssearch> test(@RequestParam(value = "value", required = false) String value) { return questionElasticsearchService.pageByOpenAndCatory(0, 10, "Spring专题", value).getContent(); } } 复制代码
结果
这个里面我具体来说说
@Query("{ \"bool\":{ \"must\":[ { \"multi_match\": { \"query\": \"?0\", \"type\": \"most_fields\", \"fields\": [ \"title\", \"content\" ] } }, { \"match\": { \"catory\": \"?1\" } } ] } } ") Page<FQuestionElasticssearch> searchByKeyWordsAndCatory(String keyword, String catory, Pageable pageable); 复制代码
这个的意思吧,其实这个和所有的springData JPA是一样的,它是为了拼成这样的条件。里面的分页有是有了的
结尾
今天就稍微讲了下这个springDataElasticsearch的增删改查,反正如果是初学的话,就先做到这,要用的时候再去深入吧