tinymeng / yii2-solr
yii2 use solr
Installs: 2 132
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 0
Open Issues: 1
Requires
- php: >=5.4.0
- solarium/solarium: ^4.1
- yiisoft/yii2: ~2.0.6
This package is auto-updated.
Last update: 2024-10-25 21:23:44 UTC
README
Install
composer require tinymeng/yii2-solr dev-master -vvv
类库使用的命名空间为
\\tinymeng\\solr
注意
- 不必安装PHP的Solr扩展(由于solr扩展很久没有更新过了,仅支持PHP5.6左右的版本,PHP7.0以上版本安装不上)
- 暂时只实现了AR查询的使用
- 未实现AR的增删改,可以使用 \yii::$app->solr 进行操作
引用小部件 main.php
'components' => [ 'solr'=>[ 'class'=> 'tinymeng\solr\Client', 'options' => [ 'endpoint'=>[ [ 'scheme' => 'http', 'host' => 'majiameng.com', 'port' => 8080, 'path' => '/solr/', 'core' => 'collection1', ], [ 'scheme' => 'http', 'host' => 'majiameng.com', 'port' => 8080, 'path' => '/solr/', 'core' => 'collection2', ], ], ] ], ]
yii AR 查询模式
create Model
<?php namespace models\solr; use \tinymeng\solr\ActiveRecord; class Collection extends ActiveRecord { /** solr core name */ public static function tableName() { return 'collection1'; } /** solr core attr */ public function attributes() { return [ 'id', 'title', 'name', ]; } }
查询方法
$where = [ 'id'=>1, 'type'=>28, ['between','type',1,100] ]; $select = ['id,title']; $page = 1; $page_size = 20; $list = Collection::find() ->select($select) ->where($where) ->offset(($page-1)*$page_size) ->limit($page_size) ->orderBy('id asc,type asc') ->asArray() ->all();
查询高亮方法
$keywords = '汽车'; Collection::find() ->select($select) ->where(['keywords'=>$keywords]) ->highlight([ "pre_tags"=>'<font color="#ff0000">', "post_tags"=>'</font>', "fields"=>['title','content'] ]) ->asArray() ->all();