Search in sources :

Example 1 with GetRequest

use of org.elasticsearch.action.get.GetRequest in project elasticsearch by elastic.

the class GeoShapeQueryBuilder method doRewrite.

@Override
protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) throws IOException {
    if (this.shape == null) {
        GetRequest getRequest = new GetRequest(indexedShapeIndex, indexedShapeType, indexedShapeId);
        ShapeBuilder shape = fetch(queryShardContext.getClient(), getRequest, indexedShapePath);
        return new GeoShapeQueryBuilder(this.fieldName, shape).relation(relation).strategy(strategy);
    }
    return this;
}
Also used : ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) GetRequest(org.elasticsearch.action.get.GetRequest)

Example 2 with GetRequest

use of org.elasticsearch.action.get.GetRequest in project elasticsearch by elastic.

the class RestGetAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final GetRequest getRequest = new GetRequest(request.param("index"), request.param("type"), request.param("id"));
    getRequest.operationThreaded(true);
    getRequest.refresh(request.paramAsBoolean("refresh", getRequest.refresh()));
    getRequest.routing(request.param("routing"));
    getRequest.parent(request.param("parent"));
    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 : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GetResponse(org.elasticsearch.action.get.GetResponse) GetRequest(org.elasticsearch.action.get.GetRequest) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestToXContentListener(org.elasticsearch.rest.action.RestToXContentListener) IOException(java.io.IOException) NOT_FOUND(org.elasticsearch.rest.RestStatus.NOT_FOUND) RestController(org.elasticsearch.rest.RestController) VersionType(org.elasticsearch.index.VersionType) Strings(org.elasticsearch.common.Strings) RestActions(org.elasticsearch.rest.action.RestActions) Settings(org.elasticsearch.common.settings.Settings) RestStatus(org.elasticsearch.rest.RestStatus) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) NodeClient(org.elasticsearch.client.node.NodeClient) HEAD(org.elasticsearch.rest.RestRequest.Method.HEAD) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) RestStatus(org.elasticsearch.rest.RestStatus) GetRequest(org.elasticsearch.action.get.GetRequest) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 3 with GetRequest

use of org.elasticsearch.action.get.GetRequest in project elasticsearch by elastic.

the class PercolateQueryBuilder method doRewrite.

@Override
protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) throws IOException {
    if (document != null) {
        return this;
    }
    GetRequest getRequest = new GetRequest(indexedDocumentIndex, indexedDocumentType, indexedDocumentId);
    getRequest.preference("_local");
    getRequest.routing(indexedDocumentRouting);
    getRequest.preference(indexedDocumentPreference);
    if (indexedDocumentVersion != null) {
        getRequest.version(indexedDocumentVersion);
    }
    GetResponse getResponse = queryShardContext.getClient().get(getRequest).actionGet();
    if (getResponse.isExists() == false) {
        throw new ResourceNotFoundException("indexed document [{}/{}/{}] couldn't be found", indexedDocumentIndex, indexedDocumentType, indexedDocumentId);
    }
    if (getResponse.isSourceEmpty()) {
        throw new IllegalArgumentException("indexed document [" + indexedDocumentIndex + "/" + indexedDocumentType + "/" + indexedDocumentId + "] source disabled");
    }
    final BytesReference source = getResponse.getSourceAsBytesRef();
    return new PercolateQueryBuilder(field, documentType, source, XContentFactory.xContentType(source));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) GetRequest(org.elasticsearch.action.get.GetRequest) ResourceNotFoundException(org.elasticsearch.ResourceNotFoundException) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 4 with GetRequest

use of org.elasticsearch.action.get.GetRequest in project elasticsearch by elastic.

the class CrudIT method testGet.

