null


License

License

Categories

Categories

CLI User Interface Search Business Logic Libraries Elasticsearch
GroupId

GroupId

org.xbib.elasticsearch
ArtifactId

ArtifactId

elasticsearch-client-http
Last Version

Last Version

2.2.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

null
null
Project URL

Project URL

https://github.com/jprante/elasticsearch-client-http
Project Organization

Project Organization

xbib
Source Code Management

Source Code Management

https://github.com/jprante/elasticsearch-client-http

Download elasticsearch-client-http

How to add to project

<!-- https://jarcasting.com/artifacts/org.xbib.elasticsearch/elasticsearch-client-http/ -->
<dependency>
    <groupId>org.xbib.elasticsearch</groupId>
    <artifactId>elasticsearch-client-http</artifactId>
    <version>2.2.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.xbib.elasticsearch/elasticsearch-client-http/
implementation 'org.xbib.elasticsearch:elasticsearch-client-http:2.2.1.0'
// https://jarcasting.com/artifacts/org.xbib.elasticsearch/elasticsearch-client-http/
implementation ("org.xbib.elasticsearch:elasticsearch-client-http:2.2.1.0")
'org.xbib.elasticsearch:elasticsearch-client-http:jar:2.2.1.0'
<dependency org="org.xbib.elasticsearch" name="elasticsearch-client-http" rev="2.2.1.0">
  <artifact name="elasticsearch-client-http" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.xbib.elasticsearch', module='elasticsearch-client-http', version='2.2.1.0')
)
libraryDependencies += "org.xbib.elasticsearch" % "elasticsearch-client-http" % "2.2.1.0"
[org.xbib.elasticsearch/elasticsearch-client-http "2.2.1.0"]

Dependencies

compile (1)

Group / Artifact Type Version
org.elasticsearch : elasticsearch jar 2.2.1

test (5)

Group / Artifact Type Version
junit : junit jar 4.12
net.java.dev.jna : jna jar 4.1.0
com.github.spullara.mustache.java : compiler jar 0.8.13
org.apache.logging.log4j : log4j-slf4j-impl jar 2.7
org.apache.logging.log4j : log4j-core jar 2.7

Project Modules

There are no modules declared in this project.

Elasticsearch HTTP client

elasticsearch client http coverage badge License Apache%202.0 blue

This is a an implementation of a Java HTTP client for Elasticsearch. The client API is compatible to the Elasticsearch TransportClient API.

Here is an example

        try (HttpClient client = HttpClient.builder()
                .url(new URL("http://127.0.0.1:9200"))
                .build()) {
            CreateIndexRequestBuilder createIndexRequestBuilder =
                    new CreateIndexRequestBuilder(client, CreateIndexAction.INSTANCE).setIndex("test");
            createIndexRequestBuilder.execute().actionGet();
            IndexRequestBuilder indexRequestBuilder =
                    new IndexRequestBuilder(client, IndexAction.INSTANCE)
                            .setIndex("test")
                            .setType("type")
                            .setId("1")
                            .setSource(jsonBuilder().startObject().field("name", "Hello World").endObject());
            indexRequestBuilder.execute().actionGet();
            RefreshRequestBuilder refreshRequestBuilder =
                    new RefreshRequestBuilder(client, RefreshAction.INSTANCE)
                            .setIndices("test");
            refreshRequestBuilder.execute().actionGet();
            SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client, SearchAction.INSTANCE)
                    .setIndices("test")
                    .setQuery(QueryBuilders.matchAllQuery()).setSize(0);
            assertTrue(searchRequestBuilder.execute().actionGet().getHits().getTotalHits() > 0);
        }

There is also bulk support. Use the class HttpBulkProcessor which is compatible to Elasticsearch BulkProcessor.

Or you can use HttpBulkClient like this

int maxthreads = Runtime.getRuntime().availableProcessors();
        int maxactions = MAX_ACTIONS;
        final long maxloop = NUM_ACTIONS;
        logger.info("HttpBulkNodeClient max={} maxactions={} maxloop={}", maxthreads, maxactions, maxloop);
        final HttpBulkClient client = HttpBulkClient.builder()
                .url(new URL("http://127.0.0.1:9200"))
                .maxActionsPerRequest(maxactions)
                .flushIngestInterval(TimeValue.timeValueSeconds(60))
                .build();
        try {
            client.newIndex("test")
                    .startBulk("test", -1);
            ThreadPoolExecutor pool = EsExecutors.newFixed("http-bulk-nodeclient-test", maxthreads, 30,
                    EsExecutors.daemonThreadFactory("http-bulk-nodeclient-test"));
            final CountDownLatch latch = new CountDownLatch(maxthreads);
            for (int i = 0; i < maxthreads; i++) {
                pool.execute(() -> {
                    for (int j = 0; j < maxloop; j++) {
                        client.index("test", "test", null, "{ \"name\" : \"" + randomString(32) + "\"}");
                    }
                    latch.countDown();
                });
            }
            logger.info("waiting for max 30 seconds...");
            latch.await(30, TimeUnit.SECONDS);
            logger.info("flush...");
            client.flushIngest();
            client.waitForResponses(TimeValue.timeValueSeconds(30));
            logger.info("got all responses, thread pool shutdown...");
            pool.shutdown();
            logger.info("pool is shut down");
            client.stopBulk("test", 1000);
            if (client.hasThrowable()) {
                logger.error("error", client.getThrowable());
            }
            assertFalse(client.hasThrowable());
            client.refreshIndex("test");
            SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client.client(), SearchAction.INSTANCE)
                    .setIndices("test")
                    .setQuery(QueryBuilders.matchAllQuery()).setSize(0);
            assertEquals(maxthreads * maxloop,
                    searchRequestBuilder.execute().actionGet().getHits().getTotalHits());
        } catch (NoNodeAvailableException e) {
            logger.warn("skipping, no node available");
        } finally {
            client.shutdown();
        }

The HTTP client is not complete yet. Many Elasticsearch actions are missing.

You are welcome to send pull requests at https://github.com/jprante/elasticsearch-client-http if you like to improve this project.

Versions

Version
2.2.1.0