如何在Codeigniter中处理Solr断开连接

时间:2019-06-12 12:33:56

标签: codeigniter exception solr solarium

我通过CodeIgniter中的日光浴室借助solr实现搜索功能。

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

  

遇到未捕获的异常

     

类型:Solarium \ Exception \ HttpException

     

消息:Solr HTTP错误:HTTP请求失败,无法连接到本地主机端口8983:连接被拒绝

     

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

     

行号: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);
        }

1 个答案:

答案 0 :(得分:0)

围绕在​​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(),而是可以返回false,也可以设置自己的标题和消息。