Search in sources :

Example 81 with IndexRequest

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

the class ReactiveElasticsearchRestClientAutoConfigurationIntegrationTests method restClientCanQueryElasticsearchNode.

@Test
void restClientCanQueryElasticsearchNode() {
    this.contextRunner.withPropertyValues("spring.elasticsearch.uris=" + elasticsearch.getHttpHostAddress(), "spring.elasticsearch.connection-timeout=120s", "spring.elasticsearch.socket-timeout=120s").run((context) -> {
        ReactiveElasticsearchClient client = context.getBean(ReactiveElasticsearchClient.class);
        Map<String, String> source = new HashMap<>();
        source.put("a", "alpha");
        source.put("b", "bravo");
        IndexRequest indexRequest = new IndexRequest("foo").id("1").source(source);
        GetRequest getRequest = new GetRequest("foo").id("1");
        GetResult getResult = client.index(indexRequest).then(client.get(getRequest)).block();
        assertThat(getResult).isNotNull();
        assertThat(getResult.isExists()).isTrue();
    });
}
Also used : ReactiveElasticsearchClient(org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient) GetResult(org.elasticsearch.index.get.GetResult) HashMap(java.util.HashMap) GetRequest(org.elasticsearch.action.get.GetRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) Test(org.junit.jupiter.api.Test)

Example 82 with IndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest 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 83 with IndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest in project graylog2-server by Graylog2.

the class FixtureImporterES7 method importNode.

private void importNode(JsonNode root) throws IOException {
    /* This supports the nosqlunit DataSet structure:
         *
         *  {
         *    "documents": [
         *      {
         *        "document": [
         *          {
         *            "index": {
         *              "indexName": "graylog_0",
         *              "indexId": "0"
         *            }
         *          },
         *          {
         *            "data": {
         *              "source": "example.org",
         *              "message": "Hi",
         *              "timestamp": "2015-01-01 01:00:00.000"
         *            }
         *          }
         *        ]
         *      }
         *    ]
         *  }
         */
    final BulkRequest bulkRequest = new BulkRequest();
    final Set<String> targetIndices = new HashSet<>();
    for (final JsonNode document : root.path("documents")) {
        final List<JsonNode> indexes = new ArrayList<>();
        Map<String, Object> data = new HashMap<>();
        for (JsonNode entry : document.path("document")) {
            if (entry.hasNonNull("index")) {
                indexes.add(entry.path("index"));
            } else if (entry.hasNonNull("data")) {
                data = OBJECT_MAPPER.convertValue(entry.path("data"), TypeReferences.MAP_STRING_OBJECT);
            }
        }
        for (final JsonNode index : indexes) {
            final IndexRequest indexRequest = new IndexRequest().source(data);
            final String indexName = index.path("indexName").asText(null);
            if (indexName == null) {
                throw new IllegalArgumentException("Missing indexName in " + index);
            }
            targetIndices.add(indexName);
            indexRequest.index(indexName);
            if (index.hasNonNull("indexId")) {
                indexRequest.id(index.path("indexId").asText());
            }
            bulkRequest.add(indexRequest);
        }
    }
    for (String indexName : targetIndices) {
        if (!indexExists(indexName)) {
            createIndex(indexName);
        }
    }
    final BulkResponse result = client.execute((c, requestOptions) -> c.bulk(bulkRequest, requestOptions), "Unable to import fixtures.");
    if (result.hasFailures()) {
        throw new IllegalStateException("Error while bulk indexing documents: " + result.buildFailureMessage());
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) BulkResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkResponse) GetIndexRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.GetIndexRequest) IndexRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest) CreateIndexRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.CreateIndexRequest) BulkRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest) HashSet(java.util.HashSet)

Example 84 with IndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest in project graylog2-server by Graylog2.

the class MessagesES7IT method indexMessage.

@Override
protected boolean indexMessage(String index, Map<String, Object> source, String id) {
    final IndexRequest indexRequest = new IndexRequest(index).source(source).id(id);
    final IndexResponse result = this.elasticsearch.elasticsearchClient().execute((c, requestOptions) -> c.index(indexRequest, requestOptions));
    return result.status().equals(RestStatus.CREATED);
}
Also used : IndexResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexResponse) IndexRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest)

