3

我已经配置了一个多核 solr 云。创建了一个包含 2 个碎片且没有复制的集合。solr 的 UI 中的云

通过 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 云集合的连接?

4

3 回答 3

3

Pysolr 目前不支持外部 zookeeper 集群。Pysolr 检查clusterstate.json中的集合,Solr 已为每个集群临时设置为state.json ,并且clusterstate.json保持为空。

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

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

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

pip install -e /usr/local/lib/python2.7/dist-packages/pysolr.py
于 2017-11-14T05:57:34.027 回答
1

常规 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)
于 2020-10-18T21:08:04.240 回答
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
于 2020-01-21T22:09:10.280 回答