1.创建项目并测试连接

1.1 新建一个spring boot的项目,在pom.xml文件中添加如下引用:

 1 <dependencies>
 2         <dependency>
 3             <groupId>org.springframework.boot</groupId>
 4             <artifactId>spring-boot-starter-web</artifactId>
 5         </dependency>
 6         <!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
 7         <dependency>
 8             <groupId>org.elasticsearch</groupId>
 9             <artifactId>elasticsearch</artifactId>
10             <version>7.3.2</version>
11         </dependency>
12         <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client -->
13         <dependency>
14             <groupId>org.elasticsearch.client</groupId>
15             <artifactId>elasticsearch-rest-high-level-client</artifactId>
16             <version>7.3.2</version>
17         </dependency>
18         <dependency>
19             <groupId>junit</groupId>
20             <artifactId>junit</artifactId>
21             <version>4.12</version>
22         </dependency>
23         <dependency>
24             <groupId>mysql</groupId>
25             <artifactId>mysql-connector-java</artifactId>
26             <scope>runtime</scope>
27         </dependency>
28         <dependency>
29             <groupId>org.projectlombok</groupId>
30             <artifactId>lombok</artifactId>
31             <optional>true</optional>
32         </dependency>
33         <dependency>
34             <groupId>org.springframework.boot</groupId>
35             <artifactId>spring-boot-starter-test</artifactId>
36             <scope>test</scope>
37         </dependency>
38     </dependencies>

 

1.2 新建一个工具类,连接ES服务

 1 package com.demo.esdemo.utils;
 2 
 3 import org.apache.http.HttpHost;
 4 import org.elasticsearch.client.RestClient;
 5 import org.elasticsearch.client.RestClientBuilder;
 6 import org.elasticsearch.client.RestHighLevelClient;
 7 
 8 public class ESClient {
 9     public static RestHighLevelClient getClient() {
10         //创建HttpHost
11         HttpHost host = new HttpHost("localhost", 9200);
12 
13 
14         // 创建RestClientBuilder
15         RestClientBuilder builder = RestClient.builder(host);
16 
17         // 创建RestHighLevelClient
18         RestHighLevelClient client = new RestHighLevelClient(builder);
19 
20         return client;
21     }
22 }

 

1.3 测试连接

1 @Test
2     void testConnect() {
3         RestHighLevelClient client = ESClient.getClient();
4         System.out.println("ok");
5     }

 

1.4 运行测试

 

 

打印"ok",测试通过。

 

2.使用Java操作索引

2.1 使用Java创建索引

 1     String index = "person";
 2     String type = "doc";
 3 
 4 
 5     @Test
 6     void createIndex() throws IOException {
 7         //settings设置
 8         Settings.Builder settings = Settings.builder()
 9                 .put("number_of_shards",3)
10                 .put("number_of_replicas",1);
11 
12 
13         //mappings设置
14         XContentBuilder mappings = JsonXContent.contentBuilder()
15             .startObject()
16                 .startObject("properties")
17                     .startObject("name")
18                         .field("type","text")
19                     .endObject()
20                     .startObject("age")
21                         .field("type","integer")
22                     .endObject()
23                     .startObject("birthday")
24                         .field("type","date")
25                         .field("format","yyyy-MM-dd")
26                     .endObject()
27                 .endObject()
28             .endObject();
29 
30         //将settings和mappings封装到Resquest对象中
31         CreateIndexRequest request = new CreateIndexRequest(index)
32                 .settings(settings)
33                 .mapping(mappings);
34 
35         //通过client连接es,并创建索引
36         RestHighLevelClient client = ESClient.getClient();
37         CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
38 
39         //输出
40         System.out.println(response);
41     }

 

 

 

2.2 检查索引是否存在

 1 String index = "person";
 2     String type = "doc";
 3 
 4     @Test
 5     void exists() throws IOException {
 6         //准备request对象
 7         GetIndexRequest request = new GetIndexRequest(index);
 8 
 9         //通过client连接es
10         RestHighLevelClient client = ESClient.getClient();
11         boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
12 
13         //输出
14         System.out.println(exists);
15     }

 

 

 

2.3 删除索引

 1     String index = "person";
 2     String type = "doc";
 3 
 4     @Test
 5     void delete() throws IOException {
 6         //准备request对象
 7 
 8         DeleteIndexRequest request = new DeleteIndexRequest(index);
 9 
10         //通过client连接es
11         RestHighLevelClient client = ESClient.getClient();
12         AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
13 
14         //输出
15         System.out.println(delete.isAcknowledged());
16     }

 

 

3.使用Java操作文档

