Home  >  Article  >  Backend Development  >  PHP's solr operation classes and demo

PHP's solr operation classes and demo

PHP中文网
PHP中文网Original
2017-03-24 13:25:442997browse

php’s solr operation class and demo

1. Solr class

'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;
    }
 
}

2. Operation 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);

The above introduces the php’s solr operation class and demo, including aspects Content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!

Related articles:

Install php-solr extension

Search solution How to install and configure solr+php?

Integrated PHP application and Solr search engine

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn