apache solr的轻量级python包装器。
pysolr的Python项目详细描述
pysolr是Apache Solr的轻量级python包装器。它提供了 查询服务器并根据查询返回结果的接口。
功能
- 基本操作,如选择、更新和删除。
- 索引优化。
- “More Like This”支持(如果在solr中设置)。
- Spelling correction(如果在solr中设置)。
- 超时支持。
- Solrcloud意识
要求
- Python2.7-3.6
- 请求2.9.1+
- 可选-simplejson
- 可选-kazoo用于solrcoud模式
安装
pysolr在pypi上:
$ pip install pysolr
或者如果您想直接从存储库中安装:python setup.py install,或者将pysolr.py文件放在PYTHONPATH中的任何位置。
用法
基本用法如下:
# If on Python 2.Xfrom__future__importprint_functionimportpysolr# Setup a Solr instance. The timeout is optional.solr=pysolr.Solr('http://localhost:8983/solr/',timeout=10,auth=<typeofauthentication>)# How you'd index data.solr.add([{"id":"doc_1","title":"A test document",},{"id":"doc_2","title":"The Banana: Tasty or Dangerous?","_doc":[{"id":"child_doc_1","title":"peel"},{"id":"child_doc_2","title":"seed"},]},])# Note that the add method has commit=True by default, so this is# immediately committed to your index.# You can index a parent/child document relationship by# associating a list of child documents with the special key '_doc'. This# is helpful for queries that join together conditions on children and parent# documents.# Later, searching is easy. In the simple case, just a plain Lucene-style# query is fine.results=solr.search('bananas')# The ``Results`` object stores total results found, by default the top# ten most relevant results and any additional data like# facets/highlighting/spelling/etc.print("Saw {0} result(s).".format(len(results)))# Just loop over it to access the results.forresultinresults:print("The title is '{0}'.".format(result['title']))# For a more advanced query, say involving highlighting, you can pass# additional options to Solr.results=solr.search('bananas',**{'hl':'true','hl.fragsize':10,})# You can also perform More Like This searches, if your Solr is configured# correctly.similar=solr.more_like_this(q='id:doc_2',mltfl='text')# Finally, you can delete either individual documents,solr.delete(id='doc_1')# also in batches...solr.delete(id=['doc_1','doc_2'])# ...or all documents.solr.delete(q='*:*')
# For SolrCloud mode, initialize your Solr like this:zookeeper=pysolr.ZooKeeper("zkhost1:2181,zkhost2:2181,zkhost3:2181")solr=pysolr.SolrCloud(zookeeper,"collection1",auth=<typeofauthentication>)
多核指数
只需将url指向索引核心:
# Setup a Solr instance. The timeout is optional.solr=pysolr.Solr('http://localhost:8983/solr/core_0/',timeout=10)
自定义请求处理程序
# Setup a Solr instance. The trailing slash is optional.solr=pysolr.Solr('http://localhost:8983/solr/core_0/',search_handler='/autocomplete',use_qt_param=False)
如果use_qt_param是True,则处理程序的名称必须与配置的名称完全一致 在solrconfig.xml中,包括前导斜杠(如果有的话)(尽管使用qt参数,前导斜杠不是 索尔的要求)。如果use_qt_param是False(默认值),则前导斜杠和尾随斜杠可以是 省略。
如果未指定search_handler,pysolr将默认为/select。
morelikethis、update、terms等的处理程序都默认为solrconfig.xmlsolr ships中设置的值 使用:mlt,update,terms等。pysolr的Solr类的特定方法(如more_like_this, suggest_terms等)允许Kwarghandler重写该值。这包括search方法。 在search中设置处理程序显式重写search_handler设置(如果有)。
自定义身份验证
# Setup a Solr instance in a kerborized enviornmentfromrequests_kerberosimportHTTPKerberosAuth,OPTIONALkerberos_auth=HTTPKerberosAuth(mutual_authentication=OPTIONAL,sanitize_mutual_error_response=False)solr=pysolr.Solr('http://localhost:8983/solr/',auth=kerberos_auth)
# Setup a CloudSolr instance in a kerborized environmentfromrequests_kerberosimportHTTPKerberosAuth,OPTIONALkerberos_auth=HTTPKerberosAuth(mutual_authentication=OPTIONAL,sanitize_mutual_error_response=False)zookeeper=pysolr.ZooKeeper("zkhost1:2181/solr, zkhost2:2181,...,zkhostN:2181")solr=pysolr.SolrCloud(zookeeper,"collection",auth=kerberos_auth)
如果您的solr服务器运行https
# Setup a Solr instance in an https environmentsolr=pysolr.Solr('http://localhost:8983/solr/',verify=path/to/cert.pem)
# Setup a CloudSolr instance in a kerborized environmentzookeeper=pysolr.ZooKeeper("zkhost1:2181/solr, zkhost2:2181,...,zkhostN:2181")solr=pysolr.SolrCloud(zookeeper,"collection",verify=path/to/cert.perm)
自定义提交策略
# Setup a Solr instance. The trailing slash is optional.# All request to solr will result in a commitsolr=pysolr.Solr('http://localhost:8983/solr/core_0/',search_handler='/autocomplete',always_commit=True)
always_commit向solr对象发送信号,以便在默认情况下为任何solr请求提交或不提交。 如果要从默认策略为alway commit的版本升级,请确保将此值更改为true。
像add和delete这样的函数仍然提供了通过传递commitkwarg来重写默认值的方法。
限制对solr的提交量通常是好的做法。 过度提交有打开过多搜索者或使用过多系统资源的风险。
许可证
pysolr根据新的bsd许可证获得许可。
运行测试
run-tests.py脚本将自动执行以下步骤,建议由 默认设置,除非需要更多控制。
运行测试solr实例
下载、配置和运行solr 4如下:
./start-solr-test-server.sh
运行测试
测试套件需要unittest2库:
Python2:
python -m unittest2 tests
Python3:
python3 -m unittest tests