ElasticsearchRestTemplate API
分页搜索
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
@Test
public void test1() {
MatchAllQueryBuilder queryBuilder1 = QueryBuilders.matchAllQuery();
Pageable pageable = PageRequest.of(0, 5);
FieldSortBuilder balance = new FieldSortBuilder("balance").order(SortOrder.DESC);
NativeSearchQuery query = new NativeSearchQueryBuilder()
.withQuery(queryBuilder1)
.withPageable(pageable)
.withSort(balance)
.build();
SearchHits<EsAccount> searchHits = elasticsearchRestTemplate.search(query, EsAccount.class);
List<EsAccount> accounts = new ArrayList<>();
for (SearchHit<EsAccount> hit : searchHits) {
accounts.add(hit.getContent());
}
Page<EsAccount> page = new PageImpl<>(accounts,pageable,searchHits.getTotalHits());
System.out.println(page.getTotalPages());
System.out.println(page.getTotalElements());
}
条件搜索
@Test
public void test2() {
TermQueryBuilder builder = QueryBuilders.termQuery("account_number", 20);
TermQueryBuilder builder1 = QueryBuilders.termQuery("address", "mill");
TermQueryBuilder builder2 = QueryBuilders.termQuery("address", "mill lane");
NativeSearchQuery query = new NativeSearchQueryBuilder()
.withQuery(builder1)
.build();
SearchHits<EsAccount> searchHits = elasticsearchRestTemplate.search(query, EsAccount.class);
for (SearchHit<EsAccount> hit : searchHits) {
System.out.println(hit.getContent());
}
}
组合搜索
@Test
public void test3() {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.matchQuery("address", "mill"));
boolQueryBuilder.must(QueryBuilders.matchQuery("address", "lane"));
NativeSearchQuery query = new NativeSearchQueryBuilder()
.withQuery(boolQueryBuilder)
.build();
SearchHits<EsAccount> searchHits = elasticsearchRestTemplate.search(query, EsAccount.class);
for (SearchHit<EsAccount> hit : searchHits) {
System.out.println(hit.getContent());
}
}
过滤搜索
filter,过滤出 balance字段在 2w~3w的文档
@Test
public void test4() {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
RangeQueryBuilder balance = QueryBuilders.rangeQuery("balance").gte(20000).lte(30000);
boolQueryBuilder.filter(balance);
NativeSearchQuery query = new NativeSearchQueryBuilder()
.withQuery(boolQueryBuilder)
.build();
SearchHits<EsAccount> searchHits = elasticsearchRestTemplate.search(query, EsAccount.class);
for (SearchHit<EsAccount> hit : searchHits) {
System.out.println(hit.getContent());
}
}
聚合搜索
聚合搜索,aggs,类似于group by,对state字段进行聚合,
@Test
public void test5() {
NativeSearchQuery query = new NativeSearchQueryBuilder()
.addAggregation(AggregationBuilders.terms("count").field("state.keyword"))
.build();
SearchHits<EsAccount> searchHits = elasticsearchRestTemplate.search(query, EsAccount.class);
Aggregations aggregations = searchHits.getAggregations();
Terms terms = (Terms) aggregations.asMap().get("count");
for (Terms.Bucket bucket : terms.getBuckets()) {
String keyAsString = bucket.getKeyAsString();
long docCount = bucket.getDocCount();
System.out.println(keyAsString + " " + docCount);
}
}
嵌套聚合
统计出相同state的文档数量,再统计出balance的平均值,降序排序
@Test
public void test6() {
TermsAggregationBuilder stateAgg = AggregationBuilders.terms("count").field("state.keyword");
AvgAggregationBuilder balanceAgg = AggregationBuilders.avg("avg_balance").field("balance");
stateAgg.subAggregation(balanceAgg);
stateAgg.order(BucketOrder.aggregation("avg_balance", false));
NativeSearchQuery build = new NativeSearchQueryBuilder()
.addAggregation(stateAgg)
.build();
SearchHits<EsAccount> searchHits = elasticsearchRestTemplate.search(build, EsAccount.class);
Aggregations aggregations = searchHits.getAggregations();
Terms terms = (Terms) aggregations.asMap().get("count");
for (Terms.Bucket bucket : terms.getBuckets()) {
ParsedAvg avg = bucket.getAggregations().get("avg_balance");
System.out.println(bucket.getKeyAsString() + " " + bucket.getDocCount() + " " + avg.getValueAsString());
}
}
范围聚合
按字段的范围进行分段聚合,按age字段[20,30],[30,40],[40,50],之后按gender统计文档个数和balance的平均值
@Test
public void test7(){
RangeAggregationBuilder group_by_age =
AggregationBuilders.range("group_by_age").field("age")
.addRange(20, 30).addRange(30, 40).addRange(40, 50);
TermsAggregationBuilder count = AggregationBuilders.terms("count").field("gender.keyword");
AvgAggregationBuilder balanceAgg = AggregationBuilders.avg("avg_balance").field("balance");
group_by_age.subAggregation(count);
count.subAggregation(balanceAgg);
NativeSearchQuery query = new NativeSearchQueryBuilder()
.addAggregation(group_by_age)
.build();
SearchHits<EsAccount> searchHits = elasticsearchRestTemplate.search(query, EsAccount.class);
ParsedRange parsedRange = searchHits.getAggregations().get("group_by_age");
for (Range.Bucket bucket : parsedRange.getBuckets()) {
System.out.println(bucket.getKeyAsString()+" : "+bucket.getDocCount());
Terms group_by_gender = bucket.getAggregations().get("count");
for (Terms.Bucket genderBucket : group_by_gender.getBuckets()) {
System.out.println(genderBucket.getKeyAsString() +" : "+ genderBucket.getDocCount());
ParsedAvg balanceAvg = genderBucket.getAggregations().get("avg_balance");
System.out.println(balanceAvg.getValueAsString());
}
System.out.println("-----------\n");
}
}
|