ElasticSearch----使用Java API查询ES

ElasticSearch版本:7.6.2
Springboot版本:2.3.12.RELEASE
springboot、es版本对应关系
Elasticsearch Clients列表
Java High Level REST Client参考文档

目录

  • 引入依赖
  • 实例化RestHighLevelClient
  • 查询
    • 多条件分组查询并对结果排序

引入依赖

<parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>2.3.12.RELEASEversion>
    <relativePath/> 
parent>



<dependency>
    <groupId>org.springframework.datagroupId>
    <artifactId>spring-data-elasticsearchartifactId>
dependency>

实例化RestHighLevelClient

@Configuration
public class ElasticSearchConfig {
    @Bean
    public RestHighLevelClient restHighLevelClient() {
        return new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("127.0.0.1", 9200, "http")));
    }
}

查询

多条件分组查询并对结果排序

@Test
public void testSearchGroupby() throws Exception{

    SearchRequest searchRequest = new SearchRequest("trafficlight_error_log");

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

    BoolQueryBuilder boolQueryBuilder =  QueryBuilders.boolQuery();
    boolQueryBuilder.must(QueryBuilders.rangeQuery("errorTime").gte("1673403774000").lte("1673414574000"));

    searchSourceBuilder.query(boolQueryBuilder);

    // 分组排序,size:分组之后前N条数据,sort:.key:按key排序;.count:按count结果排序
    TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("aIntersectionId_count").field(
            "aIntersectionId").size(1000).order(BucketOrder.key(true));
    TermsAggregationBuilder termsAggregationBuilder2 = AggregationBuilders.terms("errorMsg_count").field(
            "errorMsg.keyword").order(BucketOrder.count(true));
            
	// 多条件分组查询
    termsAggregationBuilder.subAggregation(termsAggregationBuilder2);

    searchSourceBuilder.aggregation(termsAggregationBuilder);

    searchRequest.source(searchSourceBuilder);

    SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

//	System.out.println(searchResponse.getAggregations().asMap().get("aIntersectionId_count").getClass().getSimpleName());

    ParsedLongTerms aIntersectionId_count = (ParsedLongTerms) 			searchResponse.getAggregations().asMap().get("aIntersectionId_count");

    Iterator<? extends Terms.Bucket> iterator = aIntersectionId_count.getBuckets().iterator();

	// 遍历
    while (iterator.hasNext()) {
        Terms.Bucket next = iterator.next();
        System.out.println(next.getKey());
        System.out.println(next.getDocCount());
// 		System.out.println(next.getAggregations().asMap().get("errorMsg_count").getClass().getSimpleName());
        ParsedStringTerms terms = (ParsedStringTerms) next.getAggregations().asMap().get("errorMsg_count");
        Iterator<? extends Terms.Bucket> iterator1 = terms.getBuckets().iterator();
        while (iterator1.hasNext()) {
            Terms.Bucket next1 = iterator1.next();
            System.out.println(next1.getKey());
            System.out.println(next1.getDocCount());
        }
        System.out.println("+++++++++++++++++++++");
    }
}

你可能感兴趣的:(#,elasticsearch,java,elasticsearch,java)