Solr

Overview

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

Dependencies

Maven users 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

The URI format for a solr endpoint is:

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

Endpoint options

SolrServer options describes the SolrServer options that can be configured for a Solr endpoint.

NameDefaultDescription
maxRetries 0 Specifies the maximum number of retries to attempt when transient errors occur.
soTimeout 1000 Specifies the read timeout, in milliseconds, on the underlying HttpConnectionManager. The use of this option is desirable for queries, but not for indexing.
connectionTimeout 100 Specifies the connection timeout, in milliseconds, on the underlying HttpConnectionManager.
defaultMaxConnectionsPerHost 2 Specifies the maximum total connections per host on the underlying HttpConnectionManager.
maxTotalConnections 20 Specifies the maximum total connections on the underlying HttpConnectionManager.
followRedirects false Indicates whether redirects are used to get to the Solr server.
allowCompression false Specifies whether to allow compression. For this option to effect compression, the server side must support gzip or deflate.
requestHandler /update (xml) Specifies the request handler to use.
streamingThreadCount 2 Apache Camel 2.9.2: Sets the number of threads for the StreamingUpdateSolrServer.
streamingQueueSize 10 Apache Camel 2.9.2: Sets the queue size for the StreamingUpdateSolrServer.

Message operations

the following Solr operations are supported. To use them, set an Exchange header with the key SolrOperation and a value set to one of those listed in Solr message operations.

[Note]Note

Some operations also require you to set the message body

  • INSERT operations use the CommonHttpSolrServer

  • INSERT_STREAMING operations use the StreamingUpdateHttpSolrServer (Apache Camel 2.9.2)

Solr message operations describes the supported Solr message operations.

OperationMessage bodyDescription
INSERT/INSERT_STREAMING NA Adds an index using message headers (must be prefixed with SolrField.
INSERT/INSERT_STREAMING FileAdds an index using the specified file (using ContentStreamUpdateRequest.
INSERT/INSERT_STREAMING SolrInputDocument Apache Camel 2.9.2: Updates an index based on the specified SolrInputDocument.
INSERT/INSERT_STREAMING String XML Apache Camel 2.9.2: Updates an index based on the specified XML (must follow SolrInputDocument format).
ADD_BEAN bean instance Adds an index based on values in an annotated bean.
DELETE_BY_ID index id to delete Deletes a record based on an ID.
DELETE_BY_QUERY query string Deletes a record based on a query string.
COMMIT NA Performs a commit on pending index changes.
ROLLBACK NA Performs a rollback on pending index changes.
OPTIMIZE NA First performs a commit on pending index changes and then runs the optimize command.

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 just 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, the Solr component does not support querying data natively, but you can query Solr using HTTP this way:

//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");