Search in sources :

Example 76 with BulkRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest in project jmix by jmix-framework.

the class EntityIndexingTest method indexLongPk.

@Test
@DisplayName("Indexing of entity with Long primary key")
public void indexLongPk() {
    TestLongPkEntity entity = metadata.create(TestLongPkEntity.class);
    entity.setName("Long PK entity");
    dataManager.save(entity);
    JsonNode jsonNode = TestJsonUtils.readJsonFromFile("indexing/test_content_long_pk");
    TestBulkRequestIndexActionValidationData expectedIndexAction = new TestBulkRequestIndexActionValidationData("search_index_test_longpkentity", idSerialization.idToString(Id.of(entity)), jsonNode);
    TestBulkRequestValidationData expectedData = new TestBulkRequestValidationData(Collections.singletonList(expectedIndexAction), Collections.emptyList());
    entityIndexer.index(entity);
    List<BulkRequest> bulkRequests = bulkRequestsTracker.getBulkRequests();
    TestBulkRequestValidationResult result = TestBulkRequestValidator.validate(Collections.singletonList(expectedData), bulkRequests);
    Assert.assertFalse(result.toString(), result.hasFailures());
}
Also used : BulkRequest(org.elasticsearch.action.bulk.BulkRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 77 with BulkRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest in project elasticsearch-suggest-plugin by spinscale.

the class AbstractSuggestTest method indexProducts.

private void indexProducts(List<Map<String, Object>> products, String index, String routing) throws Exception {
    long currentCount = getCurrentDocumentCount(index);
    BulkRequest bulkRequest = new BulkRequest();
    for (Map<String, Object> product : products) {
        IndexRequest indexRequest = new IndexRequest(index, "product", (String) product.get("ProductId"));
        indexRequest.source(product);
        if (Strings.hasLength(routing)) {
            indexRequest.routing(routing);
        }
        bulkRequest.add(indexRequest);
    }
    bulkRequest.refresh(true);
    BulkResponse response = client().bulk(bulkRequest).actionGet();
    if (response.hasFailures()) {
        fail("Error in creating products: " + response.buildFailureMessage());
    }
    assertDocumentCountAfterIndexing(index, products.size() + currentCount);
}
Also used : BulkRequest(org.elasticsearch.action.bulk.BulkRequest) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 78 with BulkRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest in project hazelcast by hazelcast.

the class ElasticSinkBuilderTest method when_writeToFailingSink_then_shouldCloseClient.

@Test
public void when_writeToFailingSink_then_shouldCloseClient() throws IOException {
    ClientHolder.elasticClients.clear();
    Sink<String> elasticSink = new ElasticSinkBuilder<>().clientFn(() -> {
        RestClientBuilder builder = spy(RestClient.builder(HttpHost.create("localhost:9200")));
        when(builder.build()).thenAnswer(invocation -> {
            Object result = invocation.callRealMethod();
            RestClient client = (RestClient) spy(result);
            ClientHolder.elasticClients.add(client);
            return client;
        });
        return builder;
    }).bulkRequestFn(() -> new BulkRequest().setRefreshPolicy(RefreshPolicy.IMMEDIATE)).mapToRequestFn((String item) -> new IndexRequest("my-index").source(Collections.emptyMap())).retries(0).build();
    p.readFrom(TestSources.items("a", "b", "c")).writeTo(elasticSink);
    try {
        execute();
    } catch (Exception e) {
    // ignore - elastic is not running
    }
    for (RestClient client : ClientHolder.elasticClients) {
        verify(client).close();
    }
}
Also used : BulkRequest(org.elasticsearch.action.bulk.BulkRequest) RestClient(org.elasticsearch.client.RestClient) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) IndexRequest(org.elasticsearch.action.index.IndexRequest) IOException(java.io.IOException) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 79 with BulkRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest in project hazelcast by hazelcast.

the class LocalElasticSinkTest method when_writeToSink_then_shouldCloseClient.

@Test
public void when_writeToSink_then_shouldCloseClient() throws IOException {
    ClientHolder.elasticClients.clear();
    Sink<String> elasticSink = new ElasticSinkBuilder<>().clientFn(() -> {
        RestClientBuilder builder = spy(RestClient.builder(HttpHost.create(ElasticSupport.elastic.get().getHttpHostAddress())));
        when(builder.build()).thenAnswer(invocation -> {
            Object result = invocation.callRealMethod();
            RestClient client = (RestClient) spy(result);
            ClientHolder.elasticClients.add(client);
            return client;
        });
        return builder;
    }).bulkRequestFn(() -> new BulkRequest().setRefreshPolicy(RefreshPolicy.IMMEDIATE)).mapToRequestFn((String item) -> new IndexRequest("my-index").source(Collections.emptyMap())).build();
    Pipeline p = Pipeline.create();
    p.readFrom(TestSources.items("a", "b", "c")).writeTo(elasticSink);
    hz.getJet().newJob(p).join();
    for (RestClient client : ClientHolder.elasticClients) {
        verify(client).close();
    }
}
Also used : RestClient(org.elasticsearch.client.RestClient) HazelcastInstance(com.hazelcast.core.HazelcastInstance) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Test(org.junit.Test) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Mockito.spy(org.mockito.Mockito.spy) Mockito.verify(org.mockito.Mockito.verify) TestSources(com.hazelcast.jet.pipeline.test.TestSources) IndexRequest(org.elasticsearch.action.index.IndexRequest) ClientHolder(com.hazelcast.jet.elastic.ElasticSinkBuilderTest.ClientHolder) After(org.junit.After) TestHazelcastFactory(com.hazelcast.client.test.TestHazelcastFactory) HttpHost(org.apache.http.HttpHost) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) RefreshPolicy(org.elasticsearch.action.support.WriteRequest.RefreshPolicy) Collections(java.util.Collections) Sink(com.hazelcast.jet.pipeline.Sink) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) RestClient(org.elasticsearch.client.RestClient) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) IndexRequest(org.elasticsearch.action.index.IndexRequest) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Test(org.junit.Test)

Example 80 with BulkRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest in project hazelcast by hazelcast.

the class AuthElasticSinksTest method given_clientWithoutAuthentication_whenWriteToElasticSink_thenFailWithAuthenticationException.

@Test
public void given_clientWithoutAuthentication_whenWriteToElasticSink_thenFailWithAuthenticationException() {
    ElasticsearchContainer container = ElasticSupport.secureElastic.get();
    String containerIp = container.getContainerIpAddress();
    Integer port = container.getMappedPort(PORT);
    Sink<TestItem> elasticSink = new ElasticSinkBuilder<>().clientFn(() -> client(containerIp, port)).bulkRequestFn(() -> new BulkRequest().setRefreshPolicy(RefreshPolicy.IMMEDIATE)).mapToRequestFn((TestItem item) -> new IndexRequest("my-index").source(item.asMap())).retries(0).build();
    Pipeline p = Pipeline.create();
    p.readFrom(TestSources.items(new TestItem("id", "Frantisek"))).writeTo(elasticSink);
    assertThatThrownBy(() -> submitJob(p)).hasRootCauseInstanceOf(ElasticsearchStatusException.class).hasStackTraceContaining("missing authentication credentials");
}
Also used : BulkRequest(org.elasticsearch.action.bulk.BulkRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) ElasticsearchContainer(org.testcontainers.elasticsearch.ElasticsearchContainer) TestItem(com.hazelcast.jet.elastic.CommonElasticSinksTest.TestItem) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Test(org.junit.Test)

Aggregations

BulkRequest (org.elasticsearch.action.bulk.BulkRequest)158 IndexRequest (org.elasticsearch.action.index.IndexRequest)77 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)73 IOException (java.io.IOException)47 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)40 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)28 RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)27 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)25 ArrayList (java.util.ArrayList)24 List (java.util.List)18 SearchRequest (org.elasticsearch.action.search.SearchRequest)17 Test (org.junit.jupiter.api.Test)17 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)16 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)16 Map (java.util.Map)15 Test (org.junit.Test)15 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)14 JsonNode (com.fasterxml.jackson.databind.JsonNode)14 BulkProcessor (org.elasticsearch.action.bulk.BulkProcessor)13 ActionListener (org.elasticsearch.action.ActionListener)12