资讯专栏INFORMATION COLUMN

Elasticsearch入门学习(四):使用javaAPI学习ES

kun_jian / 823人阅读

摘要:一依赖刚开始少这个包创建索引失败官方文档并没有给这个提示二开始之前的准备官方文档连接操作所用到的实体类三关于索引的操作官方文档新增索引索引名称分片副本内容查询指定索引索引名称删除索引四关于文档的操作官方文档创建文档索引名称前

一、Maven依赖

</>复制代码

  1. org.elasticsearch
  2. elasticsearch
  3. 7.1.0
  4. org.elasticsearch.client
  5. elasticsearch-rest-high-level-client
  6. 7.1.0
二、开始之前的准备

官方文档

</>复制代码

  1. /**
  2. * 连接ES
  3. * @return
  4. */
  5. public RestHighLevelClient start() {
  6. RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
  7. RestClient.builder(
  8. new HttpHost("192.168.100.151", 9201, "http"),
  9. new HttpHost("192.168.100.151", 9202, "http"),
  10. new HttpHost("192.168.100.151", 9203, "http")));
  11. return restHighLevelClient;
  12. }
  13. /**
  14. * 操作所用到的实体类
  15. */
  16. @Data
  17. class Article{
  18. private long id;
  19. private String title;
  20. public Article(long id, String title) {
  21. this.id = id;
  22. this.title = title;
  23. }
  24. }
三、关于索引的操作

官方文档

新增索引

</>复制代码

  1. public void createIndex(RestHighLevelClient client) {
  2. //索引名称
  3. CreateIndexRequest request = new CreateIndexRequest("hello");
  4. //分片副本
  5. request.settings(Settings.builder().put("index.number_of_shards", 5)
  6. .put("index.number_of_replicas", 1));
  7. //内容
  8. Map id = new HashMap <>();
  9. id.put("type","text");
  10. id.put("store",true);
  11. Map title = new HashMap <>();
  12. title.put("type","text");
  13. title.put("store",true);
  14. title.put("index",true);
  15. title.put("analyzer", "standard");
  16. Map properties = new HashMap <>();
  17. properties.put("id",id);
  18. properties.put("title",title);
  19. Map mapping = new HashMap <>();
  20. mapping.put("properties",properties);
  21. request.mapping(mapping);
  22. try {
  23. CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
  24. System.out.println(response.toString());
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. }

查询指定索引

</>复制代码

  1. public void getIndex(RestHighLevelClient client) throws IOException {
  2. //索引名称
  3. GetIndexRequest request = new GetIndexRequest("hello");
  4. try {
  5. boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
  6. System.out.println(exists);
  7. } catch (IOException e) {
  8. e.printStackTrace();
  9. }
  10. }

删除索引

</>复制代码

  1. public void delIndex(RestHighLevelClient client){
  2. DeleteIndexRequest request = new DeleteIndexRequest("hello");
  3. try {
  4. AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
  5. System.out.println(delete.toString());
  6. } catch (IOException e) {
  7. e.printStackTrace();
  8. }
  9. }
四、关于文档的操作

官方文档

创建文档

</>复制代码

  1. public void createDocument(RestHighLevelClient client){
  2. //索引名称
  3. IndexRequest indexRequest = new IndexRequest("hello");
  4. ObjectMapper mapper = new ObjectMapper();
  5. Article article = new Article(3L, "web前端");
  6. byte[] json = new byte[0];
  7. try {
  8. json = mapper.writeValueAsBytes(article);
  9. //可以设置文章ID
  10. indexRequest.id("5");
  11. indexRequest.source(json, XContentType.JSON);
  12. IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
  13. } catch (Exception e) {
  14. e.printStackTrace();
  15. }
  16. }

根据文档ID查询文档

</>复制代码

  1. public void getDocument(RestHighLevelClient client){
  2. GetRequest getRequest = new GetRequest("hello", "1");
  3. GetResponse documentFields = null;
  4. try {
  5. documentFields = client.get(getRequest, RequestOptions.DEFAULT);
  6. System.out.println(documentFields.toString());
  7. } catch (IOException e) {
  8. e.printStackTrace();
  9. }
  10. }

更新文档

</>复制代码

  1. public void updateDocument(RestHighLevelClient client){
  2. UpdateRequest updateRequest = new UpdateRequest("hello", "1");
  3. Article article = new Article(2L, "java入门到放弃");
  4. ObjectMapper mapper = new ObjectMapper();
  5. byte[] json = new byte[0];
  6. try {
  7. json = mapper.writeValueAsBytes(article);
  8. IndexRequest indexRequest = new IndexRequest("hello");
  9. indexRequest.source(json, XContentType.JSON);
  10. updateRequest.doc(indexRequest);
  11. UpdateResponse updateResponse = client.update(
  12. updateRequest, RequestOptions.DEFAULT);
  13. System.out.println(updateResponse);
  14. } catch (Exception e) {
  15. e.printStackTrace();
  16. }
  17. }

删除文档

</>复制代码

  1. public void delDocument(RestHighLevelClient client){
  2. DeleteRequest request = new DeleteRequest("hello", "1");
  3. try {
  4. DeleteResponse deleteResponse = client.delete(
  5. request, RequestOptions.DEFAULT);
  6. System.out.println(deleteResponse);
  7. } catch (IOException e) {
  8. e.printStackTrace();
  9. }
  10. }

查询文档

</>复制代码

  1. public void searchDocument(RestHighLevelClient client){
  2. SearchRequest searchRequest = new SearchRequest();
  3. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  4. searchSourceBuilder.query(QueryBuilders.matchAllQuery());
  5. searchRequest.source(searchSourceBuilder);
  6. SearchResponse searchResponse = null;
  7. try {
  8. searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
  9. System.out.println(searchResponse.toString());
  10. } catch (IOException e) {
  11. e.printStackTrace();
  12. }
  13. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/75357.html

相关文章

  • 慕课网_《ElasticSearch入门学习总结

    摘要:时间年月日星期四说明本文部分内容均来自慕课网。那么里面的数据就可以分为各种各样的索引,比如汽车索引图书索引家具索引等等。图书索引又可以细分为各种类型,比如科普类小说类技术类等等。具体到每一本书籍,就是文档,就是整个图书里面最小的存储单位。 时间:2017年09月14日星期四说明:本文部分内容均来自慕课网。@慕课网:http://www.imooc.com教学源码:无学习源码:https...

    notebin 评论0 收藏0
  • 渣渣为什么要看 ElasticSearch 源码?

    摘要:当时自己在本地测试搭建集群后,给分配了另外一个任务就是去了解中的自带分词英文分词中文分词的相同与差异以及自己建立分词需要注意的点。还有就是官网的文档了,非常非常详细,还有,版本的是有中文的官方文档,可以凑合着看。 前提 人工智能、大数据快速发展的今天,对于 TB 甚至 PB 级大数据的快速检索已然成为刚需,大型企业早已淹没在系统生成的浩瀚数据流当中。大数据技术业已集中在如何存储和处理这...

    Cciradih 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<