2016-11-16 115 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)