我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

ElasticSearch----使用JavaAPI查询ES-创新互联

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

公司主营业务:成都做网站、网站制作、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联公司推出和县免费做网站回馈大家。

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

引入依赖
org.springframework.bootspring-boot-starter-parent2.3.12.RELEASEorg.springframework.dataspring-data-elasticsearch
实例化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");

    Iteratoriterator = 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");
        Iteratoriterator1 = terms.getBuckets().iterator();
        while (iterator1.hasNext()) {Terms.Bucket next1 = iterator1.next();
            System.out.println(next1.getKey());
            System.out.println(next1.getDocCount());
        }
        System.out.println("+++++++++++++++++++++");
    }
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


当前文章:ElasticSearch----使用JavaAPI查询ES-创新互联
标题路径:http://shouzuofang.com/article/digdoj.html

其他资讯