[[solr-component]]
= Solr Component
//THIS FILE IS COPIED: EDIT THE SOURCE FILE:
:page-source: components/camel-solr/src/main/docs/solr-component.adoc
:docTitle: Solr
:artifactId: camel-solr
:description: Perform operations against Apache Lucene Solr.
:since: 2.9
:supportLevel: Stable
:component-header: Only producer is supported
include::{cq-version}@camel-quarkus:ROOT:partial$reference/components/solr.adoc[opts=optional]

*Since Camel {since}*

*{component-header}*

The Solr component allows you to interface with an
https://solr.apache.org/[Apache Solr] server.

Maven users will need to add the following dependency to their `pom.xml`
for this component:

[source,xml]
------------------------------------------------------------
<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

[source,java]
--------------------------------------
solr://host[:port]/solr?[options]
solrs://host[:port]/solr?[options]
solrCloud://host[:port]/solr?[options]
--------------------------------------

== Solr Options


// component options: START
The Solr component supports 2 options, which are listed below.



[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
| *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
| *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
|===
// component options: END



// endpoint options: START
The Solr endpoint is configured using URI syntax:

----
solr:url
----

with the following path and query parameters:

=== Path Parameters (1 parameters):


[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
| *url* | *Required* Hostname and port for the Solr server(s). Multiple hosts can be specified, separated with a comma. See the solrClient parameter for more information on the SolrClient used to connect to Solr. |  | String
|===


=== Query Parameters (19 parameters):


[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
| *autoCommit* (producer) | If true, each producer operation will be automatically followed by a commit | false | boolean
| *connectionTimeout* (producer) | Sets the connection timeout on the SolrClient |  | Integer
| *defaultMaxConnectionsPerHost* (producer) | *Deprecated* maxConnectionsPerHost on the underlying HttpConnectionManager |  | Integer
| *httpClient* (producer) | Sets the http client to be used by the solrClient. This is only applicable when solrClient is not set. |  | HttpClient
| *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
| *maxRetries* (producer) | *Deprecated* Maximum number of retries to attempt in the event of transient errors |  | Integer
| *maxTotalConnections* (producer) | *Deprecated* maxTotalConnection on the underlying HttpConnectionManager |  | Integer
| *requestHandler* (producer) | Set the request handler to be used |  | String
| *solrClient* (producer) | Uses the provided solr client to connect to solr. When this parameter is not specified, camel applies the following rules to determine the SolrClient: 1) when zkHost or zkChroot (=zookeeper root) parameter is set, then the CloudSolrClient is used. 2) when multiple hosts are specified in the uri (separated with a comma), then the CloudSolrClient (uri scheme is 'solrCloud') or the LBHttpSolrClient (uri scheme is not 'solrCloud') is used. 3) when the solr operation is INSERT_STREAMING, then the ConcurrentUpdateSolrClient is used. 4) otherwise, the HttpSolrClient is used. Note: A CloudSolrClient should point to zookeeper endpoint(s); other clients point to Solr endpoint(s). The SolrClient can also be set via the exchange header 'CamelSolrClient'. |  | SolrClient
| *soTimeout* (producer) | Sets the socket timeout on the SolrClient |  | Integer
| *streamingQueueSize* (producer) | Sets the queue size for the ConcurrentUpdateSolrClient | 10 | int
| *streamingThreadCount* (producer) | Sets the number of threads for the ConcurrentUpdateSolrClient | 2 | int
| *collection* (CloudSolrClient) | Set the default collection for SolrCloud |  | String
| *zkChroot* (CloudSolrClient) | Set the chroot of the zookeeper connection (include the leading slash; e.g. '/mychroot') |  | String
| *zkHost* (CloudSolrClient) | Set the ZooKeeper host(s) urls which the CloudSolrClient uses, e.g. zkHost=localhost:2181,localhost:2182. Optionally add the chroot, e.g. zkHost=localhost:2181,localhost:2182/rootformysolr. In case the first part of the url path (='contextroot') is set to 'solr' (e.g. 'localhost:2181/solr' or 'localhost:2181/solr/..'), then that path is not considered as zookeeper chroot for backward compatibility reasons (this behaviour can be overridden via zkChroot parameter). |  | String
| *allowCompression* (HttpSolrClient) | Server side must support gzip or deflate for this to have any effect |  | Boolean
| *followRedirects* (HttpSolrClient) | Indicates whether redirects are used to get to the Solr server |  | Boolean
| *password* (security) | Sets password for basic auth plugin enabled servers |  | String
| *username* (security) | Sets username for basic auth plugin enabled servers |  | String
|===
// endpoint options: END



== 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.

* INSERT
* INSERT_STREAMING

[width="100%",cols="10%,10%,80%",options="header",]
|=======================================================================
|Operation |Message body |Description

|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 |updates index based on the given SolrInputDocument

|INSERT/INSERT_STREAMING |String XML |updates index based on the given XML (must follow
SolrInputDocument format)

|ADD_BEAN |bean instance |adds an index based on values in an
http://wiki.apache.org/solr/Solrj#Directly_adding_POJOs_to_Solr[annotated
bean]

|ADD_BEANS |collection<bean> |adds index based on a collection of
http://wiki.apache.org/solr/Solrj#Directly_adding_POJOs_to_Solr[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

|SOFT_COMMIT |n/a |performs a `soft commit` (without guarantee that Lucene index files are written to stable storage; useful for Near Real Time operations) 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 (This command reorganizes the Solr index and might be a heavy task)
|=======================================================================

== Example

Below is a simple INSERT, DELETE and COMMIT example

[source,java]
---------------------------------------------------------------------------------------
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");
---------------------------------------------------------------------------------------

[source,xml]
------------------------------------------
<route>
    <from uri="direct:insert"/>
    <setHeader name="SolrOperation">
        <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="SolrOperation">
        <constant>DELETE_BY_ID</constant>
    </setHeader>
    <to uri="solr://localhost:8983/solr"/>
</route>
<route>
    <from uri="direct:commit"/>
    <setHeader name="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.

[source,java]
-----------------------------------------------
    template.sendBody("direct:insert", "1234");
    template.sendBody("direct:commit", null);
    template.sendBody("direct:delete", "1234");
    template.sendBody("direct:commit", null);
-----------------------------------------------

== Querying Solr

The components provides a producer operation to query Solr.

For more information:

https://solr.apache.org/guide/8_8/the-standard-query-parser.html[Solr Query Syntax]


include::{page-component-version}@camel-spring-boot::page$solr-starter.adoc[]
