【问题标题】:pysolr update document with errorpysolr 更新文档出错
【发布时间】:2017-03-30 15:49:14
【问题描述】:

更新: Pysolr 版本:3.2.0

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

【问题讨论】:

    标签: python-2.7 pysolr


    【解决方案1】:

    我遇到了同样的问题(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)
    

    【讨论】:

      猜你喜欢
      • 2018-03-04
      • 1970-01-01
      • 1970-01-01
      • 2018-12-22
      • 2017-07-18
      • 2012-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多