Python操作Solr索引库
增加/更新(overwrite=True)
# coding=utf-8 import requests data = {"add":{ "doc":{"id":"10001","item_title":"我是一个大好人"}}} # 要添加的数据 params = {"boost":1.0,"overwrite":"true","commitWithin":1000} # overwrite:如果solr中已经有这个id的数据,将其覆盖 url = 'http://127.0.0.1:8080/solr/collection1/update?wt=json' # 请求url headers = {"Content-Type": "application/json"} # 请求头(表示内容是json格式的) r = requests.post(url, json = data, params = params, headers = headers) # 发送请求 print(r.text) # {"responseHeader":{"status":0,"QTime":2}}
查询
import requests item = "我是" # 搜索字段 url = 'http://127.0.0.1:8080/solr/collection1/select?q=item_title:"\%s"&wt=json&indent=true'%item # 请求url # verify=True -->检查某个主机的SSL证书 r = requests.get(url, verify = False) # 发送请求,相应:r = <Response [200]> r = r.json() # 将其转成json格式 print(r) """ { 'responseHeader': {'QTime': 1, 'status': 0, 'params': {'q': 'item_title:"\\我"', 'indent': 'true', 'wt': 'json'}}, 'response': {'numFound': 1, 'start': 0, 'docs': [{'_version_': 1597870382723891200, 'id': '100001', 'item_title': '我是一个大好人'}]} } numFound --> 查到多少条相关数据 docs --> 返回的查询集(列表) """