Skip to content

Instantly share code, notes, and snippets.

@thesurlydev
Created November 15, 2015 17:32
Show Gist options
  • Save thesurlydev/ce2bb7a46714825ae3fa to your computer and use it in GitHub Desktop.
Save thesurlydev/ce2bb7a46714825ae3fa to your computer and use it in GitHub Desktop.
example client targeted for ES 1.5.2 running locally
package com.foo.service.elasticsearch;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequestBuilder;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.bulk.BulkProcessor;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.collect.ImmutableMap;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* Created by switbe on 11/11/15.
*/
public class LocalElasticsearch {
private static final Logger logger = LoggerFactory.getLogger(LocalElasticsearch.class);
private final Client client;
public LocalElasticsearch(Client client) {
this.client = client;
}
public static void main(String[] args) throws Exception {
String clusterName = "elasticsearch";
String indexName = "test-index";
String type = "test-type";
Client client = getClient(clusterName);
ClusterHealthRequestBuilder healthRequestBuilder = client.admin().cluster()
.prepareHealth(indexName)
.setWaitForYellowStatus();
ClusterHealthResponse healthResponse = healthRequestBuilder.execute().actionGet();
logger.info("index status: " + healthResponse.getStatus());
LocalElasticsearch esClient = new LocalElasticsearch(client)
.deleteIndex(indexName)
.createIndex(indexName);
List<IndexRequest> indexRequests = new ArrayList<>();
Map<String, String> reqHeaders = ImmutableMap.of("foo", "bar");
long now = System.currentTimeMillis();
TransactionRecord rec1 = new TransactionRecord(now, 200, "127.0.0.1", reqHeaders, "/foo", "userid");
indexRequests.add(new IndexRequest(indexName, type).source(rec1.getSource().array()));
TransactionRecord rec2 = new TransactionRecord(now, 404, "127.0.0.2", reqHeaders, "/bar", "userid");
indexRequests.add(new IndexRequest(indexName, type).source(rec2.getSource().array()));
esClient.index(indexRequests);
esClient.refreshIndex(indexName)
.searchAll(indexName);
}
private static Client getClient(String clusterName) throws UnknownHostException {
Settings settings = ImmutableSettings.settingsBuilder()
.put("client.transport.sniff",true)
.put("cluster.name", clusterName)
.build();
return new TransportClient(settings).addTransportAddresses(
new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300),
new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9301)
);
}
/**
* refresh index data so it's searchable
*
* @return
*/
public LocalElasticsearch refreshIndex(String indexName) {
client.admin().indices().prepareRefresh(indexName).get();
return this;
}
public void index(List<IndexRequest> toBeIndexed) {
BulkProcessor bulkProcessor = BulkProcessor.builder(client,
new BulkProcessor.Listener() {
public void beforeBulk(long executionId, BulkRequest request) {
logger.info("before[" + executionId + "]");
}
public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {
logger.info("success[" + executionId + "], took " + response.getTookInMillis());
}
public void afterBulk(long executionId, BulkRequest request, Throwable failure) {
logger.error("error[" + executionId + "]");
}
})
.setBulkActions(-1)
.setBulkSize(new ByteSizeValue(-1, ByteSizeUnit.MB))
.setConcurrentRequests(1)
.build();
for (IndexRequest indexRequest : toBeIndexed) {
bulkProcessor.add(indexRequest);
}
try {
bulkProcessor.awaitClose(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public LocalElasticsearch deleteIndex(String indexName) {
boolean indexExists = client.admin().indices().prepareExists(indexName).execute().actionGet().isExists();
if (indexExists) {
client.admin().indices().prepareDelete(indexName).execute().actionGet();
}
return this;
}
/**
* equivalent to: curl -XPUT 'http://localhost:9200/test-index/'
*
* @return
*/
public LocalElasticsearch createIndex(String indexName) {
client.admin().indices().prepareCreate(indexName).execute().actionGet();
return this;
}
/**
* equivalent to: curl -XGET 'http://localhost:9200/test-index/test-type/_search?pretty'
*
* @return
*/
public void searchAll(String indexName) {
SearchResponse allHits = client.prepareSearch(indexName).get();
logger.info("found " + allHits.getHits().getTotalHits());
if (allHits.getHits().getTotalHits() > 0) {
logger.info("first result: " + allHits.getHits().getAt(0).getSourceAsString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment