Java操作elasticSearch复杂查询以及解析数据以及索引保存数据

这篇具有很好参考价值的文章主要介绍了Java操作elasticSearch复杂查询以及解析数据以及索引保存数据。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、Java操作elasticSearch复杂查询以及解析数据

说明:基于银行测试库的操作

es的银行测试库,看一个Kibana操作 然后用java检索解析这个数据

聚合搜索 address 中包含 mill 的所有人的年龄分布以及平均薪资

#聚合搜索 address 中包含 mill 的所有人的年龄分布以及平均薪资
GET bank/_search
{
  "query":{
    "match": {
      "address": "mill"
    }
  },
  "aggs": {
    "ageAgg": {
      "terms": {
        "field": "age",
        "size": 10
      }
    },
    "balanceAvg":{
      "avg":{
        "field": "balance"
      }
    }
  },
  "size": 0
}

以下是分解思路实现步骤:

1.1 拆解操作数据

#聚合搜索 address 中包含 mill 的所有人的年龄分布以及平均年龄
GET bank/_search
{
“query”:{ “match”: { “address”: “mill” }
},
“aggs”: { “ageAgg”: { “terms”: { “field”: “age”, “size”: 10 } },

“balanceAvg”:{ “avg”:{ “field”: “balance” } } }, “size”: 0 }

1.2 构造一个查询器 指向索引

SearchRequest searchRequest = new SearchRequest();
//指定索引
searchRequest.indices("bank");

1.3 封装查询条件器

//指定DSL 检索条件
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//构造检索条件
searchSourceBuilder.query(QueryBuilders.matchQuery("address","mill"));
//按照年龄只分布进行聚合
TermsAggregationBuilder ageAgg = AggregationBuilders.terms("ageAgg").field("age").size(10);
searchSourceBuilder.aggregation(ageAgg);
//计算平均薪资
AvgAggregationBuilder balanceAvg = AggregationBuilders.avg("balanceAvg").field("blance");
searchSourceBuilder.aggregation(balanceAvg);

//打印检索条件 打印结果与Kibana核对
System.out.println("检索条件:"+searchSourceBuilder);
检索条件:{"query":{"match":{"address":{"query":"mill","operator":"OR","prefix_length":0,"max_expansions":50,"fuzzy_transpositions":true,"lenient":false,"zero_terms_query":"NONE","auto_generate_synonyms_phrase_query":true,"boost":1.0}}},"aggregations":{"ageAgg":{"terms":{"field":"age","size":10,"min_doc_count":1,"shard_min_doc_count":0,"show_term_doc_count_error":false,"order":[{"_count":"desc"},{"_key":"asc"}]}},"balanceAvg":{"avg":{"field":"blance"}}}}

1.4 封装的条件器置入查询器

searchRequest.source(searchSourceBuilder);

1.5 容器中的client调用查询:

//执行检索
SearchResponse search = client.search(searchRequest, GuilimallElasticSearchConfig.COMMON_OPTIONS);

1.6 解析查询结果

		System.out.println(search.toString());
//		Map map = JSON.parseObject(search.toString(), Map.class);
		//分析结果 查询结构
		SearchHits hits = search.getHits();
		SearchHit[] searchHits = hits.getHits();
		for (SearchHit hit: searchHits){
//			hit.getIndex();
//			hit.getId();
			String sourceAsString = hit.getSourceAsString();
			Accout accout = JSON.parseObject(sourceAsString, Accout.class);
			System.out.println(accout.toString());
		}
		//获取检索的分析信息
		Aggregations aggregations = search.getAggregations();
