Search in sources :

Example 81 with IndexRequest

use of org.elasticsearch.action.index.IndexRequest in project elasticsearch-jdbc by jprante.

the class StandardSink method index.

@Override
public void index(IndexableObject object, boolean create) throws IOException {
    if (clientAPI == null) {
        return;
    }
    if (Strings.hasLength(object.index())) {
        setIndex(object.index());
    }
    if (Strings.hasLength(object.type())) {
        setType(object.type());
    }
    if (Strings.hasLength(object.id())) {
        setId(object.id());
    }
    IndexRequest request = Requests.indexRequest(this.index).type(this.type).id(getId()).source(object.build());
    if (object.meta(ControlKeys._version.name()) != null) {
        request.versionType(VersionType.EXTERNAL).version(Long.parseLong(object.meta(ControlKeys._version.name())));
    }
    if (object.meta(ControlKeys._routing.name()) != null) {
        request.routing(object.meta(ControlKeys._routing.name()));
    }
    if (object.meta(ControlKeys._parent.name()) != null) {
        request.parent(object.meta(ControlKeys._parent.name()));
    }
    if (object.meta(ControlKeys._timestamp.name()) != null) {
        request.timestamp(object.meta(ControlKeys._timestamp.name()));
    }
    if (object.meta(ControlKeys._ttl.name()) != null) {
        request.ttl(Long.parseLong(object.meta(ControlKeys._ttl.name())));
    }
    if (logger.isTraceEnabled()) {
        logger.trace("adding bulk index action {}", request.source().toUtf8());
    }
    clientAPI.bulkIndex(request);
}
Also used : IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 82 with IndexRequest

use of 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.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 84 with IndexRequest

use of 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 85 with IndexRequest

use of 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)

Aggregations

IndexRequest (org.elasticsearch.action.index.IndexRequest)177 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)36 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)34 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)33 IOException (java.io.IOException)28 Test (org.junit.Test)27 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)26 ElasticsearchException (org.elasticsearch.ElasticsearchException)20 IndexResponse (org.elasticsearch.action.index.IndexResponse)17 HashMap (java.util.HashMap)16 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)15 Map (java.util.Map)14 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)14 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)13 GetRequest (org.elasticsearch.action.get.GetRequest)13 BytesReference (org.elasticsearch.common.bytes.BytesReference)12 ArrayList (java.util.ArrayList)10 Matchers.anyBoolean (org.mockito.Matchers.anyBoolean)9 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)8 CreateIndexRequest (org.elasticsearch.client.indices.CreateIndexRequest)8