Search in sources :

Example 21 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 22 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 23 with GetRequest

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

the class RestHighLevelClientEmployeeResource method getEmployeeById.

@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response getEmployeeById(@PathParam("id") final Long id) throws IOException {
    Objects.requireNonNull(id);
    final GetResponse response = client.get(new GetRequest("megacorp", Long.toString(id)), RequestOptions.DEFAULT);
    if (response.isExists()) {
        final Map<String, Object> source = response.getSource();
        final Employee employee = new Employee();
        employee.setFirstName((String) source.get("first_name"));
        employee.setLastName((String) source.get("last_name"));
        employee.setAge((Integer) source.get("age"));
        employee.setAbout((String) source.get("about"));
        @SuppressWarnings("unchecked") final List<String> interests = (List<String>) source.get("interests");
        employee.setInterests(interests);
        return Response.ok(employee).build();
    } else {
        return Response.status(Response.Status.NOT_FOUND).build();
    }
}
Also used : Employee(org.opensearch.wildfly.model.Employee) GetRequest(org.opensearch.action.get.GetRequest) List(java.util.List) GetResponse(org.opensearch.action.get.GetResponse) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 24 with GetRequest

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

the class RestGetAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    GetRequest getRequest = new GetRequest(request.param("index"), request.param("id"));
    getRequest.refresh(request.paramAsBoolean("refresh", getRequest.refresh()));
    getRequest.routing(request.param("routing"));
    getRequest.preference(request.param("preference"));
    getRequest.realtime(request.paramAsBoolean("realtime", getRequest.realtime()));
    if (request.param("fields") != null) {
        throw new IllegalArgumentException("the parameter [fields] is no longer supported, " + "please use [stored_fields] to retrieve stored fields or [_source] to load the field from _source");
    }
    final String fieldsParam = request.param("stored_fields");
    if (fieldsParam != null) {
        final String[] fields = Strings.splitStringByCommaToArray(fieldsParam);
        if (fields != null) {
            getRequest.storedFields(fields);
        }
    }
    getRequest.version(RestActions.parseVersion(request));
    getRequest.versionType(VersionType.fromString(request.param("version_type"), getRequest.versionType()));
    getRequest.fetchSourceContext(FetchSourceContext.parseFromRestRequest(request));
    return channel -> client.get(getRequest, new RestToXContentListener<GetResponse>(channel) {

        @Override
        protected RestStatus getStatus(final GetResponse response) {
            return response.isExists() ? OK : NOT_FOUND;
        }
    });
}
Also used : OK(org.opensearch.rest.RestStatus.OK) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) GetRequest(org.opensearch.action.get.GetRequest) IOException(java.io.IOException) RestStatus(org.opensearch.rest.RestStatus) Strings(org.opensearch.common.Strings) NOT_FOUND(org.opensearch.rest.RestStatus.NOT_FOUND) VersionType(org.opensearch.index.VersionType) List(java.util.List) RestActions(org.opensearch.rest.action.RestActions) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) Arrays.asList(java.util.Arrays.asList) BaseRestHandler(org.opensearch.rest.BaseRestHandler) HEAD(org.opensearch.rest.RestRequest.Method.HEAD) GetResponse(org.opensearch.action.get.GetResponse) FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) RestStatus(org.opensearch.rest.RestStatus) GetRequest(org.opensearch.action.get.GetRequest) GetResponse(org.opensearch.action.get.GetResponse)

Example 25 with GetRequest

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

the class IndicesRequestIT method testGet.

public void testGet() {
    String getShardAction = GetAction.NAME + "[s]";
    interceptTransportActions(getShardAction);
    GetRequest getRequest = new GetRequest(randomIndexOrAlias(), "id");
    internalCluster().coordOnlyNodeClient().get(getRequest).actionGet();
    clearInterceptedActions();
    assertSameIndices(getRequest, getShardAction);
}
Also used : GetRequest(org.opensearch.action.get.GetRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest)

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