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 milliseconds that the clients that Camel creates wait to connect to Solr. The connectionTimeout option of an endpoint replaces it. | 60000 | long | |
The name of the collection, for the endpoints that set no collection. | String | ||
The host name of the Solr server, for the endpoints that set their host to default. If not set, these endpoints use localhost. | 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 port of the Solr server, for the endpoints that set no port. If not set, these endpoints use 8983. | int | ||
The time in milliseconds that the clients that Camel creates wait for the response to a request. The requestTimeout option of an endpoint replaces it. | 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 a custom org.apache.solr.client.solrj.SolrClient for all the endpoints, instead of a client that Camel creates for each endpoint. The solrClient option of an endpoint replaces it. Camel does not close a client that is set or autowired. | SolrClient | ||
Whether the clients that Camel creates connect to Solr with HTTPS instead of HTTP. The enableSSL option of an endpoint replaces it. | false | boolean | |
The password for HTTP basic authentication with Solr, for all the endpoints. The password option of an endpoint replaces it. | String | ||
The TLS configuration (key store, trust store, protocols, cipher suites, named groups) of the clients that Camel creates, for all the endpoints. It is used when the enableSSL option is enabled. The sslContextParameters option of an endpoint replaces it. | SSLContextParameters | ||
Whether the endpoints use the global SSL context parameters of the CamelContext when neither the component nor the endpoint sets the sslContextParameters option. | false | boolean | |
The user name for HTTP basic authentication with Solr, for all the endpoints. The username option of an endpoint replaces it. | 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 |
|---|---|---|---|
Required The host name of the Solr server. Set it to default to use the host option of the component, or localhost when that is not set. | String | ||
The port of the Solr server. If not set, the port option of the component is used, or 8983 when that is not set. | 8983 | int | |
The base path of the Solr server. A path that starts with /solr/ sets the collection and the request handler instead: in /solr/products/update, products is the collection and /update the request handler. | /solr | String |
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
Whether the producer sends the requests asynchronously. This needs a client that extends HttpSolrClientBase, such as the HttpJdkSolrClient that Camel creates; with another client the producer sends the requests synchronously. | true | boolean | |
Whether the producer adds the commit=true parameter to each request, so that Solr commits an insert or a delete at once. | false | boolean | |
The name of the collection that the requests go to. The CamelSolrCollection header replaces this option. If not set, the defaultCollection option of the component is used, or the default collection of the solrClient. | String | ||
The time in milliseconds that the client that Camel creates waits to connect to Solr. If not set, the connectionTimeout option of the component is used. | Long | ||
Whether a DELETE takes the body as a query, or a collection of queries, instead of the id of the documents to delete. The CamelSolrDeleteByQuery header replaces this option. | false | boolean | |
The index of the first document that a search returns, which is the start parameter of Solr (0 by default in Solr). The CamelSolrFrom header replaces this option. Not used for a QueryRequest body. | Integer | ||
The operation that the producer performs. The CamelSolrOperation header replaces this option, and a body that is a SolrJ request, such as a SolrQuery or an UpdateRequest, replaces both. If none is set, the body and the headers decide, for example a Map body inserts a document. Enum values:
| SolrOperation | ||
The path of the request handler, for example /update/json. An insert or a delete sends the update request to it (/update when not set), and a search sends it as the qt parameter. The CamelSolrRequestHandler header replaces this option. | String | ||
The time in milliseconds that the client that Camel creates waits for the response to a request. If not set, the requestTimeout option of the component is used. | Long | ||
The maximum number of documents that a search returns, which is the rows parameter of Solr (10 by default in Solr). The CamelSolrSize header replaces this option. Not used for a QueryRequest body. | 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 | |
To use a custom org.apache.solr.client.solrj.SolrClient. When set, the host, port, basePath, username, password, enableSSL, certificatePath, sslContextParameters, connectionTimeout and requestTimeout options do not configure the client. If not set, Camel creates an HttpJdkSolrClient for the endpoint. Camel does not close a client that is set. | SolrClient | ||
The X.509 certificate of the certificate authority that the client that Camel creates trusts for HTTPS (the enableSSL option must be enabled). It is loaded from the classpath by default, or with the classpath:, file: or https: prefix; an http: URL is refused. Not used when the sslContextParameters option is set. | String | ||
Whether the client that Camel creates connects to Solr with HTTPS instead of HTTP. It must be enabled to use the certificatePath or sslContextParameters option, otherwise the endpoint fails at startup. | false | boolean | |
The password for HTTP basic authentication with Solr, used together with the username option by the client that Camel creates. Not used when the solrClient option is set. | String | ||
The TLS configuration (key store, trust store, protocols, cipher suites, named groups) of the client that Camel creates, which is used when the enableSSL option is enabled. It takes precedence over the certificatePath option. If not set, the global SSL context parameters are used when the useGlobalSslContextParameters option of the component is enabled. | SSLContextParameters | ||
The user name for HTTP basic authentication with Solr, used together with the password option by the client that Camel creates. Not used when the solrClient option is set. | 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: INSERT, DELETE, SEARCH or PING. It replaces the operation option, and a body that is a SolrJ request replaces it. Older values such as DELETE_BY_QUERY or COMMIT still work, and log a warning. | String | |
CamelSolrCollection (producer) Constant: | The name of the collection that the request goes to. It replaces the collection option. | String | |
CamelSolrRequestHandler (producer) Constant: | The path of the request handler, for example /update/json. It replaces the requestHandler option. | String | |
CamelSolrQueryString (producer) Constant: | The query of a search. It replaces a text body, but not a SolrQuery or QueryRequest body. When no operation is set, a message with this header is a search. | String | |
| Constant: | The maximum number of documents that a search returns, which is the rows parameter of Solr. It replaces the size option. | Integer | |
| Constant: | The index of the first document that a search returns, which is the start parameter of Solr. It replaces the from option. | Integer | |
| Constant: | The request parameters to send to Solr with an insert, a delete or a search. The headers with the CamelSolrParam. prefix add to them. | SolrParams | |
CamelSolrDeleteByQuery (producer) Constant: | Whether a DELETE takes the body as a query, or a collection of queries, instead of the id of the documents to delete. It replaces the deleteByQuery option. | false | boolean |
| Constant: | The content type of a file or text body to insert, for example application/json. If not set, it is detected from the file or the text. | 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, the sides and the tools that use them are described in Message Body Types.
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.
Connecting with HTTPS
The client that Camel creates connects to Solr with plain HTTP unless the enableSSL option is enabled. Set enableSSL=true to connect with HTTPS, and configure the TLS of the client with the sslContextParameters option, or with the certificatePath option to trust the X.509 certificate of a certificate authority. Without either, the default trust store of the JVM is used.
When the certificatePath or sslContextParameters option (of the endpoint or the component) is set, but enableSSL is not enabled, the endpoint fails at startup, as the options would not be used. The global SSL context parameters (useGlobalSslContextParameters=true) only log a WARN in that case.
The certificatePath option loads the certificate from the classpath by default, or with the classpath:, file: or https: prefix. An http: URL is refused, as the certificate would be loaded over plain HTTP.
These options do not apply to a client that is set with the solrClient option.
from("direct:search")
.to("solr:solr.example.com:8983?enableSSL=true&certificatePath=file:/etc/solr/ca.crt&operation=SEARCH"); 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: