php的solr的操作類及demo

來源:互聯網
上載者:User
php的solr的操作類及demo

一、solr類

'127.0.0.1','port' => '8080');    /**     * 設定solr庫選擇     * @param $core string 庫名稱     */    public static function setCore($core){        if($core) self::$options['path']='solr/'.$core;    }     /**    * 增加solr索引    * @param $fieldArr 索引欄位及值    * @return bool true     */    public static function addDocument($fieldArr=array()){        $client = new SolrClient(self::$options);        $doc = new SolrInputDocument();        foreach($fieldArr as $k => $v){            $doc->addField($k,$v);         }        $client->addDocument($doc);        $client->commit();        return true;    }     /**    * 刪除索引    * @param $id 主鍵id id可以為數組形式,應用於多選的情況    * @return bool true    */    public static function delDocument($ids){        $client = new SolrClient(self::$options);        if(is_array($ids))            $client->deleteByIds($ids);        else            $client->deleteById($ids);        $client->commit();        return true;    }     /**    * 查詢資料    * @param $qwhere     關鍵字     * @param $fqwhere 附加條件,根據範圍檢索,適用於數值型    * @param $getField    查詢欄位     * @param $sort 排序 array('duration'=>'asc')  asc:升序,desc:降序    * @param $pageindex   查詢頁數    * @param $pagesize    每頁顯示條數    */    public static function selectQuery($qwhere=array(),$fqwhere=array(),$getField=array(),$sort=array(),$pageindex=1,$pagesize=20){        $client = new SolrClient(self::$options);        $query = new SolrQuery();        $sel = '';        foreach($qwhere as $k => $v){//            $sel .= ' +'.$k.':'.$v;            $sel = "{$k} : \"{$v}\"";        }        $query->setQuery($sel);        //關鍵字檢索         //附加條件,根據範圍檢索,適用於數值型        if($fqwhere){            $query->setFacet(true);            foreach($fqwhere as $k => $v)                $query->addFacetQuery($v);            //$query->addFacetQuery('price:[* TO 500]');        }         //查詢欄位        if($getField){            foreach($getField as $key => $val)                $query->addField($val);        }        //排序        if($sort){            foreach($sort as $k => $v){                if($v == 'asc')                    $query->addSortField($k,SolrQuery::ORDER_ASC);                elseif($v == 'desc')                    $query->addSortField($k,SolrQuery::ORDER_DESC);            }        }        //分頁        $query->setStart(self::getPageIndex($pageindex,$pagesize));        $query->setRows($pagesize);                 $query_response = $client->query($query);        $response = $query_response->getResponse();        return $response;    }     /**    * 分頁資料處理    */    private static function getPageIndex($pageindex,$pagesize){        if($pageindex<=1)            $pageindex = 0;        else            $pageindex = ($pageindex-1)*$pagesize;        return $pageindex;    } }

二、操作demo

"wang jing jie",);print_r(phpSolr::selectQuery($qwhere)); //添加$fieldArr = array(    "id" => 15,    "username" => "si sheng chao",    "usertype" => 1,    "last_update_time" => "2016-01-05T03:35:13Z",);phpSolr::addDocument($fieldArr); //刪除//phpsolr::delDocument(15);

以上就介紹了php的solr的操作類及demo,包括了方面的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

相關文章:

安裝php-solr擴充

搜尋方案 solr+php如何安裝配置?

整合 PHP 應用和 Solr 搜尋引擎

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.