Camel Components

Solr

Since Camel 4.8

Only producer is supported

The Solr component allows you to interface with an Apache Solr server.

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]?[options]

Configuring Options

Camel components are configured on two separate levels:

  • component level

  • endpoint level

Configuring Component Options

At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.

For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.

Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.

You can configure components using:

  • the Component DSL.

  • in a configuration file (application.properties, *.yaml files, etc).

  • directly in the Java code.

Configuring Endpoint Options

You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.

Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.

A good practice when configuring options is to use Property Placeholders.

Property placeholders provide a few benefits:

  • They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.

  • They allow externalizing the configuration from the code.

  • They help the code to become more flexible and reusable.

The following two sections list all the options, firstly for the component followed by the endpoint.

Component Options

The Solr component supports the following options which are listed below.

Name Description Default Type

connectionTimeout (producer)

The time in ms to wait before connection will time out.

60000

long

defaultCollection (producer)

Solr default collection name.

String

host (producer)

The solr instance host name.

String

lazyStartProducer (producer)

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

port (producer)

The solr instance port number.

int

requestTimeout (producer)

The timeout in ms to wait before the socket will time out.

600000

long

autowiredEnabled (advanced)

Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc.

true

boolean

solrClient (advanced)

Autowired To use an existing configured solr client, instead of creating a client per endpoint. This allows customizing the client with specific advanced settings.

SolrClient

enableSSL (security)

Enable SSL.

false

boolean

password (security)

Password for authenticating.

String

sslContextParameters (security)

To configure security using SSLContextParameters. When configured, this takes precedence over the certificatePath option.

SSLContextParameters

useGlobalSslContextParameters (security)

Enable usage of global SSL context parameters.

false

boolean

username (security)

Basic authenticate user.

String

Endpoint Options

The Solr endpoint is configured using URI syntax:

solr:host:port/basePath

With the following path and query parameters:

Path Parameters

Name Description Default Type

host (producer)

Required The solr instance host name (set to 'default' to use the host name defined on component level).

String

port (producer)

The solr instance port number.

8983

int

basePath (producer)

The solr instance base path (usually /solr).

/solr

String

Query Parameters

Name Description Default Type

async (producer)

Use async request processing (when supported by the solr client).

true

boolean

autoCommit (producer)

If true, each producer insert/delete operation will be automatically performing a commit.

false

boolean

collection (producer)

The name of the collection to act against.

String

connectionTimeout (producer)

The time in ms to wait before connection will time out.

Long

deleteByQuery (producer)

For the delete instruction, interprete body as query/queries instead of id/ids.

false

boolean

from (producer)

Starting index of the response.

Integer

operation (producer)

What operation to perform.

Enum values:

  • DELETE

  • INSERT

  • PING

  • SEARCH

SolrOperation

requestHandler (producer)

The path of the update request handler (use for update requests / set solr parameter qt for search requests).

String

requestTimeout (producer)

The time in ms to wait before the request will time out (former soTimeout).

Long

size (producer)

Size of the response.

Integer

lazyStartProducer (producer (advanced))

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

solrClient (advanced)

The solr client to connect to solr. When solrClient bean is provided, all connection properties will be used from that solrClient (host, port, username, password, connectionTimeout, requestTimeout, enableSSL, …​). When not explicitly configured, camel uses the HttpJdkSolrClient.

SolrClient

certificatePath (security)

The certificate that can be used to access the solr host. It can be loaded by default from classpath, but you can prefix with classpath:, file:, or http: to load the resource from different systems.

String

enableSSL (security)

Enable SSL.

false

boolean

password (security)

Password for authenticating.

String

sslContextParameters (security)

To configure security using SSLContextParameters. When configured, this takes precedence over the certificatePath option. This allows configuring named groups, signature schemes, cipher suites, and protocols for the TLS connection.

SSLContextParameters

username (security)

Basic authenticate user.

String

Message Headers

The Solr component supports the following message header(s), which is/are listed below:

Name Description Default Type

CamelSolrOperation (producer)

Constant: PARAM_OPERATION

The operation to perform.

String

CamelSolrCollection (producer)

Constant: PARAM_COLLECTION

The collection to execute the request against.

String

CamelSolrRequestHandler (producer)

Constant: PARAM_REQUEST_HANDLER

The request handler to execute the solr request against.

String

CamelSolrQueryString (producer)

Constant: PARAM_QUERY_STRING

The query to execute.

String

CamelSolrSize (producer)

Constant: PARAM_SIZE

The size of the response.

Integer

CamelSolrFrom (producer)

Constant: PARAM_FROM

The starting index of the response.

Integer

CamelSolrParams (producer)

Constant: PARAM_SOLR_PARAMS

The solr parameters to use for the request.

SolrParams

CamelSolrDeleteByQuery (producer)

Constant: PARAM_DELETE_BY_QUERY

For the delete instruction, interpret body as query/queries instead of id/ids.

false

boolean

Content-Type (producer)

Constant: PARAM_CONTENT_TYPE

The content type is used to identify the type when inserting files.

String

CamelSolrField. (producer)

Constant: HEADER_FIELD_PREFIX

The prefix of the headers that carry the fields of the document to insert: the header CamelSolrField.id holds the id field.

Object

CamelSolrParam. (producer)

Constant: HEADER_PARAM_PREFIX

The prefix of the headers that carry extra request parameters: the header CamelSolrParam.commit holds the commit parameter.

String

Message Body

The message body at each side of the Solr component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.

Side Body Description

Producer input

any

