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>
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 11 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
The time in ms to wait before connection will time out. | 60000 | long | |
Solr default collection name. | String | ||
The solr instance host name. | String | ||
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 | |
The solr instance port number. | int | ||
The timeout in ms to wait before the socket will time out. | 600000 | long | |
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 | |
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 | ||
Enable SSL. | false | boolean | |
Password for authenticating. | String | ||
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:
Query Parameters (16 parameters)
Name | Description | Default | Type |
---|---|---|---|
Use async request processing (when supported by the solr client). | true | boolean | |
If true, each producer insert/delete operation will be automatically performing a commit. | false | boolean | |
The name of the collection to act against. | String | ||
The time in ms to wait before connection will time out. | Long | ||
For the delete instruction, interprete body as query/queries instead of id/ids. | true | boolean | |
Starting index of the response. | Integer | ||
What operation to perform. Enum values:
| SolrOperation | ||
The path of the update request handler (use for update requests / set solr parameter qt for search requests). | String | ||
The time in ms to wait before the request will time out (former soTimeout). | Long | ||
Size of the response. | Integer | ||
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 | |
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 | ||
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 | ||
Enable SSL. | false | boolean | |
Password for authenticating. | String | ||
Basic authenticate user. | String |
Message Headers
The Solr component supports 9 message header(s), which is/are listed below:
Name | Description | Default | Type |
---|---|---|---|
Constant: | The operation to perform. | String | |
CamelSolrCollection (producer) Constant: | The collection to execute the request against. | String | |
CamelSolrRequestHandler (producer) Constant: | The request handler to execute the solr request against. | String | |
CamelSolrQueryString (producer) Constant: | The query to execute. | String | |
Constant: | The size of the response. | Integer | |
Constant: | The starting index of the response. | Integer | |
Constant: | The solr parameters to use for the request. | SolrParams | |
CamelSolrDeleteByQuery (producer) Constant: | For the delete instruction, interpret body as query/queries instead of id/ids. | false | boolean |
Constant: | The content type is used to identify the type when inserting files. | String |
Usage
Message Operations
The following Solr operations are currently supported. Set an exchange header with a key of "operation" and a value set to one of the following. Some operations also require the message body to be set.
-
INSERT
-
DELETE
-
SEARCH
-
PING
Operation | Message body | Description |
---|---|---|
| n/a | inserts/updates a document using message headers (solr fields must be prefixed with "SolrField.") |
| File | inserts/updates a document or documents using the given File (using ContentStreamUpdateRequest) |
| SolrInputDocument or Collection<SolrInputDocument> | inserts/updates a document or documents based on the given (collection of) SolrInputDocument |
| bean or Collection<bean> | inserts/updates a document or documents based on values in an annotated bean |
| String | inserts/updates a document or documents index based on the given XML or json as string (must follow SolrInputDocument format) |
| String or Collection<String> | delete a record by ID (or collection of ids) or by a query (or a collection of queries) when header "deleteByQuery=true" |
| SolrPing | Pings the solr instance |
| SolrQuery | Performs a search request to the solr instance |
| QueryRequest | Performs a search request to the solr instance |
Example
Below is a simple INSERT
, DELETE
and SEARCH
example
from("direct:insert")
.setHeader(SolrConstants.PARAM_OPERATION, constant(SolrOperation.INSERT))
.setHeader(SolrConstants.FIELD + "id", body())
.to("solr://localhost:8983/solr");
from("direct:delete")
.setHeader(SolrConstants.PARAM_OPERATION, constant(SolrOperation.DELETE))
.to("solr://localhost:8983/solr");
from("direct:search")
.setHeader(SolrConstants.PARAM_OPERATION, constant(SolrOperation.SEARCH))
.to("solr://localhost:8983/solr");
<route>
<from uri="direct:insert"/>
<setHeader name="CamelSolrOperation">
<constant>INSERT</constant>
</setHeader>
<setHeader name="SolrField.id">
<simple>${body}</simple>
</setHeader>
<to uri="solr://localhost:8983/solr"/>
</route>
<route>
<from uri="direct:delete"/>
<setHeader name="CamelSolrOperation">
<constant>DELETE</constant>
</setHeader>
<to uri="solr://localhost:8983/solr"/>
</route>
<route>
<from uri="direct:search"/>
<setHeader name="CamelSolrOperation">
<constant>SEARCH</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.sendBodyAndHeader("direct:insert", "1234", "SolrParam.commit", true);
template.sendBodyAndHeader("direct:delete", "1234", "SolrParam.commit", true);
template.sendBody("direct:search", "id:1234");
For more information:
Spring Boot Auto-Configuration
When using solr with Spring Boot make sure to use the following Maven dependency to have support for auto configuration:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-solr-starter</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
The component supports 12 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
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 | |
The time in ms to wait before connection will time out. | 60000 | Long | |
Solr default collection name. | String | ||
Enable SSL. | false | Boolean | |
Whether to enable auto configuration of the solr component. This is enabled by default. | Boolean | ||
The solr instance host name. | String | ||
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 | |
Password for authenticating. | String | ||
The solr instance port number. | Integer | ||
The timeout in ms to wait before the socket will time out. | 600000 | Long | |
To use an existing configured solr client, instead of creating a client per endpoint. This allows customizing the client with specific advanced settings. The option is a org.apache.solr.client.solrj.SolrClient type. | SolrClient | ||
Basic authenticate user. | String |