//		for (Aggregation aggregation : aggregations.asList()) {
//			System.out.println("当前聚合名字:"+aggregation.getName());
//		}
		//分类聚合
		Terms ageAgg1 = aggregations.get("ageAgg");
		for (Terms.Bucket bucket : ageAgg1.getBuckets()) {
			String keyAsString = bucket.getKeyAsString();
			System.out.println("年龄:" + keyAsString + "人数:"+bucket.getDocCount());
		}
		//平局值
		Avg balanceAvg1 = aggregations.get("balanceAvg");
		System.out.println("平均薪资"+ balanceAvg1.getValue());
{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":4,"relation":"eq"},"max_score":5.4032025,"hits":[{"_index":"bank","_type":"account","_id":"970","_score":5.4032025,"_source":{"account_number":970,"balance":19648,"firstname":"Forbes","lastname":"Wallace","age":28,"gender":"M","address":"990 Mill Road","employer":"Pheast","email":"forbeswallace@pheast.com","city":"Lopezo","state":"AK"}},{"_index":"bank","_type":"account","_id":"136","_score":5.4032025,"_source":{"account_number":136,"balance":45801,"firstname":"Winnie","lastname":"Holland","age":38,"gender":"M","address":"198 Mill Lane","employer":"Neteria","email":"winnieholland@neteria.com","city":"Urie","state":"IL"}},{"_index":"bank","_type":"account","_id":"345","_score":5.4032025,"_source":{"account_number":345,"balance":9812,"firstname":"Parker","lastname":"Hines","age":38,"gender":"M","address":"715 Mill Avenue","employer":"Baluba","email":"parkerhines@baluba.com","city":"Blackgum","state":"KY"}},{"_index":"bank","_type":"account","_id":"472","_score":5.4032025,"_source":{"account_number":472,"balance":25571,"firstname":"Lee","lastname":"Long","age":32,"gender":"F","address":"288 Mill Street","employer":"Comverges","email":"leelong@comverges.com","city":"Movico","state":"MT"}}]},"aggregations":{"lterms#ageAgg":{"doc_count_error_upper_bound":0,"sum_other_doc_count":0,"buckets":[{"key":38,"doc_count":2},{"key":28,"doc_count":1},{"key":32,"doc_count":1}]},"avg#balanceAvg":{"value":null}}}
GulimallSearchApplicationTests.Accout(account_number=970, balance=19648, firstname=Forbes, lastname=Wallace, age=28, gender=M, address=990 Mill Road, employer=Pheast, email=forbeswallace@pheast.com, city=Lopezo, state=AK)
GulimallSearchApplicationTests.Accout(account_number=136, balance=45801, firstname=Winnie, lastname=Holland, age=38, gender=M, address=198 Mill Lane, employer=Neteria, email=winnieholland@neteria.com, city=Urie, state=IL)
GulimallSearchApplicationTests.Accout(account_number=345, balance=9812, firstname=Parker, lastname=Hines, age=38, gender=M, address=715 Mill Avenue, employer=Baluba, email=parkerhines@baluba.com, city=Blackgum, state=KY)
GulimallSearchApplicationTests.Accout(account_number=472, balance=25571, firstname=Lee, lastname=Long, age=32, gender=F, address=288 Mill Street, employer=Comverges, email=leelong@comverges.com, city=Movico, state=MT)
年龄:38人数:2
年龄:28人数:1
年龄:32人数:1
平均薪资25208.0

1.7 打印逐条记录时,可以把结构封装成一个model 借助一下:json.cn

es实现复杂查询,java,elasticsearch,开发语言
es实现复杂查询,java,elasticsearch,开发语言

1.8 完整操作:

	@ToString
		@Data
		static class Accout {

			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;
		}

	@Test
	public void searchData() throws IOException {
		SearchRequest searchRequest = new SearchRequest();
		//指定索引
		searchRequest.indices("bank");
		//指定DSL 检索条件
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		//构造检索条件

	/**
	 #聚合搜索 address 中包含 mill 的所有人的年龄分布以及平均年龄
	 GET bank/_search
	 {
	 "query":{ "match": { "address": "mill" }
	 },
	 "aggs": { "ageAgg": { "terms": { "field": "age", "size": 10 } },
	 "balanceAvg":{ "avg":{ "field": "balance" } } }, "size": 0 }
	 */

//		searchSourceBuilder.aggregation();
//		searchSourceBuilder.from();
//		searchSourceBuilder.size();
		searchSourceBuilder.query(QueryBuilders.matchQuery("address","mill"));
		//按照年龄只分布进行聚合
		TermsAggregationBuilder ageAgg = AggregationBuilders.terms("ageAgg").field("age").size(10);
		searchSourceBuilder.aggregation(ageAgg);
		//计算平均薪资
		AvgAggregationBuilder balanceAvg = AggregationBuilders.avg("balanceAvg").field("balance");
		searchSourceBuilder.aggregation(balanceAvg);

		//打印检索条件
 		System.out.println("检索条件:"+searchSourceBuilder);


		searchRequest.source(searchSourceBuilder);
		//执行检索
		SearchResponse search = client.search(searchRequest, GuilimallElasticSearchConfig.COMMON_OPTIONS);

		//分析结果
//		searchRequest.
		System.out.println(search.toString());
//		Map map = JSON.parseObject(search.toString(), Map.class);
		//分析结果 查询结构
		SearchHits hits = search.getHits();
		SearchHit[] searchHits = hits.getHits();
		for (SearchHit hit: searchHits){
//			hit.getIndex();
//			hit.getId();
			String sourceAsString = hit.getSourceAsString();
			Accout accout = JSON.parseObject(sourceAsString, Accout.class);
			System.out.println(accout.toString());
		}
		//获取检索的分析信息
		Aggregations aggregations = search.getAggregations();
//		for (Aggregation aggregation : aggregations.asList()) {
//			System.out.println("当前聚合名字:"+aggregation.getName());
//		}
		//分类聚合
		Terms ageAgg1 = aggregations.get("ageAgg");
		for (Terms.Bucket bucket : ageAgg1.getBuckets()) {
			String keyAsString = bucket.getKeyAsString();
			System.out.println("年龄:" + keyAsString + "人数:"+bucket.getDocCount());
		}
		//平局值
		Avg balanceAvg1 = aggregations.get("balanceAvg");
		System.out.println("平均薪资"+ balanceAvg1.getValue());

	}

二、Java操作elasticSearch索引保存数据

2.1 计划与实现

存储一个新索引students,然后保存文档

  • 借助Kibana:
> GET /students/_search

结果:
{
  "error" : {
    "root_cause" : [
      {
        "type" : "index_not_found_exception",
        "reason" : "no such index [students]",
        "resource.type" : "index_or_alias",
        "resource.id" : "students",
        "index_uuid" : "_na_",
        "index" : "students"
      }
    ],
    "type" : "index_not_found_exception",
    "reason" : "no such index [students]",
    "resource.type" : "index_or_alias",
    "resource.id" : "students",
    "index_uuid" : "_na_",
    "index" : "students"
  },
  "status" : 404
}

索引不存在

2.2单元测试

索引数据的请求是个网络操作,所以会有异常处理。

//做一个学生对象
	//注解后setter getter
	@Data
	class Student{
		private String name;
		private Integer age;
		private String gender;
	}

	@Test
	public void indexData() throws IOException {
		//索引
		IndexRequest indexRequest = new IndexRequest("students");
		//数据id 不设置会自动生成
		indexRequest.id("1");

		Student student = new Student();
		student.setAge(18);
		student.setGender("男");
		student.setName("张铁蛋");
		//对象转换json
		String jsonString = JSON.toJSONString(student);
		//索引对象加入对象json  声明保存形式
		indexRequest.source(jsonString, XContentType.JSON);
		//用容器中导入的client 调用请求  索引对象 和 配置参数 这个配置参数是整合配置时搞定的
		IndexResponse index = client.index(indexRequest, GuilimallElasticSearchConfig.COMMON_OPTIONS);
		//index为相应数据
		System.out.println(index);
	}

2.3 执行

2021-11-05 16:01:28.219  INFO 1548 --- [           main] c.a.g.s.GulimallSearchApplicationTests   : Started GulimallSearchApplicationTests in 18.417 seconds (JVM running for 19.985)
IndexResponse[index=students,type=_doc,id=1,version=1,result=created,seqNo=0,primaryTerm=1,shards={"total":2,"successful":1,"failed":0}]
2021-11-05 16:01:32.019  INFO 1548 --- [       Thread-9] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

Process finished with exit code 0

2.4 查看一下Kibana操作结果

GET /students/_search

结果集:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "students",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "age" : 18,
          "gender" : "男",
          "name" : "张铁蛋"
        }
      }
    ]
  }
}

至此,保存成功文章来源地址https://www.toymoban.com/news/detail-691937.html

到了这里,关于Java操作elasticSearch复杂查询以及解析数据以及索引保存数据的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • ElasticSearch如何使用以及java代码如何查询并排序ES中的数据(距离排序)

    import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.geo.GeoDistance; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.common.unit.

    2024年04月12日
    浏览(50)
  • ES es Elasticsearch 十三 Java api 实现搜索 分页查询 复杂查询 过滤查询 ids查询 等

    目录 Java api 实现搜索 Pom.xml 建立链接 搜索全部记录 增加规则值查某些字段 搜索分页 全代码 Ids 搜索 搜索Match搜索 multi_match 搜索 多字段搜索 复杂查询 bool查询 filter  bool 复杂查询增加过滤器查询 复杂擦好像加排序 日志 思路 参考 api 写法 写Java代码 请求条件构建层次

    2024年02月04日
    浏览(60)
  • 3、HBase的java API基本操作(创建、删除表以及对数据的添加、删除、查询以及多条件查询)

    1、hbase-2.1.0介绍及分布式集群部署、HA集群部署、验证、硬件配置推荐 2、hbase-2.1.0 shell基本操作详解 3、HBase的java API基本操作(创建、删除表以及对数据的添加、删除、查询以及多条件查询) 4、HBase使用(namespace、数据分区、rowkey设计、原生api访问hbase) 5、Apache Phoenix(5.0.0-5.

    2024年02月08日
    浏览(64)
  • 数据库实验一:基本表操作、基本数据查询和复杂数据查询

    按下图创建四个表:teacher、student、course和SC,为属性选择合适的域、合适的主码和外键约束,并为他们插入所列出数据; 中文语义 teacher (TID,TNAME,DEPT,SALARY) 教师(教工号,姓名,系,薪水) student (SID,SNAME,DEPT,AGE,GENDER) 学生(学号,姓名,系,年龄,性别) course (CID,CNAME,DE

    2024年02月01日
    浏览(63)
  • C/C++数据结构之时间复杂度和空间复杂度详细解析以及力扣刷题

    个人主页:点我进入主页 专栏分类:C语言初阶      C语言程序设计————KTV       C语言小游戏     C语言进阶 C语言刷题       数据结构初阶 欢迎大家点赞,评论,收藏。 一起努力,一起奔赴大厂。 目录  1.前言 2.算法的效率 2.1时间复杂度  2.1.1时间复杂度的定义

    2024年02月06日
    浏览(44)
  • Java根据id对elasticsearch查询操作

    一、根据一个id查询 二、根据多个ids查询

    2024年02月12日
    浏览(53)
  • 【ElasticSearch】ElasticSearch Java API的使用——常用索引、文档、查询操作(二)

    Elaticsearch ,简称为es,es是一个开源的 高扩展 的 分布式全文检索引擎 ,它可以近乎 实时的存储 、 检索数据; 本身扩展性很好,可以扩展到上百台服务器,处理PB级别(大数据时代)的数据。es也使用java开发并使用Lucene作为其核心来实现所有索引和搜索的功能,但是它的 目的

    2024年01月16日
    浏览(91)
  • ElasticSearch学习4--复杂查询

    查询所有:查询出所有数据,一般测试用。例如:match_all 全文检索(full text)查询:利用分词器对用户输入内容分词,然后去倒排索引库中匹配。例如:         match_query 根据单个字段查询         multi_match_query  根据多个字段查询         3.精确查询:根据精确

    2024年02月11日
    浏览(43)
  • ElasticSearch多条件复杂查询实现

    前面实现方式和但条件一致 查询代码区别如下 注释里面标注了或者和and那两行的区别 网站链接 elasticsearch(ES)在SpringBoot中的复杂查询(多条件分页查询以及聚合查询)_尺规作图的博客-CSDN博客_springboot 整合es多条件

    2024年02月04日
    浏览(48)
  • Java解析JSON复杂数据的第三种思路

    本文介绍了Java解析JSON复杂数据的第三种思路,通过改变思路,按照新的流程获取数据。文章详细解析了接口JSON数据,并提供了JSON转XML数据

    2024年01月25日
    浏览(63)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包