首页 > 其他分享 >42-Springboot整合HignLevelClient----构建复杂检索

42-Springboot整合HignLevelClient----构建复杂检索

时间:2023-01-19 23:12:03浏览次数:29  
标签:检索 bankEntity Springboot 42 sourceBuilder private HignLevelClient balanceAvg Str

@Test
	void searchTest() throws IOException {
		SearchRequest searchRequest = new SearchRequest();
		//1、指定索引
		searchRequest.indices("bank");

		//2.1、指定检索条件
		SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
		sourceBuilder.query(QueryBuilders.termQuery("address", "mill"));
		//2.2、按照年龄分布进行聚合
		TermsAggregationBuilder ageAgg = AggregationBuilders.terms("ageAgg").field("age");
		sourceBuilder.aggregation(ageAgg);
		//2.3、计算平均薪资
		AvgAggregationBuilder balanceAvg = AggregationBuilders.avg("balanceAvg").field("balance");
		sourceBuilder.aggregation(balanceAvg);

		//3、添加检索条件
		searchRequest.source(sourceBuilder);

		//4、执行检索
		SearchResponse searchResponse = client.search(searchRequest, GulimallElasticsearchConfig.COMMON_OPTIONS);

		//5、打印检索结果
		System.out.println(searchResponse.toString());

		//6、分析检索结果
		SearchHits hits = searchResponse.getHits();
		SearchHit[] hits1 = hits.getHits();
		for (SearchHit hit : hits1) {
			String sourceAsString = hit.getSourceAsString();
			bankEntity bankEntity = JSON.parseObject(sourceAsString, bankEntity.class);//通过json工具类讲过json字符串转为对象
			System.out.println(bankEntity);
		}

		//7、打印聚合结果
		Aggregations aggregations = searchResponse.getAggregations();
		Terms ageAgg1 = aggregations.get("ageAgg");//这个是term聚合
		for (Terms.Bucket bucket : ageAgg1.getBuckets()) {
			System.out.println("年龄:" + bucket.getKey());
		}

		Avg balanceAvg1 = aggregations.get("balanceAvg");//这个是avg聚合
		System.out.println("平均薪资:" + balanceAvg1.getValue());
	}

	@Data
	static class bankEntity {
		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;
	}

标签:检索,bankEntity,Springboot,42,sourceBuilder,private,HignLevelClient,balanceAvg,Str
From: https://www.cnblogs.com/morehair/p/17062279.html

相关文章

  • 基于springboot的景区旅游信息管理系统(源代码+数据库)
    基于springboot的景区旅游信息管理系统(源代码+数据库)一、系统介绍本项目分为管理员与普通用户两种角色用户登录前台功能:旅游路线、旅游景点、旅游酒店、旅游车票、......
  • springboot整合minio
    代码参考GitHub地址安装minio1、进入官网:https://min.io/我目前安装的是版本是:在cmd窗口中,命令行进行minio.exe所在的文件夹,输入如下命令server后面的地址是你......
  • 41-Springboot整合HignLevelClient----给es中保存数据
    1、导入依赖 <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.4.2</v......
  • SpringBoot2(一)SpringBoot入门程序
    SpringBoot2(一)SpringBoot入门程序在整合SSM框架时,需要大量的配置文件,SpringBoot可以简化这些配置,提高开发效率。并且Spring内置了Tomcat、Jetty、Undertow容器。搭建Spri......
  • 61、全文检索--Springboot整合HignLevelClient
    注意:它默认帮我们导入的elasticsearch版本不是我们使用的7.4.2,因为我们的Springboot项目一致维护了elasticsearch的版本通过以下方式在我们的项目中自己指定版本......
  • 230119_50_SpringBoot入门
    多环境配置文件指定方式一:properites文件文件名可以是application-{profile}.properties/yml,用来指定多个环境版本:server.port=8081//testserver.port=80......
  • springboot上传下载文件原来这么丝滑
    我使用了hutool的 FileUtil,IdUtil,所以需要引入hutool:<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactI......
  • SpringBoot @Target、@Retention、@Documented注解简介
    jdk1.5起开始提供了4个元注解:@Target、@Retention、@Documented、@Inherited。何谓元注解?就是注解的注解。在程序开发中,有时候我们需要自定义一个注解,这个自定义注解类就......
  • SpringBoot日志框架分析
    本文简介第一部分,介绍spring-jcl适配各种日志框架的方式第二部分,介绍slf4j适配各种日志框架的方式第三部分,介绍下logback框架的使用及原理 一、spring-jcl分析说......
  • springboot整合es
    pom.xml增加依赖(注意版本要和springboot匹配)<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-st......