use of org.opensearch.client.core.GetSourceRequest in project OpenSearch by opensearch-project.
the class RequestConverters method sourceRequest.
private static Request sourceRequest(GetSourceRequest getSourceRequest, String httpMethodName) {
Params parameters = new Params();
parameters.withPreference(getSourceRequest.preference());
parameters.withRouting(getSourceRequest.routing());
parameters.withRefresh(getSourceRequest.refresh());
parameters.withRealtime(getSourceRequest.realtime());
parameters.withFetchSourceContext(getSourceRequest.fetchSourceContext());
String endpoint = endpoint(getSourceRequest.index(), "_source", getSourceRequest.id());
Request request = new Request(httpMethodName, endpoint);
request.addParameters(parameters.asMap());
return request;
}
use of org.opensearch.client.core.GetSourceRequest in project OpenSearch by opensearch-project.
the class RequestConvertersTests method doTestSourceExists.
private static void doTestSourceExists(BiFunction<String, String, GetSourceRequest> requestFunction) throws IOException {
String index = randomAlphaOfLengthBetween(3, 10);
String id = randomAlphaOfLengthBetween(3, 10);
final GetSourceRequest getRequest = requestFunction.apply(index, id);
Map<String, String> expectedParams = new HashMap<>();
if (randomBoolean()) {
String preference = randomAlphaOfLengthBetween(3, 10);
getRequest.preference(preference);
expectedParams.put("preference", preference);
}
if (randomBoolean()) {
String routing = randomAlphaOfLengthBetween(3, 10);
getRequest.routing(routing);
expectedParams.put("routing", routing);
}
if (randomBoolean()) {
boolean realtime = randomBoolean();
getRequest.realtime(realtime);
if (realtime == false) {
expectedParams.put("realtime", "false");
}
}
if (randomBoolean()) {
boolean refresh = randomBoolean();
getRequest.refresh(refresh);
if (refresh) {
expectedParams.put("refresh", "true");
}
}
Request request = RequestConverters.sourceExists(getRequest);
assertEquals(HttpHead.METHOD_NAME, request.getMethod());
assertEquals("/" + index + "/_source/" + id, request.getEndpoint());
assertEquals(expectedParams, request.getParameters());
assertNull(request.getEntity());
}
use of org.opensearch.client.core.GetSourceRequest in project OpenSearch by opensearch-project.
the class CrudIT method testSourceExists.
public void testSourceExists() throws IOException {
{
GetSourceRequest getRequest = new GetSourceRequest("index", "id");
assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
}
IndexRequest index = new IndexRequest("index").id("id");
index.source("{\"field1\":\"value1\",\"field2\":\"value2\"}", XContentType.JSON);
index.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
highLevelClient().index(index, RequestOptions.DEFAULT);
{
GetSourceRequest getRequest = new GetSourceRequest("index", "id");
assertTrue(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
}
{
GetSourceRequest getRequest = new GetSourceRequest("index", "does_not_exist");
assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
}
}
use of org.opensearch.client.core.GetSourceRequest 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.GetSourceRequest in project OpenSearch by opensearch-project.
the class RequestConvertersTests method doTestGetSource.
private static void doTestGetSource(BiFunction<String, String, GetSourceRequest> requestFunction) throws IOException {
String index = randomAlphaOfLengthBetween(3, 10);
String id = randomAlphaOfLengthBetween(3, 10);
final GetSourceRequest getRequest = requestFunction.apply(index, id);
Map<String, String> expectedParams = new HashMap<>();
if (randomBoolean()) {
String preference = randomAlphaOfLengthBetween(3, 10);
getRequest.preference(preference);
expectedParams.put("preference", preference);
}
if (randomBoolean()) {
String routing = randomAlphaOfLengthBetween(3, 10);
getRequest.routing(routing);
expectedParams.put("routing", routing);
}
if (randomBoolean()) {
boolean realtime = randomBoolean();
getRequest.realtime(realtime);
if (realtime == false) {
expectedParams.put("realtime", "false");
}
}
if (randomBoolean()) {
boolean refresh = randomBoolean();
getRequest.refresh(refresh);
if (refresh) {
expectedParams.put("refresh", "true");
}
}
Request request = RequestConverters.getSource(getRequest);
assertEquals(HttpGet.METHOD_NAME, request.getMethod());
assertEquals("/" + index + "/_source/" + id, request.getEndpoint());
assertEquals(expectedParams, request.getParameters());
assertNull(request.getEntity());
}
Aggregations