代码之家  ›  专栏  ›  技术社区  ›  rabudde

如何在增力器中改变路径

  •  0
  • rabudde  · 技术社区  · 6 年前

    关于 documentation 必须在初始化时设置路径(即核心) SolrClient :

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

    因为我需要访问多个核心(例如 /solr/core_1 , /solr/core_2 )是否可以动态更改路径?我找不到任何选择 query request 方法。

    编辑

    我发现了一种有效的方法:

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

    但这只对我来说是个卑鄙的家伙

    1 回复  |  直到 6 年前
        1
  •  1
  •   rabudde    6 年前

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

    类似于以下伪代码(我没有可用的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');
    
    推荐文章