使用pysolr

时间:2017-11-13 11:53:06

标签: solr apache-zookeeper solrcloud pysolr

我配置了多核solr云。 创建了一个具有2个shrad并且没有复制的集合。 Cloud in the UI of solr

通过solr UI 192.168.1.56:8983,我可以获得查询结果。

我想对pysolr做同样的事情,所以尝试运行以下内容:

import pysolr
zookeeper = pysolr.ZooKeeper("192.168.1.56:2181,192.168.1.55:2182")
solr = pysolr.SolrCloud(zookeeper, "random_collection")

最后一行无法找到该集合,即使它在那里。 以下是错误跟踪:

---------------------------------------------------------------------------
SolrError                                 Traceback (most recent call last)
<ipython-input-43-9f03eca3b645> in <module>()
----> 1 solr = pysolr.SolrCloud(zookeeper, "patent_colllection")

/usr/local/lib/python2.7/dist-packages/pysolr.pyc in __init__(self, zookeeper, collection, decoder, timeout, retry_timeout, *args, **kwargs)
   1176 
   1177     def __init__(self, zookeeper, collection, decoder=None, timeout=60, retry_timeout=0.2, *args, **kwargs):
-> 1178         url = zookeeper.getRandomURL(collection)
   1179 
   1180         super(SolrCloud, self).__init__(url, decoder=decoder, timeout=timeout, *args, **kwargs)

/usr/local/lib/python2.7/dist-packages/pysolr.pyc in getRandomURL(self, collname, only_leader)
   1315 
   1316     def getRandomURL(self, collname, only_leader=False):
-> 1317         hosts = self.getHosts(collname, only_leader=only_leader)
   1318         if not hosts:
   1319             raise SolrError('ZooKeeper returned no active shards!')

/usr/local/lib/python2.7/dist-packages/pysolr.pyc in getHosts(self, collname, only_leader, seen_aliases)
   1281         hosts = []
   1282         if collname not in self.collections:
-> 1283             raise SolrError("Unknown collection: %s", collname)
   1284         collection = self.collections[collname]
   1285         shards = collection[ZooKeeper.SHARDS]

SolrError: (u'Unknown collection: %s', 'random_collection')

Solr版本为6.6.2,zookeeper版本为3.4.10

如何创建与solr云集合的连接?

3 个答案:

答案 0 :(得分:2)

Pysolr目前不支持外部zookeeper群集。 Pysolr检查 clusterstate.json 中的集合,Solr为每个集群即兴创建了 state.json ,并且 clusterstate.json 保持为空。< / p>

要解决单个集合的问题,您可以在pysolr.py中对ZooKeeper.CLUSTER_STATE变量进行硬编码,如下所示:

SELECT 1 from company_activity where product_id = :product_id and
                 applicationnumber = :applicationnumber and accountidentifier = :accountidentifier and startdate = :startdate and
                 activitydate = :activitydate and company_id = :company_id and companyemployee = :companyemployee and promotion = :promotion and
                 employee_id = :employee_id and productsalesrep_id = :productsalesrep_id limit 0, 1

pysolr.py可以在 /usr/local/lib/python2.7/dist-packages 找到,也许可以尝试重新安装

ZooKeeper.CLUSTER_STATE = '/collections/random_collection/state.json'

答案 1 :(得分:0)

一个更好的技巧是以一种通用的方式来提供这些集合:

import pysolr
import json

zookeeper = pysolr.ZooKeeper("ZK_STRING")
collections = {}
for c in zookeeper.zk.get_children("collections"):
    collections.update(json.loads(zookeeper.zk.get("collections/{}/state.json".format(c))[0].decode("ascii")))
zookeeper.collections = collections

答案 2 :(得分:0)

常规HTTP客户端甚至对于SolrCloud也能很好地工作。

这已通过Solr 7.5和PySolr 3.9.0进行了测试:

import pysolr

solr_url="https://my.solr.url"
collection = "my_collection"
solr_connection = pysolr.Solr("{}/solr/{}".format(solr_url, collection), timeout=10)
results = solr_connection.search(...)

print(results.docs)