Example 85 with IndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest in project titan by thinkaurelius.

the class ElasticSearchIndex method mutate.

@Override
public void mutate(Map<String, Map<String, IndexMutation>> mutations, KeyInformation.IndexRetriever informations, TransactionHandle tx) throws StorageException {
    BulkRequestBuilder brb = client.prepareBulk();
    int bulkrequests = 0;
    try {
        for (Map.Entry<String, Map<String, IndexMutation>> stores : mutations.entrySet()) {
            String storename = stores.getKey();
            for (Map.Entry<String, IndexMutation> entry : stores.getValue().entrySet()) {
                String docid = entry.getKey();
                IndexMutation mutation = entry.getValue();
                Preconditions.checkArgument(!(mutation.isNew() && mutation.isDeleted()));
                Preconditions.checkArgument(!mutation.isNew() || !mutation.hasDeletions());
                Preconditions.checkArgument(!mutation.isDeleted() || !mutation.hasAdditions());
                // Deletions first
                if (mutation.hasDeletions()) {
                    if (mutation.isDeleted()) {
                        log.trace("Deleting entire document {}", docid);
                        brb.add(new DeleteRequest(indexName, storename, docid));
                        bulkrequests++;
                    } else {
                        Set<String> deletions = Sets.newHashSet(mutation.getDeletions());
                        if (mutation.hasAdditions()) {
                            for (IndexEntry ie : mutation.getAdditions()) {
                                deletions.remove(ie.key);
                            }
                        }
                        if (!deletions.isEmpty()) {
                            // TODO make part of batch mutation if/when possible
                            StringBuilder script = new StringBuilder();
                            for (String key : deletions) {
                                script.append("ctx._source.remove(\"" + key + "\"); ");
                            }
                            log.trace("Deleting individual fields [{}] for document {}", deletions, docid);
                            client.prepareUpdate(indexName, storename, docid).setScript(script.toString()).execute().actionGet();
                        }
                    }
                }
                if (mutation.hasAdditions()) {
                    if (mutation.isNew()) {
                        // Index
                        log.trace("Adding entire document {}", docid);
                        brb.add(new IndexRequest(indexName, storename, docid).source(getContent(mutation.getAdditions())));
                        bulkrequests++;
                    } else {
                        // Update: TODO make part of batch mutation if/when possible
                        boolean needUpsert = !mutation.hasDeletions();
                        XContentBuilder builder = getContent(mutation.getAdditions());
                        UpdateRequestBuilder update = client.prepareUpdate(indexName, storename, docid).setDoc(builder);
                        if (needUpsert)
                            update.setUpsert(builder);
                        log.trace("Updating document {} with upsert {}", docid, needUpsert);
                        update.execute().actionGet();
                    }
                }
            }
        }
        if (bulkrequests > 0)
            brb.execute().actionGet();
    } catch (Exception e) {
        throw convert(e);
    }
}
Also used : UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) ElasticSearchInterruptedException(org.elasticsearch.ElasticSearchInterruptedException) PermanentStorageException(com.thinkaurelius.titan.diskstorage.PermanentStorageException) TitanException(com.thinkaurelius.titan.core.TitanException) TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException) IndexMissingException(org.elasticsearch.indices.IndexMissingException) StorageException(com.thinkaurelius.titan.diskstorage.StorageException) IOException(java.io.IOException) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) Map(java.util.Map) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

IndexRequest (org.elasticsearch.action.index.IndexRequest)187 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)37 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)37 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)34 IOException (java.io.IOException)30 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)27 Test (org.junit.Test)27 ElasticsearchException (org.elasticsearch.ElasticsearchException)21 IndexResponse (org.elasticsearch.action.index.IndexResponse)20 HashMap (java.util.HashMap)17 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)17 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)17 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)17 Map (java.util.Map)15 GetRequest (org.elasticsearch.action.get.GetRequest)14 CreateIndexRequest (org.elasticsearch.client.indices.CreateIndexRequest)12 BytesReference (org.elasticsearch.common.bytes.BytesReference)12 ArrayList (java.util.ArrayList)11 CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)10 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)9