By the 25th day
3, use,
You implemented index creation in the previous section, so this section gives you a try at retrieving data. In kibana, the following DSL statement is used to search for the age distribution and average salary of all the people whose address contains Mill:
GET/bank / _search {" query ": {/ / query" match ": {" address" : "mill"}}, "aggs" : {/ / aggregation "ageAgg" : {/ / age distribution "terms" : {" field ", "age", "size" : 10 / / aggregation results in only take 10}}, "balanceAvg" : {/ / average wages "avg" : {" field ":" balance "}}}}Copy the code
The results are shown below:
How do you do that in a SpringBoot integration environment? Let’s use the code to do this.
1), the test class PafcmallSearchApplicationTests. Add test method in Java searchData () :
/** * retrieve data **@throws IOException
*/
@Test
void searchData(a) throws IOException {
// create a search request
SearchRequest searchRequest = new SearchRequest();
// Specify the index
searchRequest.indices("bank");
// Specify the DSL, search criteria
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 1.1), construct the search condition
// sourceBuilder.query(QueryBuilders.termQuery("user", "kimchy"));
// sourceBuilder.from(0);
// sourceBuilder.size(5);
// sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
searchSourceBuilder.query(QueryBuilders.matchQuery("address"."mill"));
// Aggregate data
// 1.2), aggregated according to age distribution
TermsAggregationBuilder ageAgg = AggregationBuilders.terms("ageAgg").field("age").size(10);
searchSourceBuilder.aggregation(ageAgg);
// 1.3), calculate the flat salary
AvgAggregationBuilder balanceAvg = AggregationBuilders.avg("balanceAvg").field("balance");
searchSourceBuilder.aggregation(balanceAvg);
System.out.println("Search criteria:"+searchSourceBuilder.toString());
searchRequest.source(searchSourceBuilder);
// 2
SearchResponse searchResponse = client.search(searchRequest, PafcmallElasticsearchConfig.COMMON_OPTIONS);
// 3. Analysis result searchResponse
System.out.println(searchResponse.toString());
//3.1), obtain all the found data
SearchHits hits = searchResponse.getHits(); // Get the outermost hits
SearchHit[] searchHits = hits.getHits(); // Enclose the hits array
for (SearchHit hit : searchHits) {
/ * * * "_index" : "bank," * "_type" : "account," * "_id" : "970", * "_score" : 5.4032025. * "_source" : {* /
// hit.getIndex(); hit.getType()''
String str = hit.getSourceAsString();
Account account = JSON.parseObject(str, Account.class);
System.out.println(account.toString());
}
//3.1), obtain the analysis data retrieved this time
Aggregations aggregations = searchResponse.getAggregations();
// The aggregate data can be traversed
// for (Aggregation aggregation : aggregations.asList()) {
// system.out.println (" current aggregation: "+ aggregation.getname ());
// aggregation.getXxx
/ /}
// The following methods can also be used
Terms ageAgg1 = aggregations.get("ageAgg");
for (Terms.Bucket bucket : ageAgg1.getBuckets()) {
String keyAsString = bucket.getKeyAsString();
System.out.println("Age:"+keyAsString+"= = >"+bucket.getDocCount()+"A");
}
Avg balanceAvg1 = aggregations.get("balanceAvg");
System.out.println("Average salary:"+balanceAvg);
}
Copy the code
2) Add test class to collect results:
/** * Test account class */
@ToString
@Data
static class Account {
private int account_number;
private int balance;
private String firstname;
private String lastname;
private int age;
private String gender;
private String address;
private String employer;
private String email;
private String city;
private String state;
}
Copy the code
3) Perform the test method and the results are as follows:
boost
boost
The official documentation
Let’s look at the query result again, using JSON tool formatting, we can see that there are 4 returned data that meet the conditions, which is consistent with the previous kibana check:
Above, that is all the content of SpringBoot integration of ES, more advanced use can refer to the official documentation of ES to try.
For more information on retrieval, see Java-rest-high-search
Refer to documentation – Java-REST
conclusion
Of course, ES is widely used in actual production:
For example, using the ELK component for log collection or full text retrieval:
Or it can be used to collect abnormal information, make a visual interface to provide analysis, etc. :
Reference:
Elasticsearch Reference
elastic
Full text Search Engine Elasticsearch tutorial