Atomic Updates

Solr supports simple atomic updates to single documents via field modifiers such as add and inc.

Optimistic concurrency control is another method of atomically updating documents.

Modifiers

Solr supports several modifiers that atomically update values of a document.

  • set – set or replace a particular value, or remove the value if null is specified as the new value
  • add – adds an additional value to a list
  • inc – increments a numeric value by a specific amount

Note: All original source fields must be stored for field modifiers to work correctly.  This is the default in Solr.

Update Modifier Example

First, let’s add a document representing a book:

$ curl http://localhost:8983/solr/update -H 'Content-type:application/json' -d '
[
 {"id" : "book1", "title" : "Snow Crash",
  "copies_i" : 5, "cat" : "Science Fiction"
 }
]'

Now we can update that document, adding the author field, incrementing the number of copies we have, and adding an additional category:

$ curl http://localhost:8983/solr/update -H 'Content-type:application/json' -d '
[
 {"id"       : "book1",
  "author"   : {"set":"Neal Stephenson"},
  "copies_i" : {"inc":3},
  "cat"      : {"add":"Cyberpunk"}
 }
]'

Now if we retrieve the document, we will see the updated fields:

$ curl http://localhost:8983/solr/get?id=book1
{
  "doc": {
    "id":"book1",
    "title":["Snow Crash"],
    "copies_i":8,
    "cat":["Science Fiction", "Cyberpunk"],
    "author":"Neal Stephenson",
    "_version_":1408729977723027456}}