文章目录
SpringData对ES客户端的封装:ElasticsearchRestTemplate
SpringData对CRUD的封装:ElasticsearchRepository
原生ES客户端:RestHighLevelClient
查询
java
package com.xd.cubemall.sdes;
import com.xd.cubemall.search.CubemallSearchApplication;
import com.xd.cubemall.search.model.Blog;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = CubemallSearchApplication.class)
public class QueryTest {
@Autowired
private ElasticsearchRestTemplate template;
@Test
public void testQuery1() {
NativeSearchQuery query = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.matchAllQuery())
.build();
SearchHits<Blog> searchHits = template.search(query, Blog.class);
//取总记录数
long totalHits = searchHits.getTotalHits();
System.out.println("总记录数:" + totalHits);
//取文档列表
List<SearchHit<Blog>> hits = searchHits.getSearchHits();
hits.stream().forEach(h-> System.out.println(h));
}
@Test
public void testQuery2() throws Exception {
NativeSearchQuery query = new NativeSearchQueryBuilder()
//查询设置
.withQuery(QueryBuilders.multiMatchQuery("看电影","title","content"))
//过滤条件,可以设置多个,支持查询条件
.withFilter(QueryBuilders.boolQuery()
.should(QueryBuilders.termQuery("title", "电影"))
.should(QueryBuilders.termQuery("content","祝福"))
)
// .withFilter(QueryBuilders.termQuery("mobile", "13000000000"))
//分页设置
.withPageable(PageRequest.of(0, 10))
//高亮设置
.withHighlightBuilder(new HighlightBuilder()
//高亮显示的字段
.field("title")
.field("content")
//高亮显示的前缀
.preTags("<em>")
//高亮显示的后缀
.postTags("</em>")
).build();
//使用template对象执行查询
SearchHits<Blog> searchHits = template.search(query, Blog.class);
//取返回结果
//总记录数
long totalHits = searchHits.getTotalHits();
System.out.println("总记录数:" + totalHits);
//取文档列表
List<SearchHit<Blog>> hits = searchHits.getSearchHits();
hits.stream().forEach(hit->{
//取文档对象
Blog blog = hit.getContent();
//System.out.println(blog);
//取高亮结果
Map<String,List<String>> highlightFields = hit.getHighlightFields();
//System.out.println(highlightFields);
String title = highlightFields.get("title").get(0);
String content = highlightFields.get("content").get(0);
blog.setTitle(title);
blog.setContent(content);
System.out.println(blog);
});
}
}
聚合查询
api使用方法
搜索时,请求体中包含聚合条件
在aggs属性中设置聚合条件:
value_count
max
min
avg
json
GET blog_1/_search
{
"query": {
"match_all": {}
},
"aggs": {
"mobile_count": {
"value_count": {
"field": "mobile"
}
},
"mobile_max":{
"max": {
"field": "mobile"
}
}
}
}
分桶(分组)聚合
json
GET blog_1/_search
{
"query": {
"match_all": {}
},
"aggs": {
"mobile_count": {
"value_count": {
"field": "mobile"
}
},
"mobile_group":{
"terms": {
"field": "mobile",
"size": 10
}
}
}
}
RestHighLevelClient
java
@Autowired
private RestHighLevelClient restHighLevelClient;
@Test
public void testAggs() throws IOException {
SearchRequest request = new SearchRequest()
.indices("blog_1")
.source(new SearchSourceBuilder()
.query(QueryBuilders.matchAllQuery())
.aggregation(new ValueCountAggregationBuilder("doc_count").field("mobile"))
.aggregation(new TermsAggregationBuilder("group_count").field("mobile").size(10))
);
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
System.out.println(response);
}
ElasticsearchRestTemplat
java
@Test
public void testQuery2() throws Exception {
NativeSearchQuery query = new NativeSearchQueryBuilder()
//查询设置
.withQuery(QueryBuilders.multiMatchQuery("看电影","title","content"))
//过滤条件,可以设置多个,支持查询条件
.withFilter(QueryBuilders.boolQuery()
.should(QueryBuilders.termQuery("title", "电影"))
.should(QueryBuilders.termQuery("content","祝福"))
)
// .withFilter(QueryBuilders.termQuery("mobile", "13000000000"))
//分页设置
.withPageable(PageRequest.of(0, 10))
//高亮设置
.withHighlightBuilder(new HighlightBuilder()
//高亮显示的字段
.field("title")
.field("content")
//高亮显示的前缀
.preTags("<em>")
//高亮显示的后缀
.postTags("</em>")
)
.addAggregation(new TermsAggregationBuilder("mobile_group").field("mobile"))
.build();
//使用template对象执行查询
SearchHits<Blog> searchHits = template.search(query, Blog.class);
//取返回结果
//总记录数
long totalHits = searchHits.getTotalHits();
System.out.println("总记录数:" + totalHits);
//取文档列表
List<SearchHit<Blog>> hits = searchHits.getSearchHits();
hits.stream().forEach(hit->{
//取文档对象
Blog blog = hit.getContent();
//System.out.println(blog);
//取高亮结果
Map<String,List<String>> highlightFields = hit.getHighlightFields();
//System.out.println(highlightFields);
String title = highlightFields.get("title").get(0);
String content = highlightFields.get("content").get(0);
blog.setTitle(title);
blog.setContent(content);
System.out.println(blog);
});
Aggregations aggregations = searchHits.getAggregations();
ParsedStringTerms aggregation = aggregations.get("mobile_group");
List<? extends Terms.Bucket> buckets = aggregation.getBuckets();
buckets.forEach(e-> {
System.out.println(e.getKeyAsString());
System.out.println(e.getDocCount());
});
}