Christian Lendel
Christian Lendel

Reputation: 420

Update a new field to existing document

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

Upvotes: 11

Views: 28627

Answers (3)

Fuxi
Fuxi

Reputation: 5508

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

Upvotes: 10

kanishka vatsa
kanishka vatsa

Reputation: 2334

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"
}

Upvotes: 3

coderman
coderman

Reputation: 1515

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)

Upvotes: 9

Related Questions