Apache Solr 6.6 replace document instead of update

2019-08-05 06:18发布

问题:

I have configured solr 6.6.1 for a testing setup. After indexing few documents, I have to update few fields. I am using python client of solr. To update, following is my code snippet

import solr

def update_solr_index(_id, _value):
    print solr_conn2.add( id = _id, group2 = _value)

core_ulr = "http://localhost:8983/solr/use"
solr_conn2 = solr.SolrConnection(core_ulr)
update_solr_index(doc_id, field_value)

After execution (and commit), all other fields are removed from all documents and only two fields left that are doc_id and group2. What is the problem is this API or in my code?

回答1:

There is no problem in the API. You are asking solr to add a document with an ID field that is already present in your collection. The following actions happen in solr.

  • Remove the existing document which has the same ID

  • Add new document with only the fields specified in that API call

The solution you are looking for is a partial update of a document.

Refer to the reference link to understand more on this Atomic updates

You need to send a map object for the field you want to update in your document.

solr_conn2.add(id=_id, group2=*****)

The value to group2 must be a map like this {'set':'value...'}

You can probably refer to this similar stackoverflow solution similar answer