python操作solr
solr接收http請求,所以使用requests庫操作solr就可以
添加
data = {"add": {"doc": params, "commitWithin": 1000}} headers = {"Content-type": "application/json"} re = requests.post(url=url, data=json.dumps(data), headers=headers)
查詢
params = list() params.append(('q', "id:" + id)) response = requests.post(url=url, data=params)
很多時候請求文本存在特殊字符,所以需要轉義(或者用括號括起來)
def escape_query_chars(s): escape_chars = set( ('\', '+', '-', '!', '(', ')', ':', '^', '[', ']', '"', '{', '}', '~', '*', '?', '|', '&', '', '/', ' ','t')) arr = [c for c in s if (c not in escape_chars)] return "".join(arr).replace("xe3x80x80", "")