Search in sources :

Example 11 with GetRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetRequest in project spring-boot by spring-projects.

the class ElasticsearchRestClientAutoConfigurationIntegrationTests method restClientCanQueryElasticsearchNode.

@Test
@SuppressWarnings("deprecation")
void restClientCanQueryElasticsearchNode() {
    this.contextRunner.withPropertyValues("spring.elasticsearch.uris=" + elasticsearch.getHttpHostAddress(), "spring.elasticsearch.connection-timeout=120s", "spring.elasticsearch.socket-timeout=120s").run((context) -> {
        org.elasticsearch.client.RestHighLevelClient client = context.getBean(org.elasticsearch.client.RestHighLevelClient.class);
        Map<String, String> source = new HashMap<>();
        source.put("a", "alpha");
        source.put("b", "bravo");
        IndexRequest index = new IndexRequest("test").id("1").source(source);
        client.index(index, RequestOptions.DEFAULT);
        GetRequest getRequest = new GetRequest("test").id("1");
        assertThat(client.get(getRequest, RequestOptions.DEFAULT).isExists()).isTrue();
    });
}
Also used : HashMap(java.util.HashMap) GetRequest(org.elasticsearch.action.get.GetRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) Test(org.junit.jupiter.api.Test)

Example 12 with GetRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetRequest in project gora by apache.

the class ElasticsearchStore method get.

@Override
public T get(K key, String[] fields) throws GoraException {
    String[] requestedFields = getFieldsToQuery(fields);
    List<String> documentFields = new ArrayList<>();
    for (String requestedField : requestedFields) {
        documentFields.add(elasticsearchMapping.getFields().get(requestedField).getName());
    }
    try {
        // Prepare the Elasticsearch request
        GetRequest getRequest = new GetRequest(elasticsearchMapping.getIndexName(), (String) key);
        GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
        if (getResponse.isExists()) {
            Map<String, Object> sourceMap = getResponse.getSourceAsMap();
            // Map of field's name and its value from the Document
            Map<String, Object> fieldsAndValues = new HashMap<>();
            for (String field : documentFields) {
                fieldsAndValues.put(field, sourceMap.get(field));
            }
            // Build the corresponding persistent
            return newInstance(fieldsAndValues, requestedFields);
        } else {
            return null;
        }
    } catch (IOException ex) {
        throw new GoraException(ex);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) HashMap(java.util.HashMap) GetRequest(org.elasticsearch.action.get.GetRequest) ArrayList(java.util.ArrayList) IOException(java.io.IOException) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 13 with GetRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetRequest in project sonarqube by SonarSource.

the class EsRequestDetailsTest method should_format_GetRequest.

@Test
public void should_format_GetRequest() {
    GetRequest request = new GetRequest().index("index-1").type("type-1").id("id-1");
    assertThat(EsRequestDetails.computeDetailsAsString(request)).isEqualTo("ES get request for key 'id-1' on index 'index-1' on type 'type-1'");
}
Also used : GetRequest(org.elasticsearch.action.get.GetRequest) Test(org.junit.Test)

Example 14 with GetRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetRequest in project datashare by ICIJ.

the class ElasticsearchIndexer method get.

@Override
public <T extends Entity> T get(String indexName, String id, String root) {
    String type = null;
    try {
        final GetRequest req = new GetRequest(indexName, id).routing(root);
        final GetResponse resp = client.get(req, RequestOptions.DEFAULT);
        if (resp.isExists()) {
            Map<String, Object> sourceAsMap = resp.getSourceAsMap();
            sourceAsMap.put("rootDocument", ofNullable(resp.getFields().get("_routing")).orElse(new DocumentField("_routing", Collections.singletonList(id))).getValues().get(0));
            type = (String) sourceAsMap.get(esCfg.docTypeField);
            Class<T> tClass = (Class<T>) Class.forName("org.icij.datashare.text." + type);
            return JsonObjectMapper.getObject(id, sourceAsMap, tClass);
        }
    } catch (IOException e) {
        LOGGER.error("Failed to get entity " + id + " in index " + indexName, e);
    } catch (ClassNotFoundException e) {
        LOGGER.error("no entity for type " + type);
    }
    return null;
}
Also used : DocumentField(org.elasticsearch.common.document.DocumentField) GetRequest(org.elasticsearch.action.get.GetRequest) IOException(java.io.IOException) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 15 with GetRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetRequest in project datashare by ICIJ.

the class ElasticsearchSpewer method isDuplicate.

private boolean isDuplicate(String docId) throws IOException {
    GetRequest getRequest = new GetRequest(indexName, docId);
    getRequest.fetchSourceContext(new FetchSourceContext(false));
    getRequest.storedFields("_none_");
    return client.exists(getRequest, RequestOptions.DEFAULT);
}
Also used : FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) GetRequest(org.elasticsearch.action.get.GetRequest)

Aggregations

GetRequest (org.elasticsearch.action.get.GetRequest)45 GetResponse (org.elasticsearch.action.get.GetResponse)29 Test (org.junit.Test)14 IOException (java.io.IOException)13 IndexRequest (org.elasticsearch.action.index.IndexRequest)9 HashMap (java.util.HashMap)7 TikaDocument (org.icij.extract.document.TikaDocument)7 FetchSourceContext (org.elasticsearch.search.fetch.subphase.FetchSourceContext)6 ArrayList (java.util.ArrayList)5 DocumentFactory (org.icij.extract.document.DocumentFactory)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ParsingReader (org.apache.tika.parser.ParsingReader)4 ElasticsearchException (org.elasticsearch.ElasticsearchException)4 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)4 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)4 SearchRequest (org.elasticsearch.action.search.SearchRequest)4 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)4 PathIdentifier (org.icij.extract.document.PathIdentifier)4 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)3 MultiGetRequest (org.elasticsearch.action.get.MultiGetRequest)3