Red Hat Training

A Red Hat training course is available for Red Hat Fuse

Chapter 43. ElasticSearch

ElasticSearch Component

Available as of Camel 2.11
The ElasticSearch component allows you to interface with an ElasticSearch server.
Maven users will need to add the following dependency to their pom.xml for this component:
    <!-- use the same version as your Camel core version -->

Camel on EAP deployment

This component is supported by the Camel on EAP (Wildfly Camel) framework, which offers a simplified deployment model on the Red Hat JBoss Enterprise Application Platform (JBoss EAP) container. For details of this model, see chapter "Apache Camel on JBoss EAP" in "Deploying into a Web Server".

URI format

If you want to run against a local (in JVM/classloader) ElasticSearch server, just set the clusterName value in the URI to local. See the client guide for more details.

Endpoint Options

The following options may be configured on the ElasticSearch endpoint. All are required to be set as either an endpoint URI parameter or as a header (headers override endpoint properties)
name description
operation Required, indicates the operation to perform.
The name of the index to act against.
The type of the index to act against.
The TransportClient remote host IP address to use Camel 2.12.
The TransportClient remote port to use (defaults to 9300) Camel 2.12.
Camel 2.16: Comma-separated list with ip:port formatted remote transport addresses to use. Options ip and port must be left blank for transportAddresses to be considered instead.
Camel 2.16: The write consistency level to use with INDEX and BULK operations (can be any of ONE, QUORUM, ALL or DEFAULT).
Camel 2.16: The replication type to use with INDEX and BULK operations (can be any of SYNC, ASYNC or DEFAULT). Camel 2.17: replicationType option has been removed, since from Elasticsearch 2.0.0 the async replication has been removed..
Camel 2.16.1 / 2.17.0: Optionally used with INDEX operations for Elasticsearch Parent-Child relationships to specify the ID of the parent record.
Camel 2.17: Specifies whether the client is allowed to sniff the rest of the cluster.

Message Operations

The following ElasticSearch operations are currently supported. Simply set an endpoint URI option or exchange header with a key of operation and a value set to one of the following. Some operations also require other parameters or the message body to be set.
operation message body description
INDEX Map, String, byte[] or XContentBuilder content to index Adds content to an index and returns the content's indexId in the body.
GET_BY_ID Index ID of content to retrieve Retrieves the specified index and returns a GetResult object in the body.
DELETE Index ID of content to delete Deletes the specified indexId and returns a DeleteResult object in the body.
A List or Collection of any type that is already accepted (XContentBuilder, Map, byte[], or String)
Camel 2.14, Adds content to an index and return a List of the id of the successfully indexed documents in the body.
A List or Collection of any type that is already accepted (XContentBuilder, Map, byte[], or String)
Camel 2.15, Adds content to an index and returns the BulkResponse object in the body.
SEARCH Map or SearchRequest
Camel 2.15: Search the content with the map of query string.
MULTIGET List of MultigetRequest.Item
Camel 2.17: Retrieves the specified indexes, types etc. in MultigetRequest and returns a MultigetResponse object in the body.
MULTISEARCH List of SearchRequest
Camel 2.17: Search for parameters specified in MultiSearchRequest and returns a MultiSearchResponse object in the body.
Index name as header
Camel 2.17: Returns a Boolean object in the body.
UPDATE Map, String, byte[] or XContentBuilder content to update
Camel 2.17: Updates content to an index and returns the content's indexId in the body.

Index Example

Below is a simple INDEX example
    <from uri="direct:index" />
    <to uri="elasticsearch://local?operation=INDEX&indexName=twitter&indexType=tweet"/>
A client would simply need to pass a body message containing a Map to the route. The result body contains the indexId created.
Map<String, String> map = new HashMap<String, String>();
map.put("content", "test");
String indexId = template.requestBody("direct:index", map, String.class);

Java Example

The following example shows how to use the ElasticSearch component from a Camel route defined in Java:
CamelContext camelContext = new DefaultCamelContext();
camelContext.addRoutes(new RouteBuilder() {
    public void configure() throws Exception {

Map<String, String> indexedData = new HashMap<>();
indexedData.put("content", "test");

ProducerTemplate template = camelContext.createProducerTemplate();
template.sendBody("direct:index", indexedData);

For more information, see these resources