What the operation needs: INSERT a Map or a SolrInputDocument, a List of them or of @Field beans, a JSON array or XML text, or a file; DELETE the id or, with deleteByQuery=true, the query, as text or a List of text; SEARCH the query as text or a SolrQuery; PING does not use it. Convert bytes or a stream to text first. The CamelSolrField. and CamelSolrQueryString headers can replace the body.

  • With operation=DELETE: text or list

Producer output

map

The response of Solr as a Map (a SimpleOrderedMap) with the responseHeader and, for a search, the documents under response as a SolrDocumentList. Convert it to a QueryResponse or a SolrPingResponse to read it with SolrJ.

The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers are described in the Camel Catalog.

Usage

Message Operations

The following Solr operations are currently supported. Set the operation with the CamelSolrOperation header, or with the operation option of the endpoint (the header wins). Some operations also require the message body to be set. When neither is set, the body decides: a Map, a file or a collection inserts, a collection of Strings deletes by id, and the CamelSolrQueryString header searches. A body that is a request of SolrJ, such as a SolrQuery or an UpdateRequest, runs its own operation.

  • INSERT

  • DELETE

  • SEARCH

  • PING

Operation Message body Description

INSERT

n/a

inserts/updates a document using message headers (solr fields must be prefixed with "CamelSolrField."), or with no such headers, sends only the solr parameters (such as a commit)

INSERT

File

inserts/updates a document or documents using the given File (using ContentStreamUpdateRequest)

INSERT

SolrInputDocument or Collection<SolrInputDocument>

inserts/updates a document or documents based on the given (collection of) SolrInputDocument

INSERT

bean or Collection<bean>

inserts/updates a document or documents based on values in an annotated bean

INSERT

Map or Collection<Map>

inserts/updates a document or documents with the fields of the Map

INSERT

String

inserts/updates the documents of the given JSON or XML text: a JSON array of documents, or XML documents (an <add> element is added when the text does not start with one)

DELETE

String or Collection<String>

delete a record by ID (or collection of ids), or by a query (or a collection of queries) when the header CamelSolrDeleteByQuery or the option deleteByQuery is true (false by default)

DELETE

n/a

sends only the solr parameters (such as a commit)

PING

n/a

Pings the solr instance

SEARCH

SolrQuery

Performs a search request to the solr instance

SEARCH

QueryRequest

Performs a search request to the solr instance

SEARCH

String

Performs a search request with the body as the query

SEARCH

n/a

Performs a search request with the query of the CamelSolrQueryString header (or the CamelSolrParam.q header)

When the message has no body and the headers do not give what the operation needs (the CamelSolrField. headers or solr parameters for INSERT, solr parameters for DELETE, the CamelSolrQueryString header for SEARCH), the operation fails with an InvalidPayloadException. An INSERT of a body that is not a document (such as plain text, a byte array or a bean without @Field annotations) fails with the same exception, also when the request has solr parameters (such as the commit the autoCommit option adds).

A field of a Map body or a CamelSolrField. header with a null value is left out of the document.

After each operation the message body is the response of Solr as a Map (a SimpleOrderedMap) with the responseHeader, and for a search the documents under response as a SolrDocumentList. Convert it to a QueryResponse to read it with SolrJ.

Example

Below is a simple INSERT, DELETE and SEARCH example

  • Java

  • XML

  • YAML

from("direct:insert")
    .setHeader("CamelSolrOperation", constant("INSERT"))
    .setHeader("CamelSolrField.id", body())
    .to("solr://localhost:8983/solr/mycollection");

from("direct:delete")
    .setHeader("CamelSolrOperation", constant("DELETE"))
    .to("solr://localhost:8983/solr/mycollection");

from("direct:search")
    .setHeader("CamelSolrOperation", constant("SEARCH"))
    .to("solr://localhost:8983/solr/mycollection");
<route>
    <from uri="direct:insert"/>
    <setHeader name="CamelSolrOperation">
        <constant>INSERT</constant>
    </setHeader>
    <setHeader name="CamelSolrField.id">
        <simple>${body}</simple>
    </setHeader>
    <to uri="solr://localhost:8983/solr/mycollection"/>
</route>
<route>
    <from uri="direct:delete"/>
    <setHeader name="CamelSolrOperation">
        <constant>DELETE</constant>
    </setHeader>
    <to uri="solr://localhost:8983/solr/mycollection"/>
</route>
<route>
    <from uri="direct:search"/>
    <setHeader name="CamelSolrOperation">
        <constant>SEARCH</constant>
    </setHeader>
    <to uri="solr://localhost:8983/solr/mycollection"/>
</route>
- route:
    from:
      uri: direct:insert
      steps:
        - setHeader:
            name: CamelSolrOperation
            expression:
              constant:
                expression: INSERT
        - setHeader:
            name: CamelSolrField.id
            expression:
              simple:
                expression: "${body}"
        - to:
            uri: solr://localhost:8983/solr/mycollection
- route:
    from:
      uri: direct:delete
      steps:
        - setHeader:
            name: CamelSolrOperation
            expression:
              constant:
                expression: DELETE
        - to:
            uri: solr://localhost:8983/solr/mycollection
- route:
    from:
      uri: direct:search
      steps:
        - setHeader:
            name: CamelSolrOperation
            expression:
              constant:
                expression: SEARCH
        - to:
            uri: solr://localhost:8983/solr/mycollection

A client would simply need to pass a body message to the insert or delete routes, with the CamelSolrParam.commit header to commit the change.

Java-only: sending messages to Solr routes using ProducerTemplate
template.sendBodyAndHeader("direct:insert", "1234", "CamelSolrParam.commit", true);
template.sendBodyAndHeader("direct:delete", "1234", "CamelSolrParam.commit", true);
template.sendBody("direct:search", "id:1234");

For more information: