SpringBoot整合Elasticsearch:检索数据
2021-01-23 本文已影响0人
JBryan
Elasticsearch检索数据,请参考:https://www.jianshu.com/p/6b58dcac4fc8。
测试数据来自:https://github.com/elastic/elasticsearch/blob/master/docs/src/test/resources/accounts.json,在Kibana中执行:POST bank/account/_bulk。
1、Elasticsearch检索示例
搜索address中包含mill的所有人的年龄分布以及平均年龄。
GET bank/_search
{
"query": {
"match": {
"address": {
"query": "mill"
}
}
},
"aggregations": {
"ageTerm": {
"terms": {
"field": "age",
"size": 10
}
},
"ageAvg": {
"avg": {
"field": "age"
}
}
}
}
执行结果:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 5.4032025,
"hits" : [
{
"_index" : "bank",
"_type" : "account",
"_id" : "970",
"_score" : 5.4032025,
"_source" : {
"account_number" : 970,
"balance" : 19648,
"firstname" : "Forbes",
"lastname" : "Wallace",
"age" : 28,
"gender" : "M",
"address" : "990 Mill Road",
"employer" : "Pheast",
"email" : "forbeswallace@pheast.com",
"city" : "Lopezo",
"state" : "AK"
}
},
{
"_index" : "bank",
"_type" : "account",
"_id" : "136",
"_score" : 5.4032025,
"_source" : {
"account_number" : 136,
"balance" : 45801,
"firstname" : "Winnie",
"lastname" : "Holland",
"age" : 38,
"gender" : "M",
"address" : "198 Mill Lane",
"employer" : "Neteria",
"email" : "winnieholland@neteria.com",
"city" : "Urie",
"state" : "IL"
}
},
{
"_index" : "bank",
"_type" : "account",
"_id" : "345",
"_score" : 5.4032025,
"_source" : {
"account_number" : 345,
"balance" : 9812,
"firstname" : "Parker",
"lastname" : "Hines",
"age" : 38,
"gender" : "M",
"address" : "715 Mill Avenue",
"employer" : "Baluba",
"email" : "parkerhines@baluba.com",
"city" : "Blackgum",
"state" : "KY"
}
},
{
"_index" : "bank",
"_type" : "account",
"_id" : "472",
"_score" : 5.4032025,
"_source" : {
"account_number" : 472,
"balance" : 25571,
"firstname" : "Lee",
"lastname" : "Long",
"age" : 32,
"gender" : "F",
"address" : "288 Mill Street",
"employer" : "Comverges",
"email" : "leelong@comverges.com",
"city" : "Movico",
"state" : "MT"
}
}
]
},
"aggregations" : {
"ageAvg" : {
"value" : 34.0
},
"ageTerm" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 38,
"doc_count" : 2
},
{
"key" : 28,
"doc_count" : 1
},
{
"key" : 32,
"doc_count" : 1
}
]
}
}
}
2、SpringBoot整合Elasticsearch检索
SpringBoot整合Elasticsearch请参考:https://www.jianshu.com/p/6a99d6d0ec66,本文以此为基础继续讨论。
@Test
void searchData() throws IOException {
//1、创建检索请求
SearchRequest searchRequest = new SearchRequest();
//2、指定索引
searchRequest.indices("bank");
//3、创建检索条件
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//3.1、“address中包含mill”
sourceBuilder.query(QueryBuilders.matchQuery("address","mill"));
//3.2、“年龄分布”,根据年龄聚合
TermsAggregationBuilder ageAgg = AggregationBuilders.terms("ageTerm").field("age").size(10);
sourceBuilder.aggregation(ageAgg);
//3.3、计算“平均年龄”
AvgAggregationBuilder ageAvg = AggregationBuilders.avg("ageAvg").field("age");
sourceBuilder.aggregation(ageAvg);
System.out.println("检索条件:"+sourceBuilder.toString());
searchRequest.source(sourceBuilder);
//4、执行检索
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(searchResponse);
//4.1、获取所有查到的数据,这个hits是最外面的hits,里面还有很多小hits
SearchHits hits = searchResponse.getHits();
//4.2、获取到各个数据
SearchHit[] data = hits.getHits();
for (SearchHit datum : data) {
/** {
* "_index" : "bank",
* "_type" : "account",
* "_id" : "970",
* "_score" : 5.4032025,
* "_source" : {
* "account_number" : 970,
* "balance" : 19648,
* ...
* }
* }
*/
String id = datum.getId();
System.out.println(id);
String sourceAsString = datum.getSourceAsString();
Object account = JSON.parseObject(sourceAsString, Account.class);
System.out.println("Account: "+account);
}
//4.3、获取检索到的聚合数据
/**
* "aggregations" : {
* "ageAvg" : {
* ...
* },
* "ageTerm" : {
* ...
* }
* }
*/
Aggregations aggregations = searchResponse.getAggregations();
/**
* "ageTerm" : {
* "doc_count_error_upper_bound" : 0,
* "sum_other_doc_count" : 0,
* "buckets" : [
* {
* "key" : 38,
* "doc_count" : 2
* },
* ...
* ]
* }
*/
Terms ageTerm = aggregations.get("ageTerm");
/**
* "buckets": [{
* "key": 38,
* "doc_count": 2
* }, {
* "key": 28,
* "doc_count": 1
* }, {
* "key": 32,
* "doc_count": 1
* }]
*/
for (Terms.Bucket bucket : ageTerm.getBuckets()) {
System.out.println("年龄: "+ bucket.getKeyAsString()+",人数:"+bucket.getDocCount());
}
/**
* "ageAvg": {
* "value": 34.0
* }
*/
Avg avg = aggregations.get("ageAvg");
System.out.println("平均年龄:"+avg.getValue());
}