tinymeng 2019-09-27 11:18:38 5025次浏览 0条评论 1 1 0

基于Yii 中 solr 的使用的封装

yii2-solr

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();

持续更新

觉得很赞
    没有找到数据。
您需要登录后才可以评论。登录 | 立即注册