当前位置:   article > 正文

Elasticsearch学习3-使用RestClient操作es_es request.source建索引

es request.source建索引

1、RestClient 介绍

JavaREST客户端有两种模式:

Java Low Level REST Client:ES官方的低级客户端。低级别的客户端通过http与Elasticearch集群通信。
Java High Level REST Client:ES官方的高级客户端。基于上面的低级客户端,也是通过HTTP与ES集群进行通信。它提供了更多的接口。
此外Spring也对RestClient进行了封装,本文主要介绍Java High Level REST Client的使用

2、使用RestClient操作索引

2.1创建链接

  1. //创建ES连接对象
  2. String esAuth = Base64.encodeBase64String(("test_user" + ":" + "test_user").getBytes());
  3. RestClientBuilder builder = RestClient.builder(new HttpHost("test01.es.58dns.org", 9299, "http"))
  4. .setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder
  5. .setConnectTimeout(13000)
  6. .setSocketTimeout(15000)
  7. .setConnectionRequestTimeout(15000));
  8. builder.setDefaultHeaders(new BasicHeader[]{new BasicHeader("Authorization", "Basic " + esAuth)});
  9. RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
  10. System.out.println(restHighLevelClient);

2.2创建索引库

下方代码仅创建了索引库,没有指定映射mapping

可通过request.source(“映射字符串",XContentType.JSON)指定映射字符串

  1. // 创建索引
  2. CreateIndexRequest createIndexRequest = new CreateIndexRequest("user2");
  3. CreateIndexResponse response = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
  4. boolean acknowledged = response.isAcknowledged();
  5. //判断是否创建索引成功
  6. System.out.println(acknowledged);

2.3 查询索引信息、判断索引库是否存在

  1. //查询索引
  2. GetIndexRequest getIndexRequest = new GetIndexRequest("user2");
  3. GetIndexResponse getIndexResponse = restHighLevelClient.indices().get(getIndexRequest, RequestOptions.DEFAULT);
  4. System.out.println("getIndexResponse.getAliases() = " + getIndexResponse.getAliases());
  5. System.out.println("getIndexResponse.getMappings() = " + getIndexResponse.getMappings());
  6. System.out.println("getIndexResponse.getSetting() = " + getIndexResponse.getSettings());
  7. //判断索引是否存在
  8. boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
  9. System.out.println("exists = " + exists);

2.4删除索引,判断是否删除成功

  1. //删除索引
  2. DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("user2");
  3. AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
  4. //判断是否删除成功
  5. System.out.println("delete.isAcknowledged() = " + delete.isAcknowledged());

3、进行文档操作

  1. //新增数据
  2. User user = new User();
  3. user.setName("小李");
  4. user.setAge(12);
  5. user.setPassword("123321");
  6. //转化为json串
  7. ObjectMapper objectMapper = new ObjectMapper();
  8. String userString = objectMapper.writeValueAsString(user);
  9. //创建请求
  10. IndexRequest indexRequest = new IndexRequest();
  11. //指定索引库名称和id
  12. indexRequest.index("user").id("1");
  13. //指定索引类型
  14. indexRequest.type("_doc");
  15. //指定要添加的内容
  16. indexRequest.source(userString, XContentType.JSON);
  17. IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
  18. System.out.println("index.getId() = " + index.getId());
  19. //查询文档
  20. GetRequest getIndexRequest1 = new GetRequest();
  21. getIndexRequest1.index("user").id("1");
  22. GetResponse getIndexResponse1 = restHighLevelClient.get(getIndexRequest1,RequestOptions.DEFAULT);
  23. String sourceAsString = getIndexResponse1.getSourceAsString();
  24. System.out.println("sourceAsString = " + sourceAsString);
  25. //修改文档
  26. UpdateRequest updateRequest = new UpdateRequest();
  27. updateRequest.index("user").id("1");
  28. updateRequest.doc("name","129212","age",20);
  29. updateRequest.type("_doc");
  30. UpdateResponse update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
  31. System.out.println("update.getGetResult() = " + update.getGetResult());
  32. //再次查询文档
  33. GetRequest getIndexRequest2 = new GetRequest();
  34. getIndexRequest2.index("user").id("1");
  35. getIndexRequest2.type("_doc");
  36. GetResponse getIndexResponse2 = restHighLevelClient.get(getIndexRequest2,RequestOptions.DEFAULT);
  37. String sourceAsString2 = getIndexResponse2.getSourceAsString();
  38. System.out.println("sourceAsString2 = " + sourceAsString2);
  39. //删除文档
  40. DeleteRequest deleteRequest1 = new DeleteRequest();
  41. deleteRequest1.index("user").id("1");
  42. restHighLevelClient.delete(deleteRequest1,RequestOptions.DEFAULT);

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

闽ICP备14008679号