Search in sources :

Example 26 with GetRequest

use of org.opensearch.action.get.GetRequest in project OpenSearch by opensearch-project.

the class TransportGetTaskAction method getFinishedTaskFromIndex.

/**
 * Send a {@link GetRequest} to the tasks index looking for a persisted copy of the task completed task. It'll only be found only if the
 * task's result was stored. Called on the node that once had the task if that node is still part of the cluster or on the
 * coordinating node if the node is no longer part of the cluster.
 */
void getFinishedTaskFromIndex(Task thisTask, GetTaskRequest request, ActionListener<GetTaskResponse> listener) {
    GetRequest get = new GetRequest(TaskResultsService.TASK_INDEX, request.getTaskId().toString());
    get.setParentTask(clusterService.localNode().getId(), thisTask.getId());
    client.get(get, ActionListener.wrap(r -> onGetFinishedTaskFromIndex(r, listener), e -> {
        if (ExceptionsHelper.unwrap(e, IndexNotFoundException.class) != null) {
            // We haven't yet created the index for the task results so it can't be found.
            listener.onFailure(new ResourceNotFoundException("task [{}] isn't running and hasn't stored its results", e, request.getTaskId()));
        } else {
            listener.onFailure(e);
        }
    }));
}
Also used : HandledTransportAction(org.opensearch.action.support.HandledTransportAction) AbstractRunnable(org.opensearch.common.util.concurrent.AbstractRunnable) ThreadPool(org.opensearch.threadpool.ThreadPool) OpenSearchException(org.opensearch.OpenSearchException) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) XContentParser(org.opensearch.common.xcontent.XContentParser) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Inject(org.opensearch.common.inject.Inject) ActionListener(org.opensearch.action.ActionListener) GetResponse(org.opensearch.action.get.GetResponse) TaskResultsService(org.opensearch.tasks.TaskResultsService) Client(org.opensearch.client.Client) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) GetRequest(org.opensearch.action.get.GetRequest) LoggingDeprecationHandler(org.opensearch.common.xcontent.LoggingDeprecationHandler) TaskResult(org.opensearch.tasks.TaskResult) TransportRequestOptions(org.opensearch.transport.TransportRequestOptions) ExceptionsHelper(org.opensearch.ExceptionsHelper) TaskId(org.opensearch.tasks.TaskId) IOException(java.io.IOException) Task(org.opensearch.tasks.Task) TransportService(org.opensearch.transport.TransportService) XContentHelper(org.opensearch.common.xcontent.XContentHelper) ActionFilters(org.opensearch.action.support.ActionFilters) TaskInfo(org.opensearch.tasks.TaskInfo) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) TransportListTasksAction.waitForCompletionTimeout(org.opensearch.action.admin.cluster.node.tasks.list.TransportListTasksAction.waitForCompletionTimeout) ActionListenerResponseHandler(org.opensearch.action.ActionListenerResponseHandler) ClusterService(org.opensearch.cluster.service.ClusterService) OriginSettingClient(org.opensearch.client.OriginSettingClient) GetRequest(org.opensearch.action.get.GetRequest) ResourceNotFoundException(org.opensearch.ResourceNotFoundException)

Example 27 with GetRequest

use of org.opensearch.action.get.GetRequest in project OpenSearch by opensearch-project.

the class TermsQueryBuilder method fetch.

private void fetch(TermsLookup termsLookup, Client client, ActionListener<List<Object>> actionListener) {
    GetRequest getRequest = new GetRequest(termsLookup.index(), termsLookup.id());
    getRequest.preference("_local").routing(termsLookup.routing());
    client.get(getRequest, ActionListener.delegateFailure(actionListener, (delegatedListener, getResponse) -> {
        List<Object> terms = new ArrayList<>();
        if (getResponse.isSourceEmpty() == false) {
            // extract terms only if the doc source exists
            List<Object> extractedValues = XContentMapValues.extractRawValues(termsLookup.path(), getResponse.getSourceAsMap());
            terms.addAll(extractedValues);
        }
        delegatedListener.onResponse(terms);
    }));
}
Also used : Query(org.apache.lucene.search.Query) IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) BytesReference(org.opensearch.common.bytes.BytesReference) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) AbstractList(java.util.AbstractList) StreamOutput(org.opensearch.common.io.stream.StreamOutput) Supplier(java.util.function.Supplier) Strings(org.opensearch.common.Strings) ArrayList(java.util.ArrayList) XContentParser(org.opensearch.common.xcontent.XContentParser) HashSet(java.util.HashSet) ActionListener(org.opensearch.action.ActionListener) ParsingException(org.opensearch.common.ParsingException) StreamInput(org.opensearch.common.io.stream.StreamInput) Client(org.opensearch.client.Client) SetOnce(org.apache.lucene.util.SetOnce) TermsLookup(org.opensearch.indices.TermsLookup) CharBuffer(java.nio.CharBuffer) BytesRef(org.apache.lucene.util.BytesRef) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) GetRequest(org.opensearch.action.get.GetRequest) Set(java.util.Set) IOException(java.io.IOException) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) ConstantFieldType(org.opensearch.index.mapper.ConstantFieldType) Collectors(java.util.stream.Collectors) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Objects(java.util.Objects) List(java.util.List) IndexSettings(org.opensearch.index.IndexSettings) XContentMapValues(org.opensearch.common.xcontent.support.XContentMapValues) Collections(java.util.Collections) GetRequest(org.opensearch.action.get.GetRequest) AbstractList(java.util.AbstractList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 28 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 29 with GetRequest

use of org.opensearch.action.get.GetRequest in project OpenSearch by opensearch-project.

the class RequestConvertersTests method getAndExistsTest.

private static void getAndExistsTest(Function<GetRequest, Request> requestConverter, String method) {
    String index = randomAlphaOfLengthBetween(3, 10);
    String id = randomAlphaOfLengthBetween(3, 10);
    GetRequest getRequest = new GetRequest(index, id);
    Map<String, String> expectedParams = new HashMap<>();
    if (randomBoolean()) {
        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");
            }
        }
        if (randomBoolean()) {
            long version = randomLong();
            getRequest.version(version);
            if (version != Versions.MATCH_ANY) {
                expectedParams.put("version", Long.toString(version));
            }
        }
        setRandomVersionType(getRequest::versionType, expectedParams);
        if (randomBoolean()) {
            int numStoredFields = randomIntBetween(1, 10);
            String[] storedFields = new String[numStoredFields];
            String storedFieldsParam = randomFields(storedFields);
            getRequest.storedFields(storedFields);
            expectedParams.put("stored_fields", storedFieldsParam);
        }
        if (randomBoolean()) {
            randomizeFetchSourceContextParams(getRequest::fetchSourceContext, expectedParams);
        }
    }
    Request request = requestConverter.apply(getRequest);
    assertEquals("/" + index + "/_doc/" + id, request.getEndpoint());
    assertEquals(expectedParams, request.getParameters());
    assertNull(request.getEntity());
    assertEquals(method, request.getMethod());
}
Also used : HashMap(java.util.HashMap) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) GetRequest(org.opensearch.action.get.GetRequest) 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)

Example 30 with GetRequest

use of org.opensearch.action.get.GetRequest in project OpenSearch by opensearch-project.

the class CrudIT method testExists.

public void testExists() throws IOException {
    {
        GetRequest getRequest = new GetRequest("index", "id");
        assertFalse(execute(getRequest, highLevelClient()::exists, highLevelClient()::existsAsync));
    }
    IndexRequest index = new IndexRequest("index").id("id");
    index.source("{\"field1\":\"value1\",\"field2\":\"value2\"}", XContentType.JSON);
    index.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
    highLevelClient().index(index, RequestOptions.DEFAULT);
    {
        GetRequest getRequest = new GetRequest("index", "id");
        assertTrue(execute(getRequest, highLevelClient()::exists, highLevelClient()::existsAsync));
    }
    {
        GetRequest getRequest = new GetRequest("index", "does_not_exist");
        assertFalse(execute(getRequest, highLevelClient()::exists, highLevelClient()::existsAsync));
    }
    {
        GetRequest getRequest = new GetRequest("index", "does_not_exist").version(1);
        assertFalse(execute(getRequest, highLevelClient()::exists, highLevelClient()::existsAsync));
    }
}
Also used : GetRequest(org.opensearch.action.get.GetRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest)

Aggregations

GetRequest (org.opensearch.action.get.GetRequest)81 GetResponse (org.opensearch.action.get.GetResponse)50 ActionListener (org.opensearch.action.ActionListener)48 IOException (java.io.IOException)34 Client (org.opensearch.client.Client)32 IndexRequest (org.opensearch.action.index.IndexRequest)25 XContentParser (org.opensearch.common.xcontent.XContentParser)24 SearchRequest (org.opensearch.action.search.SearchRequest)22 AnomalyDetector (org.opensearch.ad.model.AnomalyDetector)21 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)21 LogManager (org.apache.logging.log4j.LogManager)20 Logger (org.apache.logging.log4j.Logger)20 AnomalyDetectorJob (org.opensearch.ad.model.AnomalyDetectorJob)19 IndexNotFoundException (org.opensearch.index.IndexNotFoundException)19 List (java.util.List)18 XContentParserUtils.ensureExpectedToken (org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken)17 Optional (java.util.Optional)15 Set (java.util.Set)15 IntervalTimeConfiguration (org.opensearch.ad.model.IntervalTimeConfiguration)15 TransportService (org.opensearch.transport.TransportService)15