0

关于文档,必须在初始化时设置路径(即核心)SolrClient

$client = new SolrClient([
    'hostname' => 'localhost',
    'port' => 8983,
    'path' => '/solr/coreXYZ',
]);

由于我需要访问多个核心(例如/solr/core_1, /solr/core_2),有没有办法动态更改路径?我找不到任何选项queryrequest方法。

编辑

我找到了一种也有效的方法:

$client->setServlet(SolrClient::SEARCH_SERVLET_TYPE, '../' . $core . '/select');
$client->setServlet(SolrClient::UPDATE_SERVLET_TYPE, '../' . $core . '/update');

但这对我来说只是一个肮脏的黑客

4

1 回答 1

1

创建工厂方法并根据您访问的核心返回不同的对象。保持对象状态在您请求的核心之间发生变化,而无需通过查询方法明确设置,这是导致奇怪错误的秘诀。

类似于以下伪代码(我没有可用的 Solr 扩展,所以我无法对此进行测试):

class SolrClientFactory {
    protected $cache = [];
    protected $commonOptions = [];

    public function __construct($common) {
        $this->commonOptions = $common;
    }

    public function getClient($core) {
        if (isset($this->cache[$core])) {
            return $this->cache[$core];
        }

        $opts = $this->commonOptions;

        // assumes $path is given as '/solr/'
        $opts['path'] .= $core;

        $this->cache[$core] = new SolrClient($opts);
        return $this->cache[$core];
    }
}

$factory = new SolrClientFactory([
    'hostname' => 'localhost',
    'port' => 8983,
    'path' => '/solr/',
]);

$client = $factory->getClient('coreXYZ');
于 2019-01-20T22:45:40.327 回答