Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit aa66b1f

Browse files
committed
feat: ES6 java 客户端示例
1 parent 8d1ef20 commit aa66b1f

File tree

10 files changed

+610
-193
lines changed

10 files changed

+610
-193
lines changed

‎codes/javadb/elasticsearch/elasticsearch6/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
<artifactId>hutool-all</artifactId>
3333
<version>5.7.20</version>
3434
</dependency>
35+
<dependency>
36+
<groupId>com.fasterxml.jackson.core</groupId>
37+
<artifactId>jackson-databind</artifactId>
38+
<version>2.15.2</version>
39+
</dependency>
3540

3641
<dependency>
3742
<groupId>ch.qos.logback</groupId>

‎codes/javadb/elasticsearch/elasticsearch6/src/main/java/io/github/dunwu/javadb/elasticsearch/Demo.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22

33
import io.github.dunwu.javadb.elasticsearch.entity.User;
44
import io.github.dunwu.javadb.elasticsearch.mapper.UserEsMapper;
5+
import io.github.dunwu.javadb.elasticsearch.util.ElasticsearchUtil;
6+
import org.elasticsearch.client.RestHighLevelClient;
57

68
import java.io.IOException;
79
import java.util.Arrays;
810
import java.util.List;
911

10-
1112
public class Demo {
13+
14+
private static final String HOSTS = "127.0.0.1:9200";
15+
private static final RestHighLevelClient restHighLevelClient = ElasticsearchUtil.newRestHighLevelClient(HOSTS);
16+
1217
public static void main(String[] args) throws IOException, InterruptedException {
13-
UserEsMapper mapper = new UserEsMapper();
18+
19+
UserEsMapper mapper = new UserEsMapper(restHighLevelClient);
1420

1521
System.out.println("索引是否存在:" + mapper.isIndexExists());
1622

@@ -24,6 +30,7 @@ public static void main(String[] args) throws IOException, InterruptedException
2430
System.out.println("根据ID批量查询:" + mapper.pojoListByIds(Arrays.asList("1", "2")).toString());
2531

2632
Thread.sleep(1000);
27-
System.out.println("根据ID批量删除:" + mapper.deleteByIds(Arrays.asList("1", "2")));
33+
System.out.println("根据ID批量删除:" + mapper.batchDeleteById(Arrays.asList("1", "2")));
2834
}
35+
2936
}

‎codes/javadb/elasticsearch/elasticsearch6/src/main/java/io/github/dunwu/javadb/elasticsearch/entity/EsEntity.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@
77
* @since 2023年06月28日
88
*/
99
public interface EsEntity {
10-
Long getId();
10+
11+
/**
12+
* 获取 ES 主键
13+
*/
14+
String getDocId();
15+
1116
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.github.dunwu.javadb.elasticsearch.entity;
2+
3+
import lombok.Data;
4+
5+
import java.util.ArrayList;
6+
import java.util.Collection;
7+
import java.util.List;
8+
9+
/**
10+
* 分页实体
11+
*
12+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
13+
* @date 2023年06月28日
14+
*/
15+
@Data
16+
public class Page<T> {
17+
18+
private long total;
19+
private int page;
20+
private int size;
21+
private List<T> content = new ArrayList<>();
22+
23+
public Page(long total, int page, int size) {
24+
this.total = total;
25+
this.page = page;
26+
this.size = size;
27+
}
28+
29+
public Page(long total, int page, int size, Collection<T> list) {
30+
this.total = total;
31+
this.page = page;
32+
this.size = size;
33+
this.content.addAll(list);
34+
}
35+
36+
}

‎codes/javadb/elasticsearch/elasticsearch6/src/main/java/io/github/dunwu/javadb/elasticsearch/entity/User.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@
1212
@Data
1313
@Builder
1414
public class User implements EsEntity {
15+
1516
private Long id;
1617
private String username;
1718
private String password;
1819
private Integer age;
1920
private String email;
2021

22+
@Override
23+
public String getDocId() {
24+
return null;
25+
}
26+
2127
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /