Sheldon Cooper
Sheldon Cooper

Reputation: 246

Solr Update If Record Exists

curl SOLR_URL/update -d \'
[
 {"id" : "1",
  "ONLINE" : {"set":"1"}
 }
]'

I am using solr6.3. Above command works fine as it update online flag to 1 for id=1. But the issue is if record is not present then it adds a value as id=1 and online=1 which is not desired.

So question is, is it possible that solr updates the value only if record is present in the solr.

Upvotes: 0

Views: 614

Answers (2)

Dximo44
Dximo44

Reputation: 1

To make sure that a document exists for update, you can simply add the _version_ field with the value 1 to the document. In this case the version is not evaluated but only interpreted that the document must exist.

[
 {"id" : "146546456",
  "ONLINE" : {"set": "1"},
  "_version_" : 1
 }
]

The error message is correspondingly meaningful:

Document not found for update. id=146546456

However, a batch update would never update a document if only one does not exist.

Upvotes: 0

MrSunshine
MrSunshine

Reputation: 31

Maybe a little bit late but you could create a required field. If you try to insert a document without this required field the update query fails. Maybe not the most elegant way but at least one solution.

Upvotes: 1

Related Questions