11

is there possibility to update a new field to an existing document? For example: There is an document with several fields, e.g.

ID=99999
Field1:text
Field2:text

This document is already in the index, now I want to insert a new field to this document WITHOUT the old data:

ID=99999
Field3:text

For now, the old document will be deleted and a new document with the ID will be created. So if I now search for the ID 99999 the result will be:

ID=99999
Field3:text

I read this at the Solr Wiki

How can I update a specific field of an existing document?

I want update a specific field in a document, is that possible? I only need to index one field for >a specific document. Do I have to index all the document for this?

No, just the one document. Let's say you have a CMS and you edit one document. You will need to re-index this document only by using the the add solr statement for the whole document (not one field only).

In Lucene to update a document the operation is really a delete followed by an add. You will need >to add the complete document as there is no such "update only a field" semantics in Lucene.

So is there any solution for this? Will this function be implemented in a further version (I currently use 3.6.0). As a workaround, I thought about writing a script or an application, which will collect the existing fields, add the new field and update the whole document. But I think this will suffer performance. Do you have any other ideas?

Best regards

3 Answers 3

10

I have 2 answers for you (both more or less bad):

  1. To update filed with in document in Solr you have to reindex whole document (to update Field3 within document ID:99999 you have to reindex that document with values for all fields)
  2. In Solr 4 they implemented feature like that, but they have a condition: all fields have to be stored, not just indexed. What is happening that is they are using stored values and reindexing document in the background. If you are interested, there is nice article about it: http://solr.pl/en/2012/07/09/solr-4-0-partial-documents-update/ This solution have obvious flaw and that is size of index when you are storing all fields.

I hope that this will help you with your problem. If you have some more questions, please ask

6
  • Thanks a lot! Your first answer is just what i thought about. Do you think this will highly suffer performance? For answer 2: Can you give me an official link with this changelog? I wasn´t able to find this feature. Nice link :-) Aug 3, 2012 at 9:27
  • Depends how often you are making changes, how big is your index, how big are the documents, what hardware are you using. As any change it costs IO, CPU etc, you would have to experiment how much in your case. For the second part I cant find it in the log: svn.apache.org/viewvc/lucene/dev/trunk/solr/… however Yonik Seeley is confirming that in grokbase.com/t/lucene/solr-user/127bc3svh7/updating-documents
    – Fuxi
    Aug 3, 2012 at 10:12
  • My index contains about 1,8million documents, each document has about 10-30 multivalued fields. Problem is, that the development of the application will cost too much time, if I can´t use it due high performance loss. Is it recommended to change the version to the 4.0 alpha version? Do you have any experience? Thanks a lot :-) Aug 3, 2012 at 10:28
  • 1
    I just started playing with 4.0 alpha at home, according to the schedule, beta version can be published very soon (wiki.apache.org/solr/Solr4.0). How often do you want to change the values, do you want to have them available at once? You can optimize index once in a while, not with every commit, that could save you some performance. You also can have master-slave configuration in balance mode, but when you are updating something query only slave. There are more possibilities. Bottom line is unless you try you will not know for sure.
    – Fuxi
    Aug 3, 2012 at 10:44
  • 1
    If I where you, I would create load test. Use your index (or copy of it), write script that will query the solr in your usual rate, then fire the second one that is updating fields/reindexing single docs in rate that you are expecting. Then you will know where you are standing. And I should not cost you more than a day or 2 of work
    – Fuxi
    Aug 3, 2012 at 10:48
9

It is possible to do this in Solr 4. E.g. Consider the following document

{
 "id": "book123",
 "name" : "Solr Rocks"
}

In order to add an author field to the document the field value would be a json object with "set" attribute and the field value

$ curl http://localhost:8983/solr/update -H 'Content-type:application/json' -d '
[
 {"id"       : "book123",
  "author"   : {"set":"The Community"}
 }
]'

Your new document

$ curl http://localhost:8983/solr/get?id=book123

will be

{
 "doc" : {
    "id"    : "book123",
    "name"  : "Solr Rocks"
    "author": "The Community"
 }
}

Set will add or replace the author field. Along with set you also have the option to increment(inc) and adding(add)

1
  • I want to update the document on author field not using ID field. How can I do that?
    – iNikkz
    Jan 20, 2015 at 8:26
3

From Solr 4 onwards you can update a field in solr ....no need to reindex the entire indexes .... various modifiers are supported like ....

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 remove – removes a value (or a list of values) from a list removeregex – removes from a list that match the given Java regular expression inc – increments a numeric value by a specific amount (use a negative value to decrement)

example :

document

{
 "id": "1",
 "name" : "Solr"
 "views" : "2"
}

now update with

$ curl http://localhost:8983/solr/demo/update -d '
[
 {"id"         : "1",
  "author"   : {"set":"Neal Stephenson"},
  "views"   : {"inc":3},
  }
]' 

will result into

{
 "id": "1",
 "name" : "Solr"
 "views" : "5"
 "author" : "Neal Stephenson"
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.