当前位置:   article > 正文

Java操作Elasticsearch进行数据检索_java实现elasticsearch搜索mysql数据

java实现elasticsearch搜索mysql数据

1.安装依赖(注意版本要和自己安装的es版本对应)

  1. <dependency>
  2. <groupId>org.elasticsearch.client</groupId>
  3. <artifactId>elasticsearch-rest-high-level-client</artifactId>
  4. <version>7.4.2</version>
  5. </dependency>

         打开发现部分依赖和我们es版本不一致,是因为springboot指定了版本,我们需要更换为自己对应版本。

1.1、改为自己es对应版本

<elasticsearch.version>7.4.2</elasticsearch.version>

 2.编写配置类

  1. @Configuration
  2. public class GullmallElasticSearchConfig {
  3. @Bean
  4. public RestHighLevelClient esRestClient() {
  5. RestClientBuilder builder = null;
  6. builder = RestClient.builder(new HttpHost("192.168.56.10",9200,"http"));
  7. RestHighLevelClient client = new RestHighLevelClient(builder);
  8. return client;
  9. }
  10. }

3.配置类添加请求选项

  1. public static final RequestOptions COMMON_OPTIONS;
  2. static {
  3. RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
  4. // builder.addHeader("Authorization", "Bearer " + TOKEN);
  5. // builder.setHttpAsyncResponseConsumerFactory(
  6. // new HttpAsyncResponseConsumerFactory
  7. // .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
  8. COMMON_OPTIONS = builder.build();
  9. }

4、测试

