forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJestDemoApplication.java
174 lines (148 loc) · 6.54 KB
/
JestDemoApplication.java
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.baeldung.jest;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.JestResult;
import io.searchbox.client.JestResultHandler;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.*;
import io.searchbox.indices.CreateIndex;
import io.searchbox.indices.IndicesExists;
import io.searchbox.indices.aliases.AddAliasMapping;
import io.searchbox.indices.aliases.ModifyAliases;
import io.searchbox.indices.aliases.RemoveAliasMapping;
import java.io.IOException;
import java.util.*;
public class JestDemoApplication {
public static void main(String[] args) throws IOException {
// Demo the JestClient
JestClient jestClient = jestClient();
// Check an index
JestResult result = jestClient.execute(new IndicesExists.Builder("employees").build());
if(!result.isSucceeded()) {
System.out.println(result.getErrorMessage());
}
// Create an index
jestClient.execute(new CreateIndex.Builder("employees").build());
// Create an index with options
Map<String, Object> settings = new HashMap<>();
settings.put("number_of_shards", 11);
settings.put("number_of_replicas", 2);
jestClient.execute(new CreateIndex.Builder("employees").settings(settings).build());
// Create an alias, then remove it
jestClient.execute(new ModifyAliases.Builder(
new AddAliasMapping.Builder(
"employees",
"e")
.build())
.build());
JestResult jestResult = jestClient.execute(new ModifyAliases.Builder(
new RemoveAliasMapping.Builder(
"employees",
"e")
.build())
.build());
if(jestResult.isSucceeded()) {
System.out.println("Success!");
}
else {
System.out.println(jestResult.getErrorMessage());
}
// Sample JSON for indexing
// {
// "name": "Michael Pratt",
// "title": "Java Developer",
// "skills": ["java", "spring", "elasticsearch"],
// "yearsOfService": 2
// }
// Index a document from String
ObjectMapper mapper = new ObjectMapper();
JsonNode employeeJsonNode = mapper.createObjectNode()
.put("name", "Michael Pratt")
.put("title", "Java Developer")
.put("yearsOfService", 2)
.set("skills", mapper.createArrayNode()
.add("java")
.add("spring")
.add("elasticsearch"));
jestClient.execute(new Index.Builder(employeeJsonNode.toString()).index("employees").build());
// Index a document from Map
Map<String, Object> employeeHashMap = new LinkedHashMap<>();
employeeHashMap.put("name", "Michael Pratt");
employeeHashMap.put("title", "Java Developer");
employeeHashMap.put("yearsOfService", 2);
employeeHashMap.put("skills", Arrays.asList("java", "spring", "elasticsearch"));
jestClient.execute(new Index.Builder(employeeHashMap).index("employees").build());
// Index a document from POJO
Employee employee = new Employee();
employee.setName("Michael Pratt");
employee.setTitle("Java Developer");
employee.setYearsOfService(2);
employee.setSkills(Arrays.asList("java", "spring", "elasticsearch"));
jestClient.execute(new Index.Builder(employee).index("employees").build());
// Read document by ID
Employee getResult = jestClient.execute(new Get.Builder("employees", "1").build()).getSourceAsObject(Employee.class);
// Search documents
String search = "{\n" +
" \"query\": {\n" +
" \"bool\": {\n" +
" \"must\": [\n" +
" { \"match\": { \"name\": \"Michael Pratt\" }}\n" +
" ]\n" +
" }\n" +
" }\n" +
"}";
List<SearchResult.Hit<Employee, Void>> searchResults =
jestClient.execute(new Search.Builder(search).build())
.getHits(Employee.class);
searchResults.forEach(hit -> {
System.out.println(String.format("Document %s has score %s", hit.id, hit.score));
});
// Update document
employee.setYearsOfService(3);
jestClient.execute(new Update.Builder(employee).index("employees").id("1").build());
// Delete documents
jestClient.execute(new Delete.Builder("2") .index("employees") .build());
// Bulk operations
Employee employeeObject1 = new Employee();
employee.setName("John Smith");
employee.setTitle("Python Developer");
employee.setYearsOfService(10);
employee.setSkills(Arrays.asList("python"));
Employee employeeObject2 = new Employee();
employee.setName("Kate Smith");
employee.setTitle("Senior JavaScript Developer");
employee.setYearsOfService(10);
employee.setSkills(Arrays.asList("javascript", "angular"));
jestClient.execute(new Bulk.Builder().defaultIndex("employees")
.addAction(new Index.Builder(employeeObject1).build())
.addAction(new Index.Builder(employeeObject2).build())
.addAction(new Delete.Builder("3").build()) .build());
// Async operations
Employee employeeObject3 = new Employee();
employee.setName("Jane Doe");
employee.setTitle("Manager");
employee.setYearsOfService(20);
employee.setSkills(Arrays.asList("managing"));
jestClient.executeAsync( new Index.Builder(employeeObject3).build(), new JestResultHandler<JestResult>() {
@Override public void completed(JestResult result) {
// handle result
}
@Override public void failed(Exception ex) {
// handle exception
}
});
}
private static JestClient jestClient()
{
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(
new HttpClientConfig.Builder("http://localhost:9200")
.multiThreaded(true)
.defaultMaxTotalConnectionPerRoute(2)
.maxTotalConnection(20)
.build());
return factory.getObject();
}
}