public void testGet() throws IOException {
    {
        GetRequest getRequest = new GetRequest("index", "type", "id");
        ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync));
        assertEquals(RestStatus.NOT_FOUND, exception.status());
        assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index]", exception.getMessage());
        assertEquals("index", exception.getMetadata("es.index").get(0));
    }
    String document = "{\"field1\":\"value1\",\"field2\":\"value2\"}";
    StringEntity stringEntity = new StringEntity(document, ContentType.APPLICATION_JSON);
    Response response = client().performRequest("PUT", "/index/type/id", Collections.singletonMap("refresh", "wait_for"), stringEntity);
    assertEquals(201, response.getStatusLine().getStatusCode());
    {
        GetRequest getRequest = new GetRequest("index", "type", "id").version(2);
        ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync));
        assertEquals(RestStatus.CONFLICT, exception.status());
        assertEquals("Elasticsearch exception [type=version_conflict_engine_exception, " + "reason=[type][id]: " + "version conflict, current version [1] is different than the one provided [2]]", exception.getMessage());
        assertEquals("index", exception.getMetadata("es.index").get(0));
    }
    {
        GetRequest getRequest = new GetRequest("index", "type", "id");
        if (randomBoolean()) {
            getRequest.version(1L);
        }
        GetResponse getResponse = execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync);
        assertEquals("index", getResponse.getIndex());
        assertEquals("type", getResponse.getType());
        assertEquals("id", getResponse.getId());
        assertTrue(getResponse.isExists());
        assertFalse(getResponse.isSourceEmpty());
        assertEquals(1L, getResponse.getVersion());
        assertEquals(document, getResponse.getSourceAsString());
    }
    {
        GetRequest getRequest = new GetRequest("index", "type", "does_not_exist");
        GetResponse getResponse = execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync);
        assertEquals("index", getResponse.getIndex());
        assertEquals("type", getResponse.getType());
        assertEquals("does_not_exist", getResponse.getId());
        assertFalse(getResponse.isExists());
        assertEquals(-1, getResponse.getVersion());
        assertTrue(getResponse.isSourceEmpty());
        assertNull(getResponse.getSourceAsString());
    }
    {
        GetRequest getRequest = new GetRequest("index", "type", "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("type", getResponse.getType());
        assertEquals("id", getResponse.getId());
        assertTrue(getResponse.isExists());
        assertTrue(getResponse.isSourceEmpty());
        assertEquals(1L, getResponse.getVersion());
        assertNull(getResponse.getSourceAsString());
    }
    {
        GetRequest getRequest = new GetRequest("index", "type", "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("type", getResponse.getType());
        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 : GetResponse(org.elasticsearch.action.get.GetResponse) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) DocWriteResponse(org.elasticsearch.action.DocWriteResponse) StringEntity(org.apache.http.entity.StringEntity) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) GetRequest(org.elasticsearch.action.get.GetRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException) GetResponse(org.elasticsearch.action.get.GetResponse) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap)

Example 5 with GetRequest

use of org.elasticsearch.action.get.GetRequest in project graylog2-server by Graylog2.

the class Messages method get.

public ResultMessage get(String messageId, String index) throws DocumentNotFoundException {
    final GetRequest request = c.prepareGet(index, IndexMapping.TYPE_MESSAGE, messageId).request();
    final GetResponse r = c.get(request).actionGet();
    if (!r.isExists()) {
        throw new DocumentNotFoundException(index, messageId);
    }
    return ResultMessage.parseFromSource(r);
}
Also used : GetRequest(org.elasticsearch.action.get.GetRequest) GetResponse(org.elasticsearch.action.get.GetResponse)

Aggregations

GetRequest (org.elasticsearch.action.get.GetRequest)26 GetResponse (org.elasticsearch.action.get.GetResponse)15 IndexRequest (org.elasticsearch.action.index.IndexRequest)9 IOException (java.io.IOException)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)4 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)4 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)4 FetchSourceContext (org.elasticsearch.search.fetch.subphase.FetchSourceContext)4 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)3 MultiGetRequest (org.elasticsearch.action.get.MultiGetRequest)3 IndexResponse (org.elasticsearch.action.index.IndexResponse)3 SearchRequest (org.elasticsearch.action.search.SearchRequest)3 Settings (org.elasticsearch.common.settings.Settings)3 List (java.util.List)2 Message (org.apache.camel.Message)2 GoraException (org.apache.gora.util.GoraException)2 StringEntity (org.apache.http.entity.StringEntity)2