微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Pysolr Solr 轻量的 Python 应用

程序名称:Pysolr

Pysolr 介绍

Pysolr,是基于Python的Apache
Solr
轻量级封装。它提供了服务器查询并返回基于查询的结果接口。

特性:

  • 基本功能,选择、更新和删除

  • 索引优化

  • 超时支持

使用环境:

  • Python 2.7 - 3.5

  • Requests 2.0+

  • Optional - simplejson

  • Optional - kazoo for SolrCloud mode

示例代码:

# If on Python 2.X
from __future__ import print_function
import pysolr
# Setup a Solr instance. The timeout is optional.
solr = pysolr.Solr('http://localhost:8983/solr/', timeout=10)
# How you'd index data.
solr.add([
    {
        "id": "doc_1",
        "title": "A test document",
    },
    {
        "id": "doc_2",
        "title": "The Banana: Tasty or Dangerous?",
    },
])
# 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.
for result in results:
    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')
# ...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")

Pysolr 官网

https://github.com/django-haystack/pysolr

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