Solr Component

Available as of Camel 2.9

The Solr component allows you to interface with an Apache Lucene Solr server (based on SolrJ 3.5.0).

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-solr</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

URI format

solr://host[:port]/solr?[options]

Endpoint Options

The following SolrServer options may be configured on the Solr endpoint.

name default value description
maxRetries0maximum number of retries to attempt in the event of transient errors
soTimeout1000read timeout on the underlying HttpConnectionManager. This is desirable for queries, but probably not for indexing
connectionTimeout100connectionTimeout on the underlying HttpConnectionManager
defaultMaxConnectionsPerHost2maxConnectionsPerHost on the underlying HttpConnectionManager
maxTotalConnections20maxTotalConnection on the underlying HttpConnectionManager
followRedirectsfalseindicates whether redirects are used to get to the Solr server
allowCompressionfalseserver side must support gzip or deflate for this to have any effect
requestHandler/update (xml)set the request handler to be used
streamingThreadCount2Camel 2.10 set the number of threads for the StreamingUpdateSolrServer
streamingQueueSize10Camel 2.10 set the queue size for the StreamingUpdateSolrServer

Message Operations

The following Solr operations are currently supported. Simply set an exchange header with a key of "SolrOperation" and a value set to one of the following. Some operations also require the message body to be set.

operation message body description
INSERTn/aadds an index using message headers (must be prefixed with "SolrField.")
INSERTFileadds an index using the given File (using ContentStreamUpdateRequest)
INSERTSolrInputDocumentCamel 2.10 adds an index based on the given SolrInputDocument
INSERT_STREAMINGn/aCamel 2.10 uses the StreamingUpdateSolrServer to add an index using message headers (must be prefixed with "SolrField.")
INSERT_STREAMINGFileCamel 2.10 uses the StreamingUpdateSolrServer to add an index using the given File (using ContentStreamUpdateRequest)
INSERT_STREAMINGSolrInputDocumentCamel 2.10 uses the StreamingUpdateSolrServer to add an index based on the given SolrInputDocument
ADD_BEANbean instanceadds an index based on values in an annotated bean
DELETE_BY_IDindex id to deletedelete a record by ID
DELETE_BY_QUERYquery stringdelete a record by a query
COMMITn/aperforms a commit on any pending index changes
ROLLBACKn/aperforms a rollback on any pending index changes
OPTIMIZEn/aperforms a commit on any pending index changes and then runs the optimize command

Example

Below is a simple INSERT and COMMIT example

from("direct:insert")
    .setHeader(SolrConstants.OPERATION, constant(SolrConstants.OPERATION_INSERT))
    .setHeader(SolrConstants.FIELD + "id", body())
    .to("solr://localhost:8983/solr");

from("direct:commit")
    .setHeader(SolrConstants.OPERATION, constant(SolrConstants.OPERATION_COMMIT))
    .to("solr://localhost:8983/solr");
<route>
    <from uri="direct:insert"/>
    <setHeader headerName="SolrOperation">
        <constant>INSERT</constant>
    </setHeader>
    <setHeader headerName="SolrField.id">
        <simple>${body}</simple>
    </setHeader>
    <to uri="solr://localhost:8983/solr"/>
</route>
<route>
    <from uri="direct:commit"/>
    <setHeader headerName="SolrOperation">
        <constant>COMMIT</constant>
    </setHeader>
    <to uri="solr://localhost:8983/solr"/>
</route>

A client would simply need to pass a body message to the insert route and then call the commit route.

    template.sendBody("direct:insert", "1234");
    template.sendBody("direct:commit", null);

Querying Solr

Currently, this component doesn't support querying data natively (may be added later). For now, you can query Solr using HTTP as follows:

//define the route to perform a basic query
from("direct:query")
    .recipientList(simple("http://localhost:8983/solr/select/?q=${body}"))
    .convertBodyTo(String.class);
...
//query for an id of '1234' (url encoded)
String responseXml = (String) template.requestBody("direct:query", "id%3A1234");

For more information, see these resources...

Solr Query Tutorial

Solr Query Syntax

See Also

© 2004-2011 The Apache Software Foundation.
Apache Camel, Camel, Apache, the Apache feather logo, and the Apache Camel project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.
Graphic Design By Hiram