• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php的solr的操作类及demo

php 搞代码 3年前 (2022-01-22) 17次浏览 已收录 0个评论

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);        }   <i>1本文来#源gaodai$ma#com搞$代*码*网</i><pre>搞代gaodaima码

//排序 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,包括了方面的内容,更多相关内容请随时关注我们网站!

相关文章:

安装php-solr扩展

搜索方案 solr+php如何安装配置?

集成 PHP 应用和 Solr 搜索引擎


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php的solr的操作类及demo

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址