Search in sources :

Example 1 with GetRequest

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();
}
Also used : GetRequest(org.opensearch.action.get.GetRequest) IOException(java.io.IOException) GetResponse(org.opensearch.action.get.GetResponse) UpdateInfo(org.bedework.util.opensearch.DocBuilderBase.UpdateInfo)

Example 2 with GetRequest

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();
    }
}
Also used : SearchHit(org.opensearch.search.SearchHit) GetRequest(org.opensearch.action.get.GetRequest) IOException(java.io.IOException) GetResponse(org.opensearch.action.get.GetResponse) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 3 with GetRequest

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);
}
Also used : GetRequest(org.opensearch.action.get.GetRequest) SneakyThrows(lombok.SneakyThrows)

Example 4 with GetRequest

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());
}
Also used : HashMap(java.util.HashMap) RandomSearchRequestGenerator.randomSearchRequest(org.opensearch.search.RandomSearchRequestGenerator.randomSearchRequest) MasterNodeRequest(org.opensearch.action.support.master.MasterNodeRequest) WriteRequest(org.opensearch.action.support.WriteRequest) AbstractBulkByScrollRequest(org.opensearch.index.reindex.AbstractBulkByScrollRequest) RatedRequest(org.opensearch.index.rankeval.RatedRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) AcknowledgedRequest(org.opensearch.action.support.master.AcknowledgedRequest) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) DocWriteRequest(org.opensearch.action.DocWriteRequest) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) ExplainRequest(org.opensearch.action.explain.ExplainRequest) SearchRequest(org.opensearch.action.search.SearchRequest) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) BulkRequest(org.opensearch.action.bulk.BulkRequest) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) ReplicationRequest(org.opensearch.action.support.replication.ReplicationRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) CountRequest(org.opensearch.client.core.CountRequest) BulkShardRequest(org.opensearch.action.bulk.BulkShardRequest) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) RankEvalRequest(org.opensearch.index.rankeval.RankEvalRequest) DeleteStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest) GetRequest(org.opensearch.action.get.GetRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) GetSourceRequest(org.opensearch.client.core.GetSourceRequest)

Example 5 with GetRequest

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"));
    }
}
Also used : FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) GetRequest(org.opensearch.action.get.GetRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) OpenSearchException(org.opensearch.OpenSearchException) Matchers.containsString(org.hamcrest.Matchers.containsString) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) MultiGetResponse(org.opensearch.action.get.MultiGetResponse) GetResponse(org.opensearch.action.get.GetResponse) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap)

Aggregations

GetRequest (org.opensearch.action.get.GetRequest)27 GetResponse (org.opensearch.action.get.GetResponse)11 MultiGetRequest (org.opensearch.action.get.MultiGetRequest)11 IOException (java.io.IOException)10 IndexRequest (org.opensearch.action.index.IndexRequest)10 ActionListener (org.opensearch.action.ActionListener)8 XContentParser (org.opensearch.common.xcontent.XContentParser)7 Client (org.opensearch.client.Client)6 List (java.util.List)5 OpenSearchException (org.opensearch.OpenSearchException)5 BulkRequest (org.opensearch.action.bulk.BulkRequest)5 DeleteRequest (org.opensearch.action.delete.DeleteRequest)5 UpdateRequest (org.opensearch.action.update.UpdateRequest)5 ResourceNotFoundException (org.opensearch.ResourceNotFoundException)4 DocWriteRequest (org.opensearch.action.DocWriteRequest)4 DeleteStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest)4 GetStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest)4 PutStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest)4 ExplainRequest (org.opensearch.action.explain.ExplainRequest)4 FieldCapabilitiesRequest (org.opensearch.action.fieldcaps.FieldCapabilitiesRequest)4