更新: Pysolr 版本:3.2.0
这似乎是 solr 中的一个错误。在操作中不更新任何内容时,它将删除此文档。
以前我在原子更新中使用 pysolr 时使用了代码,但在以下情况下我犯了错误。
现在文档架构可能是这样的:
doc = {
'id': ...,
'title': ...,
'body': ...,
}
Run Code Online (Sandbox Code Playgroud)
我已经索引了一批文档,现在我想用一个新字段 anchor_text 更新每个文档。这是我的代码:
solr = pysolr.Solr(url_solr)
doc_update = {
'id': ...,
'anchor_text': [a,b,c,...]
}
solr.add([doc_update], fieldUpdates={
'anchor_text': 'set'
})
Run Code Online (Sandbox Code Playgroud)
但我发现一些原始文档仅在留下id字段时被删除。更新后是这样的:
doc = {
'id':...
}
Run Code Online (Sandbox Code Playgroud)
特别是,对于那些其 anchor_text 字段为空列表的人,原始文档被删除。而其他人则不是。(可能我猜是因为我只看到几个案例)。
我查看了源代码,但没有发现任何有价值的内容。这里发生了什么?
在更新文档中使用 pysolr 的正确方法是什么?
我遇到了同样的问题(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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
965 次 |
| 最近记录: |