Update Request Processors
Every update request received by Solr is run through a chain of plugins known as Update Request Processors, or URPs.
This can be useful, for example, to add a field to the document being indexed; to change the value of a particular field; or to drop an update if the incoming document doesn’t fulfill certain criteria. In fact, a surprisingly large number of features in Solr are implemented as Update Processors and therefore it is necessary to understand how such plugins work and where are they configured.
URP Anatomy and Lifecycle
An Update Request Processor is created as part of a chain of one or more update processors. Solr creates a default update request processor chain comprising of a few update request processors which enable essential Solr features. This default chain is used to process every update request unless a user chooses to configure and specify a different custom update request processor chain.
The easiest way to describe an Update Request Processor is to look at the Javadocs of the abstract class UpdateRequestProcessor. Every UpdateRequestProcessor must have a corresponding factory class which extends UpdateRequestProcessorFactory. This factory class is used by Solr to create a new instance of this plugin. Such a design provides two benefits:
-
An update request processor need not be thread safe because it is used by one and only one request thread and destroyed once the request is complete.
-
The factory class can accept configuration parameters and maintain any state that may be required between requests. The factory class must be thread-safe.
Every update request processor chain is constructed during loading of a Solr core and cached until the core is unloaded.
Each UpdateRequestProcessorFactory
specified in the chain is also instantiated and initialized with configuration that may have been specified in solrconfig.xml
.
When an update request is received by Solr, it looks up the update chain to be used for this request. A new instance of each UpdateRequestProcessor specified in the chain is created using the corresponding factory. The update request is parsed into corresponding UpdateCommand objects which are run through the chain. Each UpdateRequestProcessor instance is responsible for invoking the next plugin in the chain. It can choose to short circuit the chain by not invoking the next processor and even abort further processing by throwing an exception.
A single update request may contain a batch of multiple new documents or deletes and therefore the corresponding processXXX methods of an UpdateRequestProcessor will be invoked multiple times for every individual update. However, it is guaranteed that a single thread will serially invoke these methods. |
Update Request Processor Configuration
Update request processors chains can be created by either creating the whole chain directly in solrconfig.xml
or by creating individual update processors in solrconfig.xml
and then dynamically creating the chain at run-time by specifying all processors via request parameters.
However, before we understand how to configure update processor chains, we must learn about the default update processor chain because it provides essential features which are needed in most custom request processor chains as well.
Default Update Request Processor Chain
In case no update processor chains are configured in solrconfig.xml
, Solr will automatically create a default update processor chain which will be used for all update requests.
This default update processor chain consists of the following processors (in order):
-
LogUpdateProcessorFactory
- Tracks the commands processed during this request and logs them -
DistributedUpdateProcessorFactory
- Responsible for distributing update requests to the right node e.g., routing requests to the leader of the right shard and distributing updates from the leader to each replica. This processor is activated only in SolrCloud mode. -
RunUpdateProcessorFactory
- Executes the update using internal Solr APIs.
Each of these perform an essential function and as such any custom chain usually contain all of these processors.
The RunUpdateProcessorFactory
is usually the last update processor in any custom chain.
Custom Update Request Processor Chain
The following example demonstrates how a custom chain can be configured inside solrconfig.xml
.
<updateRequestProcessorChain name="dedupe">
<processor class="solr.processor.SignatureUpdateProcessorFactory">
<bool name="enabled">true</bool>
<str name="signatureField">id</str>
<bool name="overwriteDupes">false</bool>
<str name="fields">name,features,cat</str>
<str name="signatureClass">solr.processor.Lookup3Signature</str>
</processor>
<processor class="solr.LogUpdateProcessorFactory" />
<processor class="solr.RunUpdateProcessorFactory" />
</updateRequestProcessorChain>
In the above example, a new update processor chain named "dedupe" is created with SignatureUpdateProcessorFactory
, LogUpdateProcessorFactory
and RunUpdateProcessorFactory
in the chain.
The SignatureUpdateProcessorFactory
is further configured with different parameters such as "signatureField", "overwriteDupes", etc.
This chain is an example of how Solr can be configured to perform de-duplication of documents by calculating a signature using the value of name, features, cat fields which is then used as the "id" field.
As you may have noticed, this chain does not specify the DistributedUpdateProcessorFactory
.
Because this processor is critical for Solr to operate properly, Solr will automatically insert DistributedUpdateProcessorFactory
in any chain that does not include it just prior to the RunUpdateProcessorFactory
.
RunUpdateProcessorFactory
Do not forget to add |
Configuring Individual Processors as Top-Level Plugins
Update request processors can also be configured independent of a chain in solrconfig.xml
.
<updateProcessor class="solr.processor.SignatureUpdateProcessorFactory" name="signature">
<bool name="enabled">true</bool>
<str name="signatureField">id</str>
<bool name="overwriteDupes">false</bool>
<str name="fields">name,features,cat</str>
<str name="signatureClass">solr.processor.Lookup3Signature</str>
</updateProcessor>
<updateProcessor class="solr.RemoveBlankFieldUpdateProcessorFactory" name="remove_blanks"/>
In this case, an instance of SignatureUpdateProcessorFactory
is configured with the name "signature" and a RemoveBlankFieldUpdateProcessorFactory
is defined with the name "remove_blanks".
Once the above has been specified in solrconfig.xml
, we can be refer to them in update request processor chains in solrconfig.xml
as follows:
<updateProcessorChain name="custom" processor="remove_blanks,signature">
<processor class="solr.RunUpdateProcessorFactory" />
</updateProcessorChain>
Update Processors in SolrCloud
In a user-managed cluster or a single-node installation, each update is run through all the update processors in a chain exactly once. In SolrCloud, however, the behavior of update request processors deserves special consideration.
A critical SolrCloud functionality is the routing and distributing of requests.
For update requests this routing is implemented by the DistributedUpdateRequestProcessor
, and this processor is given a special status by Solr due to its important function.
In SolrCloud clusters, all processors in the chain before the DistributedUpdateProcessor
are run on the first node that receives an update from the client, regardless of this node’s status as a leader or replica.
The DistributedUpdateProcessor
then forwards the update to the appropriate shard leader for the update (or to multiple leaders in the event of an update that affects multiple documents, such as a delete by query or commit).
The shard leader uses a transaction log to apply Partial Document Updates and then forwards the update to all of the shard replicas.
The leader and each replica run all of the processors in the chain that are listed after the DistributedUpdateProcessor
.
For example, consider the "dedupe" chain which we saw in a section above. Assume that a 3-node SolrCloud cluster exists where node A hosts the leader of shard1, node B hosts the leader of shard2 and node C hosts a NRT-type replica of shard2. Assume that an update request is sent to node A which forwards the update to node B (because the update belongs to shard2) which then distributes the update to its replica node C. Let’s see what happens at each node:
-
Node A: Runs the update through the
SignatureUpdateProcessor
(which computes the signature and puts it in the "id" field), thenLogUpdateProcessor
and thenDistributedUpdateProcessor
. This processor determines that the update actually belongs to node B and is forwarded to node B. The update is not processed further. This is required because the next processor,RunUpdateProcessor
, will execute the update against the local shard1 index which would lead to duplicate data on shard1 and shard2. -
Node B: Receives the update and sees that it was forwarded by another node. The update is directly sent to
DistributedUpdateProcessor
because it has already been through theSignatureUpdateProcessor
on node A and doing the same signature computation again would be redundant. TheDistributedUpdateProcessor
determines that the update indeed belongs to this node, distributes it to its replica on Node C and then forwards the update further in the chain toRunUpdateProcessor
. -
Node C: Receives the update and sees that it was distributed by its leader. The update is directly sent to
DistributedUpdateProcessor
which performs some consistency checks and forwards the update further in the chain toRunUpdateProcessor
.
In summary:
-
All processors before
DistributedUpdateProcessor
are only run on the first node that receives an update request whether it be a forwarding node (e.g., node A in the above example) or a leader (e.g., node B). We call these "pre-processors" or just "processors". -
All processors after
DistributedUpdateProcessor
run only on the leader and the replica nodes. They are not executed on forwarding nodes. Such processors are called "post-processors".
In the previous section, we saw that the updateRequestProcessorChain
was configured with processor="remove_blanks, signature"
.
This means that such processors are of the #1 kind and are run only on the forwarding nodes.
Similarly, we can configure them as the #2 kind by specifying with the attribute "post-processor" as follows:
<updateProcessorChain name="custom" processor="signature" post-processor="remove_blanks">
<processor class="solr.RunUpdateProcessorFactory" />
</updateProcessorChain>
However executing a processor only on the forwarding nodes is a great way of distributing an expensive computation such as de-duplication across a SolrCloud cluster by sending requests randomly via a load balancer. Otherwise the expensive computation is repeated on both the leader and replica nodes.
Custom update chain post-processors may never be invoked on a recovering replica
While a replica is in recovery, inbound update requests are buffered to the transaction log. After recovery has completed successfully, those buffered update requests are replayed. As of this writing, however, custom update chain post-processors are never invoked for buffered update requests. See SOLR-8030. To work around this problem until SOLR-8030 has been fixed, avoid specifying post-processors in custom update chains. |
Atomic Update Processor Factory
If the AtomicUpdateProcessorFactory
is in the update chain before the DistributedUpdateProcessor
, the incoming document to the chain will be a partial document.
Because DistributedUpdateProcessor
is responsible for processing atomic updates into full documents on the leader node, this means that pre-processors which are executed only on the forwarding nodes can only operate on the partial document.
If you have a processor which must process a full document then the only choice is to specify it as a post-processor.
Using Custom Chains
update.chain Request Parameter
The update.chain
parameter can be used in any update request to choose a custom chain which has been configured in solrconfig.xml
.
For example, in order to choose the "dedupe" chain described in a previous section, one can issue the following request:
curl "http://localhost:8983/solr/gettingstarted/update/json?update.chain=dedupe&commit=true" -H 'Content-type: application/json' -d '
[
{
"name" : "The Lightning Thief",
"features" : "This is just a test",
"cat" : ["book","hardcover"]
},
{
"name" : "The Lightning Thief",
"features" : "This is just a test",
"cat" : ["book","hardcover"]
}
]'
The above should dedupe the two identical documents and index only one of them.
Processor & Post-Processor Request Parameters
We can dynamically construct a custom update request processor chain using the processor
and post-processor
request parameters.
Multiple processors can be specified as a comma-separated value for these two parameters.
For example:
curl "http://localhost:8983/solr/gettingstarted/update/json?processor=remove_blanks,signature&commit=true" -H 'Content-type: application/json' -d '
[
{
"name" : "The Lightning Thief",
"features" : "This is just a test",
"cat" : ["book","hardcover"]
},
{
"name" : "The Lightning Thief",
"features" : "This is just a test",
"cat" : ["book","hardcover"]
}
]'
curl "http://localhost:8983/solr/gettingstarted/update/json?processor=remove_blanks&post-processor=signature&commit=true" -H 'Content-type: application/json' -d '
[
{
"name" : "The Lightning Thief",
"features" : "This is just a test",
"cat" : ["book","hardcover"]
},
{
"name" : "The Lightning Thief",
"features" : "This is just a test",
"cat" : ["book","hardcover"]
}
]'
In the first example, Solr will dynamically create a chain which has "signature" and "remove_blanks" as pre-processors to be executed only on the forwarding node where as in the second example, "remove_blanks" will be executed as a pre-processor and "signature" will be executed on the leader and replicas as a post-processor.
Configuring a Custom Chain as a Default
We can also specify a custom chain to be used by default for all requests sent to specific update handlers instead of specifying the names in request parameters for each request.
This can be done by adding either "update.chain" or "processor" and "post-processor" as default parameter for a given path which can be done either via InitParams or by adding them in a "defaults" section which is supported by all request handlers.
The following is an initParam
defined in the Schemaless Mode which applies a custom update chain to all request handlers starting with "/update/".
<initParams path="/update/**">
<lst name="defaults">
<str name="update.chain">add-unknown-fields-to-the-schema</str>
</lst>
</initParams>
Alternately, one can achieve a similar effect using the "defaults" as shown in the example below:
<requestHandler name="/update/extract" startup="lazy" class="solr.extraction.ExtractingRequestHandler" >
<lst name="defaults">
<str name="update.chain">add-unknown-fields-to-the-schema</str>
</lst>
</requestHandler>
Update Request Processor Factories
What follows are brief descriptions of the currently available update request processors.
An UpdateRequestProcessorFactory
can be integrated into an update chain in solrconfig.xml
as necessary.
You are strongly urged to examine the Javadocs for these classes; these descriptions are abridged snippets taken for the most part from the Javadocs.
General Use UpdateProcessorFactories
- AddSchemaFieldsUpdateProcessorFactory
-
This processor will dynamically add fields to the schema if an input document contains one or more fields that don’t match any field or dynamic field in the schema.
- AtomicUpdateProcessorFactory
-
This processor will convert conventional field-value documents to atomic update documents. This processor can be used at runtime (without defining it in
solrconfig.xml
), see the section AtomicUpdateProcessorFactory below. - ClassificationUpdateProcessorFactory
-
This processor uses Lucene’s classification module to provide simple document classification. See https://cwiki.apache.org/confluence/display/solr/SolrClassification for more details on how to use this processor.
- CloneFieldUpdateProcessorFactory
-
Clones the values found in any matching source field into the configured dest field.
- DefaultValueUpdateProcessorFactory
-
A simple processor that adds a default value to any document which does not already have a value in fieldName.
- DocBasedVersionConstraintsProcessorFactory
-
This Factory generates an UpdateProcessor that helps to enforce version constraints on documents based on per-document version numbers using a configured name of a versionField.
- DocExpirationUpdateProcessorFactory
-
Update Processor Factory for managing automatic "expiration" of documents.
- FieldNameMutatingUpdateProcessorFactory
-
Modifies field names by replacing all matches to the configured
pattern
with the configuredreplacement
. - IgnoreCommitOptimizeUpdateProcessorFactory
-
Allows you to ignore commit and/or optimize requests from client applications when running in SolrCloud mode, for more information, see: Shards and Indexing Data in SolrCloud
- IgnoreLargeDocumentProcessorFactory
-
Allows you to prevent large documents with size more than
limit
(in KB) from getting indexed. It can help to prevent unexpected problems on indexing as well as on recovering because of very large documents.By default, this processor will abort the update request and send an error back to users if it encounters a document that exceed its configured limit. Documents processed prior to the offender are indexed by Solr; documents following the offender are left unprocessed.
Alternatively, the processor offers a "permissive" mode (
permissiveMode=true
) which skips the offending document and logs a warning, but doesn’t abort the remainder of the batch or return an error to users. - RegexpBoostProcessorFactory
-
A processor which will match content of "inputField" against regular expressions found in "boostFilename", and if it matches will return the corresponding boost value from the file and output this to "boostField" as a double value.
- SignatureUpdateProcessorFactory
-
Uses a defined set of fields to generate a hash "signature" for the document. Useful for only indexing one copy of "similar" documents.
- ScriptUpdateProcessorFactory
-
A processor that enables the use of update processors implemented as scripts. Learn more in the section Script Update Processor.
- TemplateUpdateProcessorFactory
-
Allows adding new fields to documents based on a template pattern. This update processor can also be used at runtime (without defining it in
solrconfig.xml
), see the section TemplateUpdateProcessorFactory below. - TimestampUpdateProcessorFactory
-
An update processor that adds a newly generated date value of "NOW" to any document being added that does not already have a value in the specified field.
- URLClassifyProcessorFactory
-
Update processor which examines a URL and outputs to various other fields with characteristics of that URL, including length, number of path levels, whether it is a top level URL (levels==0), whether it looks like a landing/index page, a canonical representation of the URL (e.g., stripping index.html), the domain and path parts of the URL, etc.
- UUIDUpdateProcessorFactory
-
An update processor that adds a newly generated UUID value to any document being added that does not already have a value in the specified field. This processor can also be used at runtime (without defining it in
solrconfig.xml
), see the section UUIDUpdateProcessorFactory below.
FieldMutatingUpdateProcessorFactory Derived Factories
These factories all provide functionality to modify fields in a document as they’re being indexed. When using any of these factories, please consult the FieldMutatingUpdateProcessorFactory javadocs for details on the common options they all support for configuring which fields are modified.
- ConcatFieldUpdateProcessorFactory
-
Concatenates multiple values for fields matching the specified conditions using a configurable delimiter.
- CountFieldValuesUpdateProcessorFactory
-
Replaces any list of values for a field matching the specified conditions with the count of the number of values for that field.
- FieldLengthUpdateProcessorFactory
-
Replaces any CharSequence values found in fields matching the specified conditions with the lengths of those CharSequences (as an Integer).
- FirstFieldValueUpdateProcessorFactory
-
Keeps only the first value of fields matching the specified conditions.
- HTMLStripFieldUpdateProcessorFactory
-
Strips all HTML Markup in any CharSequence values found in fields matching the specified conditions.
- IgnoreFieldUpdateProcessorFactory
-
Ignores and removes fields matching the specified conditions from any document being added to the index.
- LastFieldValueUpdateProcessorFactory
-
Keeps only the last value of fields matching the specified conditions.
- MaxFieldValueUpdateProcessorFactory
-
An update processor that keeps only the maximum value from any selected fields where multiple values are found.
- MinFieldValueUpdateProcessorFactory
-
An update processor that keeps only the minimum value from any selected fields where multiple values are found.
- ParseBooleanFieldUpdateProcessorFactory
-
Attempts to mutate selected fields that have only CharSequence-typed values into Boolean values.
- ParseDateFieldUpdateProcessorFactory
-
Attempts to mutate selected fields that have only CharSequence-typed values into Date values.
- ParseNumericFieldUpdateProcessorFactory derived classes
-
- ParseDoubleFieldUpdateProcessorFactory
-
Attempts to mutate selected fields that have only CharSequence-typed values into Double values.
- ParseFloatFieldUpdateProcessorFactory
-
Attempts to mutate selected fields that have only CharSequence-typed values into Float values.
- ParseIntFieldUpdateProcessorFactory
-
Attempts to mutate selected fields that have only CharSequence-typed values into Integer values.
- ParseLongFieldUpdateProcessorFactory
-
Attempts to mutate selected fields that have only CharSequence-typed values into Long values.
- PreAnalyzedUpdateProcessorFactory
-
An update processor that parses configured fields of any document being added using PreAnalyzedField with the configured format parser.
- RegexReplaceProcessorFactory
-
An updated processor that applies a configured regex to any CharSequence values found in the selected fields, and replaces any matches with the configured replacement string.
- RemoveBlankFieldUpdateProcessorFactory
-
Removes any values found which are CharSequence with a length of 0 (i.e., empty strings).
- TrimFieldUpdateProcessorFactory
-
Trims leading and trailing whitespace from any CharSequence values found in fields matching the specified conditions.
- TruncateFieldUpdateProcessorFactory
-
Truncates any CharSequence values found in fields matching the specified conditions to a maximum character length.
- UniqFieldsUpdateProcessorFactory
-
Removes duplicate values found in fields matching the specified conditions.
Update Processor Factories That Can Be Loaded as Plugins
These processors are included in Solr releases as "module", and require additional jars loaded at runtime. See the README files associated with each module for details:
- The
langid
module provides -
- LangDetectLanguageIdentifierUpdateProcessorFactory
-
Identifies the language of a set of input fields using http://code.google.com/p/language-detection.
- TikaLanguageIdentifierUpdateProcessorFactory
-
Identifies the language of a set of input fields using Tika’s LanguageIdentifier.
- The
analysis-extras
module provides -
- OpenNLPExtractNamedEntitiesUpdateProcessorFactory
-
Update document(s) to be indexed with named entities extracted using an OpenNLP NER model. Note that in order to use model files larger than 1MB on SolrCloud, you must either configure both ZooKeeper server and clients or store the model files on the filesystem on each node hosting a collection replica.
Update Processor Factories You Should Not Modify or Remove
These are listed for completeness, but are part of the Solr infrastructure, particularly SolrCloud. Other than insuring you do not remove them when modifying the update request handlers (or any copies you make), you will rarely, if ever, need to change these.
- DistributedUpdateProcessorFactory
-
Used to distribute updates to all necessary nodes.
- NoOpDistributingUpdateProcessorFactory
-
An alternative No-Op implementation of
DistributingUpdateProcessorFactory
that always returns null. Designed for experts who want to bypass distributed updates and use their own custom update logic.
- LogUpdateProcessorFactory
-
A logging processor. This keeps track of all commands that have passed through the chain and prints them on finish().
- RunUpdateProcessorFactory
-
Executes the update commands using the underlying UpdateHandler. Almost all processor chains should end with an instance of
RunUpdateProcessorFactory
unless the user is explicitly executing the update commands in an alternative customUpdateRequestProcessorFactory
.
Update Processors That Can Be Used at Runtime
These Update processors do not need any configuration in solrconfig.xml
.
They are automatically initialized when their name is added to the processor
parameter sent with an update request.
Multiple processors can be used by appending multiple processor names separated by commas.
AtomicUpdateProcessorFactory
The AtomicUpdateProcessorFactory
is used to atomically update documents.
Use the parameter processor=atomic
to invoke it.
Use it to convert your normal update
operations to atomic update operations.
This is particularly useful when you use endpoints such as /update/csv
or /update/json/docs
which does not otherwise have a syntax for atomic operations.
For example:
processor=atomic&atomic.field1=add&atomic.field2=set&atomic.field3=inc&atomic.field4=remove&atomic.field4=remove
The above parameters convert a normal update
operation in the following ways:
-
field1
to an atomicadd
operation -
field2
to an atomicset
operation -
field3
to an atomicinc
operation -
field4
to an atomicremove
operation
TemplateUpdateProcessorFactory
The TemplateUpdateProcessorFactory
can be used to add new fields to documents based on a template pattern.
Use the parameter processor=template
to use it.
The template parameter template.field
(multivalued) defines the field to add and the pattern.
Templates may contain placeholders which refer to other fields in the document.
You can have multiple Template.field
parameters in a single request.
For example:
processor=template&template.field=fullName:Mr. {firstName} {lastName}
The above example would add a new field to the document called fullName
.
The fields firstName and
lastName
are supplied from the document fields.
If either of them is missing, that part is replaced with an empty string.
If those fields are multi-valued, only the first value is used.
UUIDUpdateProcessorFactory
The UUIDUpdateProcessorFactory
is used to add generated UUIDs to documents.
Use the parameter processor=uuid
to invoke it.
You will also need to specify the field where the UUID will be added with the uuid.fieldName
parameter.
For example:
processor=uuid&uuid.fieldName=somefield_name