Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
factory for getting ES client
import org.apache.log4j.Logger;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.collect.ImmutableList;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import java.util.Map;
public class TransportClientFactory {
private static final Logger log = Logger.getLogger(TransportClientFactory.class);
private Map<String, String> settings;
private String clientTransportHost;
private Integer clientTransportPort;
private boolean disabled = false;
public TransportClientFactory(Map<String, String> settings,
String clientTransportHost,
Integer clientTransportPort,
boolean disabled) {
this.settings = settings;
this.clientTransportHost = clientTransportHost;
this.clientTransportPort = clientTransportPort;
this.disabled = disabled;
public TransportClient getClient() throws ElasticSearchUnavailableException {
if (this.disabled) {
throw new ElasticSearchUnavailableException("ES is disabled. Check 'es.disable' property");
}"getting client");
Settings s = ImmutableSettings.settingsBuilder()
TransportClient client = new TransportClient(s);
client.addTransportAddress(new InetSocketTransportAddress(
return client;
private void verifyConnection(TransportClient client) {
ImmutableList<DiscoveryNode> nodes = client.connectedNodes();
if (nodes.isEmpty()) {
throw new ElasticSearchUnavailableException("No nodes available. Verify ES is running!");
} else {"connected to nodes: " + nodes.toString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.