use of org.opensearch.client.core.GetSourceResponse in project OpenSearch by opensearch-project.
the class CRUDDocumentationIT method testGetSource.
public void testGetSource() throws Exception {
RestHighLevelClient client = highLevelClient();
{
Request createIndex = new Request("PUT", "/posts");
createIndex.setJsonEntity("{\n" + " \"mappings\" : {\n" + " \"properties\" : {\n" + " \"message\" : {\n" + " \"type\": \"text\",\n" + " \"store\": true\n" + " }\n" + " }\n" + " }\n" + "}");
Response response = client().performRequest(createIndex);
assertEquals(200, response.getStatusLine().getStatusCode());
IndexRequest indexRequest = new IndexRequest("posts").id("1").source("user", "foobar", "postDate", new Date(), "message", "trying out OpenSearch");
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult());
}
// tag::get-source-request
GetSourceRequest getSourceRequest = new GetSourceRequest(// <1>
"posts", // <2>
"1");
// end::get-source-request
// tag::get-source-request-optional
// <2>
String[] includes = Strings.EMPTY_ARRAY;
String[] excludes = new String[] { "postDate" };
getSourceRequest.fetchSourceContext(// <1>
new FetchSourceContext(true, includes, excludes));
// end::get-source-request-optional
// tag::get-source-request-routing
// <1>
getSourceRequest.routing("routing");
// end::get-source-request-routing
// tag::get-source-request-preference
// <1>
getSourceRequest.preference("preference");
// end::get-source-request-preference
// tag::get-source-request-realtime
// <1>
getSourceRequest.realtime(false);
// end::get-source-request-realtime
// tag::get-source-request-refresh
// <1>
getSourceRequest.refresh(true);
// end::get-source-request-refresh
{
// tag::get-source-execute
GetSourceResponse response = client.getSource(getSourceRequest, RequestOptions.DEFAULT);
// end::get-source-execute
// tag::get-source-response
Map<String, Object> source = response.getSource();
// end::get-source-response
Map<String, Object> expectSource = new HashMap<>();
expectSource.put("user", "foobar");
expectSource.put("message", "trying out OpenSearch");
assertEquals(expectSource, source);
}
{
GetSourceRequest request = new GetSourceRequest("posts", "1");
// tag::get-source-execute-listener
ActionListener<GetSourceResponse> listener = new ActionListener<GetSourceResponse>() {
@Override
public void onResponse(GetSourceResponse getResponse) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::get-source-execute-listener
// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::get-source-execute-async
// <1>
client.getSourceAsync(request, RequestOptions.DEFAULT, listener);
// end::get-source-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
}
use of org.opensearch.client.core.GetSourceResponse in project OpenSearch by opensearch-project.
the class CrudIT method testGetSource.
public void testGetSource() throws IOException {
{
GetSourceRequest getRequest = new GetSourceRequest("index", "id");
OpenSearchException exception = expectThrows(OpenSearchException.class, () -> execute(getRequest, highLevelClient()::getSource, highLevelClient()::getSourceAsync));
assertEquals(RestStatus.NOT_FOUND, exception.status());
assertEquals("OpenSearch exception [type=index_not_found_exception, reason=no such index [index]]", exception.getMessage());
assertEquals("index", exception.getMetadata("opensearch.index").get(0));
}
IndexRequest index = new IndexRequest("index").id("id");
String document = "{\"field1\":\"value1\",\"field2\":\"value2\"}";
index.source(document, XContentType.JSON);
index.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
highLevelClient().index(index, RequestOptions.DEFAULT);
{
GetSourceRequest getRequest = new GetSourceRequest("index", "id");
GetSourceResponse response = execute(getRequest, highLevelClient()::getSource, highLevelClient()::getSourceAsync);
Map<String, Object> expectedResponse = new HashMap<>();
expectedResponse.put("field1", "value1");
expectedResponse.put("field2", "value2");
assertEquals(expectedResponse, response.getSource());
}
{
GetSourceRequest getRequest = new GetSourceRequest("index", "does_not_exist");
OpenSearchException exception = expectThrows(OpenSearchException.class, () -> execute(getRequest, highLevelClient()::getSource, highLevelClient()::getSourceAsync));
assertEquals(RestStatus.NOT_FOUND, exception.status());
assertEquals("OpenSearch exception [type=resource_not_found_exception, " + "reason=Document not found [index]/[does_not_exist]]", exception.getMessage());
}
{
GetSourceRequest getRequest = new GetSourceRequest("index", "id");
getRequest.fetchSourceContext(new FetchSourceContext(true, Strings.EMPTY_ARRAY, Strings.EMPTY_ARRAY));
GetSourceResponse response = execute(getRequest, highLevelClient()::getSource, highLevelClient()::getSourceAsync);
Map<String, Object> expectedResponse = new HashMap<>();
expectedResponse.put("field1", "value1");
expectedResponse.put("field2", "value2");
assertEquals(expectedResponse, response.getSource());
}
{
GetSourceRequest getRequest = new GetSourceRequest("index", "id");
getRequest.fetchSourceContext(new FetchSourceContext(true, new String[] { "field1" }, Strings.EMPTY_ARRAY));
GetSourceResponse response = execute(getRequest, highLevelClient()::getSource, highLevelClient()::getSourceAsync);
Map<String, Object> expectedResponse = new HashMap<>();
expectedResponse.put("field1", "value1");
assertEquals(expectedResponse, response.getSource());
}
{
GetSourceRequest getRequest = new GetSourceRequest("index", "id");
getRequest.fetchSourceContext(new FetchSourceContext(true, Strings.EMPTY_ARRAY, new String[] { "field1" }));
GetSourceResponse response = execute(getRequest, highLevelClient()::getSource, highLevelClient()::getSourceAsync);
Map<String, Object> expectedResponse = new HashMap<>();
expectedResponse.put("field2", "value2");
assertEquals(expectedResponse, response.getSource());
}
{
GetSourceRequest getRequest = new GetSourceRequest("index", "id");
getRequest.fetchSourceContext(new FetchSourceContext(false));
OpenSearchException exception = expectThrows(OpenSearchException.class, () -> execute(getRequest, highLevelClient()::getSource, highLevelClient()::getSourceAsync));
assertEquals("OpenSearch exception [type=action_request_validation_exception, " + "reason=Validation Failed: 1: fetching source can not be disabled;]", exception.getMessage());
}
}
Aggregations