3.1 添加文档

 1     String index = "person";
 2     String type = "doc";
 3 
 4     @Test
 5     void createDoc() throws IOException {
 6         //准备一个json数据
 7         Person person = new Person(1,"张三",18,new Date());
 8         ObjectMapper mapper = new ObjectMapper();
 9         String json = mapper.writeValueAsString(person);
10 
11         //准备request对象
12         IndexRequest request = new IndexRequest(index,"_doc","1");
13         request.source(json, XContentType.JSON);
14 
15         //通过client对象连接es
16         RestHighLevelClient client = ESClient.getClient();
17         IndexResponse response = client.index(request, RequestOptions.DEFAULT);
18 
19         //输出
20         System.out.println(response.getResult());
21     }

 

 

3.2 修改与删除文档

 1     String index = "person";
 2     String type = "doc";
 3 
 4     @Test
 5     void updateDoc() throws IOException {
 6         //创建一个map,指定需要修改的内容
 7         Map<String,Object> doc = new HashMap<>();
 8         doc.put("name","李四");
 9 
10         //request对象
11         UpdateRequest request = new UpdateRequest(index,"1");
12         request.doc(doc);
13 
14         //连接es
15         RestHighLevelClient client = ESClient.getClient();
16         UpdateResponse update = client.update(request, RequestOptions.DEFAULT);
17 
18         //输出
19         System.out.println(update.getResult());
20     }

 

 

3.3 删除文档

 1     String index = "person";
 2     String type = "doc";
 3     
 4     @Test
 5     void deleteDoc() throws IOException {
 6         //request对象
 7         DeleteRequest request = new DeleteRequest(index,"1");
 8 
 9         //client执行
10         RestHighLevelClient client = ESClient.getClient();
11         DeleteResponse delete = client.delete(request, RequestOptions.DEFAULT);
12 
13         //输出
14         System.out.println(delete.getResult());
15     }

 

 

4.批量操作文档

4.1 批量添加文档

 1     String index = "person";
 2     String type = "doc";
 3 
 4     @Test
 5     void bulkCreateDoc() throws IOException {
 6         //准备一个json数据
 7         Person person1 = new Person(1,"张三",18,new Date());
 8         Person person2 = new Person(2,"李四",20,new Date());
 9         Person person3 = new Person(3,"王五",24,new Date());
10         ObjectMapper mapper = new ObjectMapper();
11         String json1 = mapper.writeValueAsString(person1);
12         String json2 = mapper.writeValueAsString(person2);
13         String json3 = mapper.writeValueAsString(person3);
14 
15         //准备request对象
16         BulkRequest request = new BulkRequest();
17         request.add(new IndexRequest(index,"_doc",person1.getId().toString()).source(json1,XContentType.JSON));
18         request.add(new IndexRequest(index,"_doc",person2.getId().toString()).source(json2,XContentType.JSON));
19         request.add(new IndexRequest(index,"_doc",person3.getId().toString()).source(json3,XContentType.JSON));
20 
21 
22         //通过client对象连接es
23         RestHighLevelClient client = ESClient.getClient();
24         BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
25 
26         //输出
27         System.out.println(response);
28     }

 

 

4.2 批量删除文档

 1     String index = "person";
 2     String type = "_doc";
 3 
 4     @Test
 5     void buldDeleteDoc() throws IOException {
 6         BulkRequest request = new BulkRequest();
 7         request.add(new DeleteRequest(index,type,"1"));
 8         request.add(new DeleteRequest(index,type,"2"));
 9         request.add(new DeleteRequest(index,type,"3"));
10 
11         RestHighLevelClient client = ESClient.getClient();
12         BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
13 
14         System.out.println(response);
15     }

 

5.查询文档

5.1 使用term方式查询

 1     String index = "book";
 2     String type = "_doc";
 3 
 4     @Test
 5     void termQuery() throws IOException {
 6         //request
 7         SearchRequest request = new SearchRequest(index);
 8         //request.searchType(type);
 9 
10         //指定查询条件
11         SearchSourceBuilder builder = new SearchSourceBuilder();
12         builder.from(0);
13         builder.size(5);
14         builder.query(QueryBuilders.termQuery("name","机器"));
15 
16         request.source(builder);
17 
18         RestHighLevelClient client = ESClient.getClient();
19         SearchResponse search = client.search(request, RequestOptions.DEFAULT);
20 
21         for(SearchHit hit:search.getHits().getHits()){
22             Map<String,Object> result = hit.getSourceAsMap();
23             System.out.println(result);
24         }
25     }

 

 

查询结果是是正确的。 

 

posted on 2021-02-03 20:45  Sempron2800+  阅读(1343)  评论(0编辑  收藏  举报