Solr Component

Table of Contents

URI format
Solr Options
Message Operations
Example
Querying Solr
See Also

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

NOTE: solrs and solrCloud are new added since Camel 2.14.

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

Solr Options

The Solr component has no options.

The Solr component supports 14 endpoint options which are listed below:

{% raw %}

NameGroupDefaultJava TypeDescription

url

producer

 

String

Required Hostname and port for the solr server

allowCompression

producer

 

Boolean

Server side must support gzip or deflate for this to have any effect

connectionTimeout

producer

 

Integer

connectionTimeout on the underlying HttpConnectionManager

defaultMaxConnectionsPerHost

producer

 

Integer

maxConnectionsPerHost on the underlying HttpConnectionManager

followRedirects

producer

 

Boolean

indicates whether redirects are used to get to the Solr server

maxRetries

producer

 

Integer

Maximum number of retries to attempt in the event of transient errors

maxTotalConnections

producer

 

Integer

maxTotalConnection on the underlying HttpConnectionManager

requestHandler

producer

 

String

Set the request handler to be used

soTimeout

producer

 

Integer

Read timeout on the underlying HttpConnectionManager. This is desirable for queries but probably not for indexing

streamingQueueSize

producer

10

int

Set the queue size for the StreamingUpdateSolrServer

streamingThreadCount

producer

2

int

Set the number of threads for the StreamingUpdateSolrServer

synchronous

advanced

false

boolean

Sets whether synchronous processing should be strictly used or Camel is allowed to use asynchronous processing (if supported).

collection

solrCloud

 

String

Set the collection name which the solrCloud server could use

zkHost

solrCloud

 

String

Set the ZooKeeper host information which the solrCloud could use such as zkhost=localhost:8123.

{% endraw %}

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.

OperationMessage bodyDescription

INSERT/INSERT_STREAMING

n/a

adds an index using message headers (must be prefixed with "SolrField.")

INSERT/INSERT_STREAMING

File

adds an index using the given File (using ContentStreamUpdateRequest)

INSERT/INSERT_STREAMING

SolrInputDocument

Camel 2.9.2 updates index based on the given SolrInputDocument

INSERT/INSERT_STREAMING

String XML

Camel 2.9.2 updates index based on the given XML (must follow SolrInputDocument format)

ADD_BEAN

bean instance

adds an index based on values in an annotated bean

ADD_BEANS

collection<bean>

Camel 2.15 adds index based on a collection of annotated bean

DELETE_BY_ID

index id to delete

delete a record by ID

DELETE_BY_QUERY

query string

delete a record by a query

COMMIT

n/a

performs a commit on any pending index changes

ROLLBACK

n/a

performs a rollback on any pending index changes

OPTIMIZE

n/a

performs a commit on any pending index changes and then runs the optimize command

Example

Below is a simple INSERT, DELETE 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:delete")
    .setHeader(SolrConstants.OPERATION, constant(SolrConstants.OPERATION_DELETE_BY_ID))
    .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:delete"/>
    <setHeader headerName="SolrOperation">
        <constant>DELETE_BY_ID</constant>
    </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 or delete routes and then call the commit route.

    template.sendBody("direct:insert", "1234");
    template.sendBody("direct:commit", null);
    template.sendBody("direct:delete", "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