Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use SolrJ to send documents from Java to a Solr collection: build a SolrInputDocument, populate its fields, and call SolrClient.add(). To update an existing record, either submit a replacement with the same schema unique key or send an atomic update for selected fields. Choose a commit strategy separately: a successful write does not necessarily make the document immediately searchable.

Set up SolrJ and connect to Solr

SolrJ is Apache Solr’s Java client API. The Solr 10.0 Reference Guide documents the Maven dependency as org.apache.solr:solr-solrj:10.0.0. Match the client library to the Solr release you deploy; do not assume this version is compatible with every Solr installation. See the SolrJ guide for the documented client options and examples.

Use a SolrClient to issue requests. For SolrCloud routing, the Solr 10.0 guide lists CloudSolrClient; it also documents ConcurrentUpdateJettySolrClient for indexing-focused workloads with internal buffering, as well as HTTP clients for direct communication. Pick the client for the deployment and workload rather than treating these options as interchangeable.

Add a document from Java

A Solr document consists of fields that must be accepted by the target collection’s schema. The basic SolrJ flow is to create a SolrInputDocument, set its fields, and pass it to the client’s add method:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "book-123");
doc.addField("title", "A Solr example");
doc.addField("author", "A. Writer");

UpdateResponse response = client.add("catalog", doc);
// Use the deployment's chosen commit/visibility strategy.

Here, catalog is the collection name and id must match the collection’s configured unique key. The call submits an update; it does not by itself decide when the new content becomes visible to searchers.

Index Java beans instead

SolrJ also supports bean mapping with the @Field annotation and client.addBean(collection, bean). This can reduce document-construction code when indexing domain objects, but the annotated Java fields still need to map to fields accepted by the collection schema. The annotation does not configure the Solr schema for you.

Choose the right kind of update

“Update” can mean replacing a whole document or changing only selected fields. The right choice depends on whether the application has a complete, current record or only a small change to apply.

Update approach What it changes Important behavior
Add or replace by unique key The submitted document’s full field set With the default overwrite behavior, a matching unique key replaces the previous document.
Atomic partial update Fields named in the update, using modifiers such as set or inc A regular atomic update internally reindexes the entire document; a restricted in-place optimization is available only for eligible fields.
Optimistic concurrency A replacement or update conditioned on the version read earlier Prevents silently applying an edit against an intervening change; a version mismatch returns HTTP 409.

Replace a document by its unique key

Submitting a document with an existing unique key replaces that document by default. This is appropriate when the application has the full record to write. Avoid setting overwrite=false unless the ingestion design guarantees that duplicate IDs cannot occur: disabling the check can allow documents with duplicate keys.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Change selected fields with atomic updates

For a limited change, Solr atomic updates accept modifiers including set, add, remove, add-distinct, and numeric inc operations. For example, an update can set a product’s price and increment its popularity without the caller resending every field. The modifier names are part of the update document sent to Solr; ensure the target fields and values conform to the schema.

Do not assume that a partial update means Solr only touches the changed field internally. A regular atomic update reindexes the document. Solr’s in-place optimization applies only to a narrower set of cases: eligible fields must be single-valued numeric fields with docValues, and must be neither indexed nor stored. The documented constraints also apply to _version_ and copy-field targets. Check the Partial Document Updates guide before designing around in-place updates.

Protect updates from concurrent writers

If another process may edit a document between your read and write, an unconditional update can overwrite its intervening change. Solr’s optimistic concurrency mechanism uses the document’s _version_ value as an expected version.

  1. Read the current document and its _version_, for example through Solr’s /get handler.
  2. Apply the intended local change to that version of the document.
  3. Submit the update with the expected _version_.
  4. If Solr returns HTTP 409 for a version conflict, reread the latest document and retry or resolve the conflict according to the application’s rules.

Solr automatically adds _version_ under the default schema and reserves it for versioning and SolrCloud update distribution; do not repurpose it. In a batched operation, one version conflict can reject the entire batch. Where individual conflicts should be skipped instead, the guide documents failOnVersionConflicts=false.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Decide when writes become searchable

Solr’s commit behavior separates accepting an update from making it visible to searchers. A hard commit flushes data to stable storage; a soft commit makes updates visible sooner without waiting for the same storage and background-merge work. Auto-commit can be configured around a maximum document count, elapsed time, or transaction-log size. Auto-soft-commit controls the cadence of search visibility.

You can also request commitWithin for an update. The appropriate interval depends on how fresh search results must be and the performance costs the application can accept; shorter visibility intervals can reduce indexing efficiency. The Solr 10.0 commits and transaction logs guide gives 60-second hard-commit and 10-second soft-commit values as examples, not defaults.

The SolrJ indexing example says, “Indexed documents must be committed,” but the guide explicitly presents its short example for syntax and says it breaks best practices. For ordinary applications, it recommends batching documents and generally favors administrator-configured auto-commit over a client commit after every document. Set the visibility and durability policy for the deployment rather than adding commit() after each call.

Delete documents when needed

Solr update handlers support deletion either by a document’s unique ID or by a query matching documents to remove. Delete by ID relies on the schema’s unique key; delete by query removes all documents matching the submitted query. The guide notes that some query parsers impose restrictions and that commitWithin is ignored for delete-by-query. SolrJ provides delete operations and request objects for calling Solr APIs; see the update handlers guide and client APIs guide for operation details.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.