编程语言
首页 > 编程语言> > elasticsearch笔记(3) java操作es的index

elasticsearch笔记(3) java操作es的index

作者:互联网

一: 准备es插件和elasticsearch-rest-high-level-client插件

 

 二.

   2.1创建索引连接

 1 @Test
 2     public void esTest() throws IOException {
 3     //        PUT /books
 4 //        {
 5 //            "settings": {
 6 //            "number_of_replicas": 1,
 7 //                    "number_of_shards": 5
 8 //        },
 9 //            "mappings": {
10 //            "noval": {
11 //                "properties": {
12 //                    "name": {
13 //                        "type": "text",
14 //                                "analyzer": "ik_max_word",
15 //                                "index": true,
16 //                                "store": false
17 //                    },
18 //                    "author":{
19 //                        "type": "keyword"
20 //                    },
21     
22        String hostname = "192.168.43.30";
23        Integer prot = 9200;
24 
25         // 设置索引  类型
26         String index = "person";
27         String type = "man";
28 
29 
30         //设置索引的信息:分片 备份
31         Settings.Builder settings = Settings.builder()
32                 .put("number_of_shards", 5)
33                 .put("number_of_replicas", 1);
34         //构建mapping
35         XContentBuilder mapping = JsonXContent.contentBuilder()
36                 .startObject()
37                   .startObject("properties")
38                     .startObject("name")
39                       .field("type","text")
40                     .endObject()
41                     .startObject("age")
42                       .field("type","integer")
43                     .endObject()
44                   .endObject()
45                 .endObject();
46 
47 //        构建索引
48         CreateIndexRequest createIndexRequest = new CreateIndexRequest(index)
49                 .settings(settings) //分盘和备份
50                 .mapping(type,mapping);
51 
52 
53        //链接es
54         HttpHost httpHost = new HttpHost(hostname,prot);
55         RestClientBuilder restClientBuilder = RestClient.builder(httpHost);
56         RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
57         
58         //创建索引连接
59         CreateIndexResponse createIndexResponse = restHighLevelClient.indices()
60                 .create(createIndexRequest, RequestOptions.DEFAULT);
61       
62 
63     }

2.2 检查索引是否存在

 1     public void findIndex() throws IOException {
 2      //  获取连接restHighLevelClient
 3         HttpHost httpHost = new HttpHost(HOST,PORT);
 4         RestClientBuilder restClientBuilder = RestClient.builder(httpHost);
 5         RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
 6 
 7         GetIndexRequest getIndexRequest = new GetIndexRequest();
 8         getIndexRequest.indices("person");
 9 
10         boolean exists = restHighLevelClient.indices()
11                   .exists(getIndexRequest, RequestOptions.DEFAULT);
12         System.out.println(exists);
13 
14     }

2.3 删除索引

 1  public void deletIndex() throws IOException {
 2         //  获取连接restHighLevelClient
 3         HttpHost httpHost = new HttpHost(HOST,PORT);
 4         RestClientBuilder restClientBuilder = RestClient.builder(httpHost);
 5         RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
 6 
 7         DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest();
 8         deleteIndexRequest.indices("person");
 9 
10         AcknowledgedResponse delete = restHighLevelClient.indices()
11                 .delete(deleteIndexRequest, RequestOptions.DEFAULT);
12         System.out.println(delete.isAcknowledged());
13 
14     }

 

标签:index,java,elasticsearch,restClientBuilder,new,RestHighLevelClient,type,restHigh
来源: https://www.cnblogs.com/dangdanghepingping/p/14401766.html