4.1、存储数据到es

  1. package com.atguigu.gulimall.search;
  2. import com.alibaba.fastjson.JSON;
  3. import com.atguigu.gulimall.search.config.GullmallElasticSearchConfig;
  4. import lombok.Data;
  5. import org.elasticsearch.action.index.IndexRequest;
  6. import org.elasticsearch.action.index.IndexResponse;
  7. import org.elasticsearch.client.RestHighLevelClient;
  8. import org.elasticsearch.common.xcontent.XContentType;
  9. import org.junit.jupiter.api.Test;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.boot.test.context.SpringBootTest;
  12. import java.io.IOException;
  13. /**
  14. * @author cy
  15. * @create 2022-11-04 12:03
  16. */
  17. @SpringBootTest
  18. public class GulimallSearchApplicationTests {
  19. @Autowired
  20. private RestHighLevelClient client;
  21. /**
  22. * 测试存储数据到es(更新也可以)
  23. * @throws IOException
  24. */
  25. @Test
  26. public void indexData() throws IOException {
  27. IndexRequest indexRequest = new IndexRequest("users");
  28. indexRequest.id("1");
  29. // indexRequest.source("userName","zhangsan","age",18,"gender","男");
  30. User user = new User();
  31. user.setName("张三");
  32. user.setGender("男");
  33. user.setAge(18);
  34. String jsonString = JSON.toJSONString(user);
  35. indexRequest.source(jsonString, XContentType.JSON);//要保存的内容
  36. //执行操作
  37. IndexResponse index = client.index(indexRequest, GullmallElasticSearchConfig.COMMON_OPTIONS);
  38. System.out.println(index);
  39. }
  40. @Data
  41. class User {
  42. private String Name;
  43. private String gender;
  44. private Integer age;
  45. }
  46. }

 4.2、检索数据

         使用Kibana工具检索

  1. GET bank/_search
  2. {
  3. "query": {
  4. "match": {
  5. "address": {
  6. "query": "mill"
  7. }
  8. }
  9. },
  10. "aggregations": {
  11. "ageAgg": {
  12. "terms": {
  13. "field": "age",
  14. "size": 10,
  15. }
  16. },
  17. "balanceAvg": {
  18. "avg": {
  19. "field": "balance"
  20. }
  21. }
  22. }
  23. }

        代码实现 :

  1. package com.atguigu.gulimall.search;
  2. import com.alibaba.fastjson.JSON;
  3. import com.atguigu.gulimall.search.config.GullmallElasticSearchConfig;
  4. import com.mysql.cj.QueryBindings;
  5. import lombok.Data;
  6. import lombok.ToString;
  7. import org.elasticsearch.action.index.IndexRequest;
  8. import org.elasticsearch.action.index.IndexResponse;
  9. import org.elasticsearch.action.search.SearchRequest;
  10. import org.elasticsearch.action.search.SearchResponse;
  11. import org.elasticsearch.client.RestHighLevelClient;
  12. import org.elasticsearch.common.xcontent.XContentType;
  13. import org.elasticsearch.index.query.QueryBuilders;
  14. import org.elasticsearch.search.SearchHit;
  15. import org.elasticsearch.search.SearchHits;
  16. import org.elasticsearch.search.aggregations.Aggregation;
  17. import org.elasticsearch.search.aggregations.AggregationBuilders;
  18. import org.elasticsearch.search.aggregations.Aggregations;
  19. import org.elasticsearch.search.aggregations.bucket.terms.Terms;
  20. import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
  21. import org.elasticsearch.search.aggregations.metrics.Avg;
  22. import org.elasticsearch.search.aggregations.metrics.AvgAggregationBuilder;
  23. import org.elasticsearch.search.builder.SearchSourceBuilder;
  24. import org.junit.jupiter.api.Test;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.boot.test.context.SpringBootTest;
  27. import javax.naming.directory.SearchResult;
  28. import java.io.IOException;
  29. /**
  30. * @author cy
  31. * @create 2022-11-04 12:03
  32. */
  33. @SpringBootTest
  34. public class GulimallSearchApplicationTests {
  35. @Autowired
  36. private RestHighLevelClient client;
  37. @ToString
  38. @Data
  39. static class Account {
  40. private int account_number;
  41. private int balance;
  42. private String firstname;
  43. private String lastname;
  44. private int age;
  45. private String gender;
  46. private String address;
  47. private String employer;
  48. private String email;
  49. private String city;
  50. private String state;
  51. }
  52. @Test
  53. public void searchData() throws IOException {
  54. //1、创建构建索引
  55. SearchRequest searchRequest = new SearchRequest();
  56. //1、指定索引
  57. searchRequest.indices("bank");
  58. SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
  59. //1.1、构造检索条件
  60. sourceBuilder.query(QueryBuilders.matchQuery("address","mill"));
  61. //1.2、按照年龄的值分布进行聚合
  62. TermsAggregationBuilder ageAgg = AggregationBuilders.terms("ageAgg").field("age").size(10);
  63. sourceBuilder.aggregation(ageAgg);
  64. //1.3、计算平均薪资
  65. AvgAggregationBuilder balanceAvg = AggregationBuilders.avg("balanceAvg").field("balance");
  66. sourceBuilder.aggregation(balanceAvg);
  67. System.out.println("检索条件"+sourceBuilder.toString());
  68. searchRequest.source(sourceBuilder);
  69. //2、执行检索
  70. SearchResponse searchResponse = client.search(searchRequest, GullmallElasticSearchConfig.COMMON_OPTIONS);
  71. //3、分析结果
  72. System.out.println(searchResponse.toString());
  73. //3.1)、获取所有查到的数据
  74. SearchHits hits = searchResponse.getHits();
  75. SearchHit[] searchHits = hits.getHits();
  76. for (SearchHit hit : searchHits) {
  77. String string = hit.getSourceAsString();
  78. Account account = JSON.parseObject(string, Account.class);
  79. System.out.println("account"+account);
  80. }
  81. //3.2、获取这次检索到的分析信息
  82. Aggregations aggregations = searchResponse.getAggregations();
  83. Terms ageAgg1 = aggregations.get("ageAgg");
  84. for (Terms.Bucket bucket : ageAgg1.getBuckets()) {
  85. String keyAsString = bucket.getKeyAsString();
  86. System.out.println("年龄:"+keyAsString + "==>" +bucket.getDocCount());
  87. }
  88. Avg balanceAvg1 = aggregations.get("balanceAvg");
  89. System.out.println("平均薪资:"+balanceAvg1.getValue());
  90. }
  91. }

         结果:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/952159
推荐阅读
相关标签
  

闽ICP备14008679号