forked from kumjiten/Java-Client
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Example.java
115 lines (98 loc) · 5.01 KB
/
Example.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
import java.io.IOException;
import java.util.Objects;
import org.apache.http.HttpHost;
import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.client.opensearch._types.OpenSearchException;
import org.opensearch.client.opensearch.core.IndexRequest;
import org.opensearch.client.opensearch.core.IndexResponse;
import org.opensearch.client.opensearch.core.InfoResponse;
import org.opensearch.client.opensearch.core.SearchResponse;
import org.opensearch.client.opensearch.core.UpdateRequest;
import org.opensearch.client.opensearch.core.UpdateResponse;
import org.opensearch.client.opensearch.indices.CreateIndexRequest;
import org.opensearch.client.opensearch.indices.DeleteIndexRequest;
import org.opensearch.client.opensearch.indices.IndexSettings;
import org.opensearch.client.opensearch.indices.PutIndicesSettingsRequest;
import org.opensearch.client.transport.aws.AwsSdk2Transport;
import org.opensearch.client.transport.aws.AwsSdk2TransportOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.apache.ApacheHttpClient;
import software.amazon.awssdk.regions.Region;
public class Example {
public static void main(final String[] args) throws IOException, InterruptedException {
Logger logger = LoggerFactory.getLogger(Example.class);
SdkHttpClient httpClient = ApacheHttpClient.builder().build();
String endpoint = System.getenv("ENDPOINT");
if (endpoint == null) {
throw new RuntimeException("missing ENDPOINT");
}
String region = System.getenv().getOrDefault("AWS_REGION", "us-east-1");
String service = System.getenv().getOrDefault("SERVICE", "es");
try {
OpenSearchClient client = new OpenSearchClient(
new AwsSdk2Transport(
httpClient,
HttpHost.create(endpoint).getHostName(),
service,
Region.of(region),
AwsSdk2TransportOptions.builder().build()));
// TODO: remove when Serverless supports GET /
if (! service.equals("aoss")) {
InfoResponse info = client.info();
logger.info(info.version().distribution() + ": " + info.version().number());
}
// create the index
String index = "movies";
CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder().index(index).build();
try {
client.indices().create(createIndexRequest);
// add settings to the index
IndexSettings indexSettings = new IndexSettings.Builder().build();
PutIndicesSettingsRequest putSettingsRequest = new PutIndicesSettingsRequest.Builder()
.index(index)
.settings(indexSettings)
.build();
client.indices().putSettings(putSettingsRequest);
} catch (OpenSearchException ex) {
final String errorType = Objects.requireNonNull(ex.response().error().type());
if (! errorType.equals("resource_already_exists_exception")) {
throw ex;
}
}
// index data
Movie movie = new Movie("Bennett Miller", "Moneyball", 2011);
IndexRequest<Movie> indexRequest = new IndexRequest.Builder<Movie>()
.index(index)
.id("1")
.document(movie)
.build();
IndexResponse indexResponse = client.index(indexRequest);
System.out.println(String.format("Document %s.", indexResponse.result().toString().toLowerCase()));
// update data
Movie movieUpdate = new Movie("Bennett Miller", "Moneyball 2", 2011);
UpdateRequest<Movie, Movie> updateRequest = new UpdateRequest.Builder<Movie, Movie>()
.id("1")
.index(index)
.doc(movieUpdate)
.build();
UpdateResponse<Movie> updateResponse = client.update(updateRequest, Movie.class);
System.out.println(String.format("Document %s.", updateResponse.result().toString().toLowerCase()));
// wait for the document to index
Thread.sleep(3000);
// search for the document
SearchResponse<Movie> searchResponse = client.search(s -> s.index(index), Movie.class);
for (int i = 0; i < searchResponse.hits().hits().size(); i++) {
logger.info(searchResponse.hits().hits().get(i).source().toString());
}
// delete the document
client.delete(b -> b.index(index).id("1"));
// delete the index
DeleteIndexRequest deleteRequest = new DeleteIndexRequest.Builder().index(index).build();
client.indices().delete(deleteRequest);
} finally {
httpClient.close();
}
}
}