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,*.yamlfiles, 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 |
|---|---|---|---|
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 | ||
To configure security using SSLContextParameters. When configured, this takes precedence over the certificatePath option. | SSLContextParameters | ||
Enable usage of global SSL context parameters. | false | boolean | |
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
| 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. | false | 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 | ||
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 | ||
Basic authenticate user. | String |
Message Headers
The Solr component supports the following 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 | |
| Constant: | The prefix of the headers that carry the fields of the document to insert: the header CamelSolrField.id holds the id field. | Object | |
| Constant: | 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 |
| 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.
|
Producer output |
| 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 |
|---|---|---|
| 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) |
| 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 |
| Map or Collection<Map> | inserts/updates a document or documents with the fields of the Map |
| String | inserts/updates the documents of the given JSON or XML text: a JSON array of documents, or XML documents (an |
| 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 |
| n/a | sends only the solr parameters (such as a commit) |
| n/a | Pings the solr instance |
| SolrQuery | Performs a search request to the solr instance |
| QueryRequest | Performs a search request to the solr instance |
| String | Performs a search request with the body as the query |
| n/a | Performs a search request with the query of the |
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.
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: