ElasticSearch集成SpringData史上最全查询教程
1、简单介绍
springboot 使用springdata操作es,ElasticsearchRepository使用QueryBuilder构造查询条件
2、集成es
//maven集成
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
//实现ElasticsearchRepository
public interface NewsInfoRepository extends ElasticsearchRepository<NewInfoItem,Long> {
}
**3、 查询所有数据matchAllQuery **
matchAllQuery 查询所用:相当于sql中的select * from 。当然,除了使用QueryBuilder,ElasticsearchRepository的findAll方法也是查询所有。
public RestResult queryAll() {
QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
List<NewInfoItem> resultList = new ArrayList<>();
Iterable<NewInfoItem> search = this.newsInfoRepository.search(queryBuilder);
search.forEach(resultList::add);
return interReturnResult(GlobalErrorCode.SUCCESS, resultList);
}
4、matchQuery字段匹配查询
matchQuery可以简单理解为mysql中的like,因为在elasticsearch中使用matchQuery查询时,他会对查询的field进行分词。当然我们进行查询的这个field的mapping必须是text类型,传给match查询的词条将被建立索引时相同的分析器处理
public RestResult matchQuery(String title) {
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("title", title);
//多字段进行匹配
//MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(title, "title", "name");
Iterable<NewInfoItem> search = this.newsInfoRepository.search(matchQueryBuilder);
List<NewInfoItem> resultList = new ArrayList<>();
search.forEach(resultList::add);
return interReturnResult(GlobalErrorCode.SUCCESS, resultList);
}
5、query_string查询
相比其他可用的查询,query_string查询支持全部的Apache Lucene查询语法针对多字段的query_string查询
public RestResult queryString(String field) {
QueryBuilder queryBuilder = QueryBuilders.queryStringQuery(field);
List<NewInfoItem> resultList = new ArrayList<>();
Iterable<NewInfoItem> search = this.newsInfoRepository.search(queryBuilder);
search.forEach(resultList::add);
return interReturnResult(GlobalErrorCode.SUCCESS, resultList);
}
6、通配符查询wildcardQuery
匹配多个字符,?匹配1个字符 避免 开始, 会检索大量内容造成效率缓慢
public RestResult wildcardQuery(String field) {
WildcardQueryBuilder wildcardQueryBuilder = QueryBuilders.wildcardQuery("title", "故事*");
Iterable<NewInfoItem> search = this.newsInfoRepository.search(wildcardQueryBuilder);
List<NewInfoItem> resultList = new ArrayList<>();
search.forEach(resultList::add);
return interReturnResult(GlobalErrorCode.SUCCESS, resultList);
}
7、 词条查询termQuery
词条查询是Elasticsearch中的一个简单查询。它仅匹配在给定字段中含有该词条的文档,而且是确切的、未经分析的词条相当于sql语句中的“=”,使用这个搜索一般是对索引中keyword的mapping进行等值搜索。term query 属于过滤器查询,可以处理数字(numbers)、布尔值(Booleans)、日期(dates)以及文本(text)。
public RestResult termQuery(String title) {
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", title);
Iterable<NewInfoItem> search = this.newsInfoRepository.search(termQueryBuilder);
List<NewInfoItem> resultList = new ArrayList<>();
search.forEach(resultList::add);
return interReturnResult(GlobalErrorCode.SUCCESS, resultList);
}
8、查询ID(标识符查询)idsQuery
public RestResult queryIdsQ(String id) {
//可以添加多个id
IdsQueryBuilder idsQueryBuilder = QueryBuilders.idsQuery().addIds(id);
Iterable<NewInfoItem> search = this.newsInfoRepository.search(idsQueryBuilder);
List<NewInfoItem> resultList = new ArrayList<>();
search.forEach(resultList::add);
return interReturnResult(GlobalErrorCode.SUCCESS, resultList);
}
9、相似度查询fuzzyQuery
fuzzy查询是模糊查询中的第三种类型,它基于编辑距离算法来匹配文档
public RestResult fuzzyQuery(String str) {
FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("title", str);
Iterable<NewInfoItem> search = this.newsInfoRepository.search(fuzzyQueryBuilder);
List<NewInfoItem> resultList = new ArrayList<>();
search.forEach(resultList::add);
return interReturnResult(GlobalErrorCode.SUCCESS, resultList);
}
10、范围查询rangeQuery
范围查询使我们能够找到在某一字段值在某个范围里的文档,字段可以是数值型,也可以是基于字符串的
public RestResult rangeQuery(String str) {
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").from(10).to(20);
//大于:select * from wtyy where age >1
QueryBuilder queryBuilder1 = QueryBuilders.rangeQuery("age").gt(str);
//大于等于:select * from wtyy where age >=1
QueryBuilder queryBuilder2 = QueryBuilders.rangeQuery("age").gte(str);
//小于:select * from wtyy where age <4
QueryBuilder queryBuilder3 = QueryBuilders.rangeQuery("age").lt(str);
//小于等于
QueryBuilder queryBuilder4 = QueryBuilders.rangeQuery("age").lte(str);
Iterable<NewInfoItem> search = this.newsInfoRepository.search(rangeQueryBuilder);
List<NewInfoItem> resultList = new ArrayList<>();
search.forEach(resultList::add);
return interReturnResult(GlobalErrorCode.SUCCESS, resultList);
}
11、 组合查询(复杂查询)
must(QueryBuilders) : AND
mustNot(QueryBuilders): NOT
should(QueryBuilders):OR
public RestResult multiQuery(String title, String stockCode) {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
.must(QueryBuilders.matchQuery("title", title))
.must(QueryBuilders.termQuery("stockCode", stockCode));
Iterable<NewInfoItem> search1 = this.newsInfoRepository.search(boolQueryBuilder);
List<NewInfoItem> resultList = new ArrayList<>();
search1.forEach(resultList::add);
return interReturnResult(GlobalErrorCode.SUCCESS, resultList);
}
12、复合查询+排序
public RestResult sortQuery(String title, String stockCode) {
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
nativeSearchQueryBuilder.withQuery(QueryBuilders.boolQuery()
.must(QueryBuilders.matchQuery("title", title))
.must(QueryBuilders.termQuery("stockCode", stockCode)))
.withSort(SortBuilders.fieldSort("id").order(SortOrder.DESC));
Page<NewInfoItem> search = this.newsInfoRepository.search(nativeSearchQueryBuilder.build());
List<NewInfoItem> resultList = new ArrayList<>();
search.forEach(resultList::add);
return interReturnResult(GlobalErrorCode.SUCCESS, resultList);
}
13、复合查询+排序+分页
public RestResult pageQuery(String title, String stockCode, Integer pageNum, Integer pageSize) {
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
nativeSearchQueryBuilder.withQuery(QueryBuilders.boolQuery()
.must(QueryBuilders.matchQuery("title", title))
.must(QueryBuilders.termQuery("stockCode", stockCode)))
.withSort(SortBuilders.fieldSort("id").order(SortOrder.DESC))
.withPageable(PageRequest.of(pageNum, pageSize));
Page<NewInfoItem> search = this.newsInfoRepository.search(nativeSearchQueryBuilder.build());
List<NewInfoItem> resultList = new ArrayList<>();
search.forEach(resultList::add);
return interReturnResult(GlobalErrorCode.SUCCESS, resultList);
}
14、复合查询+排序+分页 高亮显示
public RestResult pageQuery(String title, Integer pageNum, Integer pageSize) {
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
String preTag = "<font color='#dd4b39'>";//google的色值
String postTag = "</font>";
nativeSearchQueryBuilder.withQuery(QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery("title", title)))
.withSort(SortBuilders.fieldSort("id").order(SortOrder.DESC))
.withHighlightFields(new HighlightBuilder.Field("stockCode").preTags(preTag).postTags(postTag))
.withPageable(PageRequest.of(pageNum, pageSize));
Page<NewInfoItem> search = this.newsInfoRepository.search(nativeSearchQueryBuilder.build());
List<NewInfoItem> resultList = new ArrayList<>();
search.forEach(resultList::add);
return interReturnResult(GlobalErrorCode.SUCCESS, resultList);
}
关注我的微信公众号