1.本文主要参考链接
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-search.html
2.代码
@Test
public void searchData() throws IOException {
// SearchRequest searchRequest = new SearchRequest();
// searchRequest.indices("users");
//创建执行请求并指定索引
SearchRequest searchRequest = new SearchRequest("bank");
//指定DSL检索条件
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
searchRequest.source(searchSourceBuilder);
//添加聚合
TermsAggregationBuilder countGender = AggregationBuilders.terms("countGender").field("gender.keyword").size(10);
AvgAggregationBuilder avgAge = AggregationBuilders.avg("avgAge").field("age");
searchSourceBuilder.aggregation(countGender);
searchSourceBuilder.aggregation(avgAge);
//执行检索
SearchResponse searchResponse = client.search(searchRequest, ElasticConfiguration.COMMON_OPTIONS);
//响应
System.out.println("searchResponse:"+searchResponse);
//分析结果
//检索状态
RestStatus status = searchResponse.status();
//花费时间
TimeValue took = searchResponse.getTook();
//是否未检索完就提前终止
Boolean terminatedEarly = searchResponse.isTerminatedEarly();
//是否超时
boolean timedOut = searchResponse.isTimedOut();
//命中记录对象
SearchHits hits = searchResponse.getHits();
//命中记录数
TotalHits totalHits = hits.getTotalHits();
long numHits = totalHits.value;
//最大相关性得分
TotalHits.Relation relation = totalHits.relation;
float maxScore = hits.getMaxScore();
//获取记录数
SearchHit[] searchHits = hits.getHits();
List<BankEntity> bankEntityList = new ArrayList<>();
for (SearchHit hit : searchHits) {
String sourceAsString = hit.getSourceAsString();
BankEntity bankEntity = JSON.parseObject(sourceAsString, BankEntity.class);
bankEntityList.add(bankEntity);
}
System.out.println("bankEntityList:"+bankEntityList);
}
上面的BankEntity是通过https://www.bejson.com/json2javapojo/new/ 根据source内的json串生成的
3.控制台打印结果
如下为searchResponse 可以从中看到花费时间,是否超时,分片信息,记录详情
searchResponse: {
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1000,
"relation": "eq"
},
"max_score": 1.0,
"hits": [{
"_index": "bank",
"_type": "account",
"_id": "1",
"_score": 1.0,
"_source": {
"account_number": 1,
"balance": 39225,
"firstname": "Amber",
"lastname": "Duke",
"age": 32,
"gender": "M",
"address": "880 Holmes Lane",
"employer": "Pyrami",
"email": "amberduke@pyrami.com",
"city": "Brogan",
"state": "IL"
}
}, {
"_index": "bank",
"_type": "account",
"_id": "6",
"_score": 1.0,
"_source": {
"account_number": 6,
"balance": 5686,
"firstname": "Hattie",
"lastname": "Bond",
"age": 36,
"gender": "M",
"address": "671 Bristol Street",
"employer": "Netagy",
"email": "hattiebond@netagy.com",
"city": "Dante",
"state": "TN"
}
}, {
"_index": "bank",
"_type": "account",
"_id": "13",
"_score": 1.0,
"_source": {
"account_number": 13,
"balance": 32838,
"firstname": "Nanette",
"lastname": "Bates",
"age": 28,
"gender": "F",
"address": "789 Madison Street",
"employer": "Quility",
"email": "nanettebates@quility.com",
"city": "Nogal",
"state": "VA"
}
}, {
"_index": "bank",
"_type": "account",
"_id": "18",
"_score": 1.0,
"_source": {
"account_number": 18,
"balance": 4180,
"firstname": "Dale",
"lastname": "Adams",
"age": 33,
"gender": "M",
"address": "467 Hutchinson Court",
"employer": "Boink",
"email": "daleadams@boink.com",
"city": "Orick",
"state": "MD"
}
}, {
"_index": "bank",
"_type": "account",
"_id": "20",
"_score": 1.0,
"_source": {
"account_number": 20,
"balance": 16418,
"firstname": "Elinor",
"lastname": "Ratliff",
"age": 36,
"gender": "M",
"address": "282 Kings Place",
"employer": "Scentric",
"email": "elinorratliff@scentric.com",
"city": "Ribera",
"state": "WA"
}
}, {
"_index": "bank",
"_type": "account",
"_id": "25",
"_score": 1.0,
"_source": {
"account_number": 25,
"balance": 40540,
"firstname": "Virginia",
"lastname": "Ayala",
"age": 39,
"gender": "F",
"address": "171 Putnam Avenue",
"employer": "Filodyne",
"email": "virginiaayala@filodyne.com",
"city": "Nicholson",
"state": "PA"
}
}, {
"_index": "bank",
"_type": "account",
"_id": "32",
"_score": 1.0,
"_source": {
"account_number": 32,
"balance": 48086,
"firstname": "Dillard",
"lastname": "Mcpherson",
"age": 34,
"gender": "F",
"address": "702 Quentin Street",
"employer": "Quailcom",
"email": "dillardmcpherson@quailcom.com",
"city": "Veguita",
"state": "IN"
}
}, {
"_index": "bank",
"_type": "account",
"_id": "37",
"_score": 1.0,
"_source": {
"account_number": 37,
"balance": 18612,
"firstname": "Mcgee",
"lastname": "Mooney",
"age": 39,
"gender": "M",
"address": "826 Fillmore Place",
"employer": "Reversus",
"email": "mcgeemooney@reversus.com",
"city": "Tooleville",
"state": "OK"
}
}, {
"_index": "bank",
"_type": "account",
"_id": "44",
"_score": 1.0,
"_source": {
"account_number": 44,
"balance": 34487,
"firstname": "Aurelia",
"lastname": "Harding",
"age": 37,
"gender": "M",
"address": "502 Baycliff Terrace",
"employer": "Orbalix",
"email": "aureliaharding@orbalix.com",
"city": "Yardville",
"state": "DE"
}
}, {
"_index": "bank",
"_type": "account",
"_id": "49",
"_score": 1.0,
"_source": {
"account_number": 49,
"balance": 29104,
"firstname": "Fulton",
"lastname": "Holt",
"age": 23,
"gender": "F",
"address": "451 Humboldt Street",
"employer": "Anocha",
"email": "fultonholt@anocha.com",
"city": "Sunriver",
"state": "RI"
}
}]
},
"aggregations": {
"avg#avgAge": {
"value": 30.171
},
"sterms#countGender": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [{
"key": "M",
"doc_count": 507
}, {
"key": "F",
"doc_count": 493
}]
}
}
}
可以看到 获取到的source内的hit对象们成功封装到对象数组内
bankEntityList:[BankEntity(account_number=1, balance=39225, firstname=Amber, lastname=Duke, age=32, gender=M, address=880 Holmes Lane, employer=Pyrami, email=amberduke@pyrami.com, city=Brogan, state=IL), BankEntity(account_number=6, balance=5686, firstname=Hattie, lastname=Bond, age=36, gender=M, address=671 Bristol Street, employer=Netagy, email=hattiebond@netagy.com, city=Dante, state=TN), BankEntity(account_number=13, balance=32838, firstname=Nanette, lastname=Bates, age=28, gender=F, address=789 Madison Street, employer=Quility, email=nanettebates@quility.com, city=Nogal, state=VA), BankEntity(account_number=18, balance=4180, firstname=Dale, lastname=Adams, age=33, gender=M, address=467 Hutchinson Court, employer=Boink, email=daleadams@boink.com, city=Orick, state=MD), BankEntity(account_number=20, balance=16418, firstname=Elinor, lastname=Ratliff, age=36, gender=M, address=282 Kings Place, employer=Scentric, email=elinorratliff@scentric.com, city=Ribera, state=WA), BankEntity(account_number=25, balance=40540, firstname=Virginia, lastname=Ayala, age=39, gender=F, address=171 Putnam Avenue, employer=Filodyne, email=virginiaayala@filodyne.com, city=Nicholson, state=PA), BankEntity(account_number=32, balance=48086, firstname=Dillard, lastname=Mcpherson, age=34, gender=F, address=702 Quentin Street, employer=Quailcom, email=dillardmcpherson@quailcom.com, city=Veguita, state=IN), BankEntity(account_number=37, balance=18612, firstname=Mcgee, lastname=Mooney, age=39, gender=M, address=826 Fillmore Place, employer=Reversus, email=mcgeemooney@reversus.com, city=Tooleville, state=OK), BankEntity(account_number=44, balance=34487, firstname=Aurelia, lastname=Harding, age=37, gender=M, address=502 Baycliff Terrace, employer=Orbalix, email=aureliaharding@orbalix.com, city=Yardville, state=DE), BankEntity(account_number=49, balance=29104, firstname=Fulton, lastname=Holt, age=23, gender=F, address=451 Humboldt Street, employer=Anocha, email=fultonholt@anocha.com, city=Sunriver, state=RI)]
Elasticsearch Java REST 客户端搜索与聚合实战

本文演示了使用Elasticsearch Java REST客户端进行搜索和聚合操作,包括匹配所有查询、计算平均年龄和性别分布。代码展示了如何获取搜索响应中的关键信息如花费时间、超时状态、命中记录以及将结果转换为自定义对象BankEntity的列表。
2957

被折叠的 条评论
为什么被折叠?



