4

更新: Pysolr 版本:3.2.0

这似乎是 solr 中的一个错误。在操作中不更新任何内容时,它将删除此文档。

以前我在 atomic update 中使用 pysolr 时使用了代码,但在以下情况下我犯了错误。

现在一个文档模式可能是这样的:

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 的正确方法是什么?

4

1 回答 1

6

我遇到了同样的问题(python-3.6、pysolr-3.6、solr 6.4.1)。由于我在网上找不到更多信息,因此我使用了 requests 解决方法,我会留在这里,以防它对任何人有用。

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)
于 2017-05-02T15:13:30.920 回答