27

Solr 4 beta is out, the GA version will follow soon. Partial document updates has been around for a while as explained here: http://solr.pl/en/2012/07/09/solr-4-0-partial-documents-update/

However, I haven't figured out how to do it with solrj api.

Does anyone knows if it is possible with solrj? Or is solrj just not up-to-speed with this feature?

update: as I describe in the mailing list (see reply here), I found that in the solrj api, the value of a SolrInputField can be a map - it doesn't have to be a simple scalar value. If it is a map, solrj adds an additional update attribute to the field's xml element. For example, This code:

SolrInputDocument doc = new SolrInputDocument();
Map<String, String> partialUpdate = new HashMap<String, String>();
partialUpdate.put("set", "foo");
doc.addField("id", "test_123");
doc.addField("description", partialUpdate);

yields this document:

<doc boost="1.0">
    <field name="id">test_123</field>
    <field name="description" update="set">foo</field>
</doc>

In this example I used the word "set" for this additional attribute, but it doesn't work. Solr doesn't update the field as I expected. According to this link: http://solr.pl/en/2012/07/09/solr-4-0-partial-documents-update/ valid values are "set" and "add".

Any idea?

Yoni
  • 10,171
  • 9
  • 55
  • 72
  • 1
    This might help you [solr-update][1] [1]: http://stackoverflow.com/questions/8365713/solr-update-specific-fields-rather-than-entire-document – prashant Sep 07 '12 at 18:46
  • @prashant the answer in that question is outdated. There is another answer in a duplicate of that question, but it doesn't help either – Yoni Sep 08 '12 at 14:41
  • you can even insert list of values using `ArrayList` then the `partialUpdate` will be declared as `Map partialUpdate = new HashMap();` and the key as `add` – veer7 Apr 14 '14 at 11:28
  • Upvote for the java code example. Most other examples only have plain JSON. Thanks. – ilinca Jan 15 '15 at 17:00

2 Answers2

31

As it turns out, the code snippet shown above in the question actually works. I don't know what was wrong the first time I tried it, perhaps I simply forgot to commit or my schema was misconfigured.

In any case, this question is very localized. However, since the api with the hash map is so poorly documented, I thought maybe it is worth to keep this question and answer.

The key of the hash map can be one of three values:

  • set - to set a field.
  • add - to add to a multi-valued field.
  • inc - to increment a field.

There is an example of this code in the solrj unit tests, in a method called testUpdateField.

Yoni
  • 10,171
  • 9
  • 55
  • 72
  • 1
    To update a multi-valued field with multiple values, create a `Map>` as in https://issues.apache.org/jira/browse/SOLR-4134?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13537900#comment-13538174 – juanmirocks Dec 21 '12 at 16:20
  • 4
    [link]http://svn.apache.org/repos/asf/lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java may halp someone – Halis Yılboğa Oct 30 '13 at 14:47
  • remove and removeregex are also possible values for the hash https://cwiki.apache.org/confluence/display/solr/Updating+Parts+of+Documents – ilinca Jan 16 '15 at 17:18
  • This only works if the field is 'stored', else you need to submit the entire docment again. https://cwiki.apache.org/confluence/display/solr/Updating+Parts+of+Documents#UpdatingPartsofDocuments-AtomicUpdates – Prashant Saraswat Feb 23 '17 at 03:30
0

You can update parts of documents using the SOLR API's update endpoint

curl 'https://solr-url/update?commitWithin=1000&overwrite=true&wt=json' \
    -X POST \
    -H 'accept: application/json, text/plain, */*' \
    --data-raw '[{ "the-unique-filed": "value", "field-to-change":{"set": "new-value"} }]' \
    --compressed

Or from UI

enter image description here

Umair Ayub
  • 19,358
  • 14
  • 72
  • 146