use of org.opensearch.action.get.GetRequest in project bw-calendar-engine by Bedework.
the class BwIndexEsImpl method currentChangeToken.
@Override
public String currentChangeToken() {
UpdateInfo ui;
try {
final GetRequest req = new GetRequest(targetIndex, updateTrackerId).storedFields("esUpdateCount");
final GetResponse resp = getClient().get(req, RequestOptions.DEFAULT);
if (!resp.isExists()) {
return null;
}
final EntityBuilder er = getEntityBuilder(resp.getFields());
ui = er.makeUpdateInfo();
synchronized (updateInfo) {
final UpdateInfo fromTbl = updateInfo.get(targetIndex);
if ((fromTbl == null) || (fromTbl.getCount() < ui.getCount())) {
updateInfo.put(targetIndex, ui);
} else {
ui = fromTbl;
}
}
return ui.getChangeToken();
} catch (final IOException ie) {
warn("Exception getting UpdateInfo: " + ie.getLocalizedMessage());
ui = new UpdateInfo();
}
return ui.getChangeToken();
}
use of org.opensearch.action.get.GetRequest in project bw-calendar-engine by Bedework.
the class BwIndexEsImpl method fetchEntity.
private EntityBuilder fetchEntity(final String docType, final String val, final PropertyInfoIndex... index) throws CalFacadeException {
requireDocType(docType);
try {
fetchStart();
if ((!docType.equals(docTypeCollection) && !docType.equals(docTypeEvent)) && (index.length == 1) && (index[0] == PropertyInfoIndex.HREF)) {
// This path avoids the tombstone check.
final GetRequest req = new GetRequest(targetIndex, val);
final GetResponse gr = getClient().get(req, RequestOptions.DEFAULT);
if (!gr.isExists()) {
return null;
}
return getEntityBuilder(gr.getSourceAsMap());
}
final SearchHit hit = fetchEntity(docType, getFilters(null).singleEntityQuery(val, index));
if (hit == null) {
return null;
}
return getEntityBuilder(hit.getSourceAsMap());
} catch (final IOException ie) {
throw new CalFacadeException(ie);
} finally {
fetchEnd();
}
}
use of org.opensearch.action.get.GetRequest in project veilarbportefolje by navikt.
the class OpensearchTestClient method fetchDocument.
@SneakyThrows
public GetResponse fetchDocument(AktorId aktoerId) {
GetRequest getRequest = new GetRequest();
getRequest.index(indexName.getValue());
getRequest.id(aktoerId.toString());
return restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
}
use of org.opensearch.action.get.GetRequest 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.action.get.GetRequest in project OpenSearch by opensearch-project.
the class CrudIT method testGet.
public void testGet() throws IOException {
{
GetRequest getRequest = new GetRequest("index", "id");
OpenSearchException exception = expectThrows(OpenSearchException.class, () -> execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync));
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);
{
GetRequest getRequest = new GetRequest("index", "id").version(2);
OpenSearchException exception = expectThrows(OpenSearchException.class, () -> execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync));
assertEquals(RestStatus.CONFLICT, exception.status());
assertEquals("OpenSearch exception [type=version_conflict_engine_exception, " + "reason=[id]: " + "version conflict, current version [1] is different than the one provided [2]]", exception.getMessage());
assertEquals("index", exception.getMetadata("opensearch.index").get(0));
}
{
GetRequest getRequest = new GetRequest("index", "id");
if (randomBoolean()) {
getRequest.version(1L);
}
GetResponse getResponse = execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync);
assertEquals("index", getResponse.getIndex());
assertEquals("id", getResponse.getId());
assertTrue(getResponse.isExists());
assertFalse(getResponse.isSourceEmpty());
assertEquals(1L, getResponse.getVersion());
assertEquals(document, getResponse.getSourceAsString());
}
{
GetRequest getRequest = new GetRequest("index", "does_not_exist");
GetResponse getResponse = execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync);
assertEquals("index", getResponse.getIndex());
assertEquals("does_not_exist", getResponse.getId());
assertFalse(getResponse.isExists());
assertEquals(-1, getResponse.getVersion());
assertTrue(getResponse.isSourceEmpty());
assertNull(getResponse.getSourceAsString());
}
{
GetRequest getRequest = new GetRequest("index", "id");
getRequest.fetchSourceContext(new FetchSourceContext(false, Strings.EMPTY_ARRAY, Strings.EMPTY_ARRAY));
GetResponse getResponse = execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync);
assertEquals("index", getResponse.getIndex());
assertEquals("id", getResponse.getId());
assertTrue(getResponse.isExists());
assertTrue(getResponse.isSourceEmpty());
assertEquals(1L, getResponse.getVersion());
assertNull(getResponse.getSourceAsString());
}
{
GetRequest getRequest = new GetRequest("index", "id");
if (randomBoolean()) {
getRequest.fetchSourceContext(new FetchSourceContext(true, new String[] { "field1" }, Strings.EMPTY_ARRAY));
} else {
getRequest.fetchSourceContext(new FetchSourceContext(true, Strings.EMPTY_ARRAY, new String[] { "field2" }));
}
GetResponse getResponse = execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync);
assertEquals("index", getResponse.getIndex());
assertEquals("id", getResponse.getId());
assertTrue(getResponse.isExists());
assertFalse(getResponse.isSourceEmpty());
assertEquals(1L, getResponse.getVersion());
Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
assertEquals(1, sourceAsMap.size());
assertEquals("value1", sourceAsMap.get("field1"));
}
}
Aggregations