十、ELASTICSEARCH JAVAAPI-索引管理


 

版本

 
不同版本的java api和 elasticsearch之间存在兼容风险,当前教程使用的 java api用于链接  Elastic Search 6.2,请确保版本一致,否则会出现无法预计的错误

TestElasticSearch4J

 
代码很简单,无非就是获取连接,然后调用对应的API进行 增加和删除。
需要指出来的是,通过 RestHighLevelClient 查看索引是否存在在当前 java api版本(6.2) 里是不提供了,使用的是通过捕捉异常来判断是否存在。
jar 包什么的在右上角的可运行项目里已经包含了,不再赘述
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package  com.how2java;
 
import  java.io.IOException;
 
import  org.apache.http.HttpHost;
import  org.elasticsearch.ElasticsearchStatusException;
import  org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import  org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import  org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
import  org.elasticsearch.client.RestClient;
import  org.elasticsearch.client.RestHighLevelClient;
 
public  class  TestElasticSearch4J {
     private  static  RestHighLevelClient client =  new  RestHighLevelClient(
             RestClient.builder(
                     new  HttpHost( "localhost" 9200 "http" )
             ));
     
     public  static  void  main(String[] args)  throws  IOException {
 
         String indexName =  "how2java" ;
         
         if (!checkExistIndex(indexName)){
             createIndex(indexName);        
         }
 
         if (checkExistIndex(indexName)){
             deleteIndex(indexName);
         }
         checkExistIndex(indexName);
         client.close();
     }
 
     private  static  boolean  checkExistIndex(String indexName)  throws  IOException {
         boolean  result = true ;
         try  {
 
             OpenIndexRequest openIndexRequest =  new  OpenIndexRequest(indexName);
             client.indices().open(openIndexRequest).isAcknowledged();
 
         catch  (ElasticsearchStatusException ex) {
             String m =  "Elasticsearch exception [type=index_not_found_exception, reason=no such index]" ;
             if  (m.equals(ex.getMessage())) {
                 result =  false ;
             }
         }
         if (result)
             System.out.println( "索引:"  +indexName +  " 是存在的" );
         else
             System.out.println( "索引:"  +indexName +  " 不存在" );
         
         return  result;
         
     }
 
     private  static  void  deleteIndex(String indexName)  throws  IOException {
         DeleteIndexRequest request =  new  DeleteIndexRequest(indexName);
         client.indices().delete(request);
         System.out.println( "删除了索引:" +indexName);
 
     }
 
     private  static  void  createIndex(String indexName)  throws  IOException {
         // TODO Auto-generated method stub
         CreateIndexRequest request =  new  CreateIndexRequest(indexName);
         client.indices().create(request);
         System.out.println( "创建了索引:" +indexName);
     }
      
}
实例下载路径:elasticsearch4j .rar : http://download.how2j.cn/1700/elasticsearch4j.rar


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM