Skip to content

Instantly share code, notes, and snippets.

@focampo
Created November 8, 2011 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save focampo/1347758 to your computer and use it in GitHub Desktop.
Save focampo/1347758 to your computer and use it in GitHub Desktop.
ElasticSearch Client Factory
class ElasticSearchItemIndexerFactory {
private final static Logger logger = Logger.getLogger(ElasticSearchItemIndexerFactory.class)
public final static String NODE_TYPE = 'node'
public final static String TRANSPORT_TYPE = 'transport'
/**
* Crea un indexador de items para ElasticSearch
* @param indexerConfig mapa de configuracion generica del indexador
* @param esConfig mapa de configuracion especifica de ElasticSearch
* @return el indexador
*/
public static ElasticSearchItemIndexer createIndexer(indexerConfig, esConfig) {
try {
switch(esConfig.clientType) {
case NODE_TYPE:
Node node = this.createNodeClient(esConfig.clusterName, esConfig.node.local)
return new ElasticSearchItemIndexer(node, indexerConfig)
break
case TRANSPORT_TYPE:
def client = createTransportClient(esConfig.clusterName, esConfig.transport.port,
esConfig.transport.shards, esConfig.transport.sniffCluster)
return new ElasticSearchItemIndexer(client, indexerConfig)
break
}
} catch (NoNodeAvailableException exc) {
throw new RuntimeException("Error creating the indexer. No data server can be found", exc)
} catch (Exception exc) {
throw new RuntimeException("Unexpected error creating the indexer", exc)
}
}
/**
* Crea una instancia de TransportClient
* Este cliente realiza request en modo round-robin entre todos los shards definidos.
*
* @param clusterName
* @param port puerto de conexion como cliente
* @param hosts servidores ES dentro del cluster. Pueden ser solo los masters
* @param sniffCluster si es 'true', se sniffea la red para detectar servidores agregados al cluster
* @return un cliente de tipo TransportClient
*/
protected static Client createTransportClient(clusterName, port, hosts, sniffCluster) {
logger.info("Creating ElasticSearch Transport Client for cluster [$clusterName] and hosts $hosts at port $port")
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", clusterName)
.put("node.client", true)
.put("client.transport.sniff", sniffCluster)
.build()
Client client = new TransportClient(settings)
hosts.each { host ->
client.addTransportAddress(new InetSocketTransportAddress(host, port))
}
return client
}
/**
* Genera un cliente de ES de tipo Node. Este cliente pasa a formar parte del cluster
* pero sin almacenar informacion. De esta manera sabe a que servers redirigir la informacion
* @param clusterName
* @param local
* @return una instancia de NodeClient
*/
protected static Node createNodeClient(clusterName, local) {
logger.info("Creating ElasticSearch Node Client for cluster [$clusterName]")
org.elasticsearch.node.NodeBuilder builder = org.elasticsearch.node.NodeBuilder.nodeBuilder()
.clusterName(clusterName)
.loadConfigSettings(true)
.local(local)
.client(true)
builder.build()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment