2016-11-16 97 views
2

更新: Pysolr版本:3.2.0pysolr更新文档

这似乎Solr中的错误。当在操作中没有更新时,它将删除该文档。

前面我使用using pysolr in atomic update中的代码,但是我在以下情况下发生了错误。

现在文档模式,也许是这样的:

doc = { 
    'id': ..., 
    'title': ..., 
    'body': ..., 
} 

我已经收录了一批文件,现在我想更新一个新的领域anchor_text每个文档。这里是我的代码:

solr = pysolr.Solr(url_solr) 
doc_update = { 
    'id': ..., 
    'anchor_text': [a,b,c,...] 
} 
solr.add([doc_update], fieldUpdates={ 
    'anchor_text': 'set' 
}) 

但是我发现了一些原有的文档被删除ID场左侧。 事情是这样的更新后:

doc = { 
    'id':... 
} 

特别是对于那些anchor_text场都是空的名单,原来的文档被删除。而其他人不是。(可能我猜是因为我只看到几个案例)。

我查看了源代码,但没有发现有价值的东西。这里发生了什么?

什么是在更新文档中使用pysolr的正确方法?

回答

1

我遇到了同样的问题(python-3.6,pysolr-3.6,solr 6.4.1)。由于我在网上找不到更多信息,我使用了一个请求解决办法,我会在这里离开,以防其他人使用它。

import requests 
import json 

def update_single_solr_field(doc_id_field, doc_id, field_update_name, field_update_value): 
    # Updates a single field in a document with id 'doc_id'. 
    # Updates only the 'field_update_name' field to the 'field_update_value', leaving other fields intact 

    base_url = 'http://localhost:8983/' 
    solr_url = 'solr/mysolrcore/' 
    update_url = 'update?commit=true' 
    full_url = base_url + solr_url + update_url 
    headers = {'content-type': "application/json"} 

    payload = [{ 
     doc_id_field: doc_id, 
     field_update_name: { 
      'set': field_update_value 
     } 
    }] 

    response = requests.post(full_url, data=json.dumps(payload), headers=headers) 

    return response 

# example 
id_field_name = 'id' 
doc_id_to_update = '1700370208' 
field_to_update = 'weight_field' 
field_to_update_value = 20000 
response_update = update_single_solr_field(id_field_name, doc_id_to_update, field_to_update, field_to_update_value) 

print(response_update)