如何处理 codeigniter 中的 solr 断开连接

How to handle solr disconnection in codeigniter

我在 Solr 的帮助下通过 CodeIgniter 中的日光浴室实现了搜索功能。

我在没有 solr 连接的情况下启动我的网站(即如果 solr 连接停止),我的网站会抛出以下警告消息,

An uncaught Exception was encountered

Type: Solarium\Exception\HttpException

Message: Solr HTTP error: HTTP request failed, Failed to connect to localhost port 8983: Connection refused

Filename: C:\wamp\www\project-folder\project-name\vendor\solarium\solarium\src\Core\Client\Adapter\Curl.php

Line Number: 170

我怀疑是否有机会在 solr 连接中添加异常处理。

这意味着,如果 solr status 为 true,它将按原样工作。如果 solr 状态为 false(未连接),则不会显示错误警告。

上述情况是否可能通过使用异常处理来实现。

更新

我的控制器页面,

        function __construct()
        {
          parent::__construct();
          $this->config->load('solarium');
          $this->client = new Solarium\Client($this->config->item('solarium_endpoint'));
        }

        public function solrQuery() 
        {
            $query = $this->client->createSelect();

            $query->setStart(0)->setRows(1000);

            // get the facetset component
            $facetSet = $query->getFacetSet();

            $facetSet->createFacetField('product_category_1')->setField('product_category_1');

            $categories_data = $this->client->select($query);
        }

围绕 try-catch 块中抛出异常的相关代码位,如下所示:

function __construct() {
    parent::__construct();
    $this->config->load('solarium');
    try {
        $this->client = new Solarium\Client($this->config->item('solarium_endpoint'));
    } catch (\Exception $e) {
        show_error($e->getMessage());
    }
}

public function solrQuery() {
    try {
        $query = $this->client->createSelect();

        $query->setStart(0)->setRows(1000);

        // get the facetset component
        $facetSet = $query->getFacetSet();

        $facetSet->createFacetField('product_category_1')->setField('product_category_1');

        $categories_data = $this->client->select($query);
    } catch (\Exception $e) {
        show_error($e->getMessage());
    }
}

当然你没有使用 show_error() 而是可以 return false,或者设置你自己的 headers 和你自己的消息。