sadovojav / yii2-search-lucene
此包的最新版本(dev-master)没有可用的许可证信息。
Yii2 搜索 Lucene
dev-master
2020-08-31 12:26 UTC
Requires
- php: >=5.4.0
- yiisoft/yii2: *
- zendframework/zendsearch: *@RC
This package is auto-updated.
Last update: 2024-08-29 04:36:06 UTC
README
功能
- 易于使用
- 在模型中搜索
- 多词
- 按数字搜索
- 在文档中搜索(xlsx, docx, pptx)
- 关系值
- 交互式添加/更新/删除索引
Composer
安装此扩展的首选方式是通过 Composer。
运行 php composer.phar require sadovojav/yii2-search-lucene "dev-master"
或将 "sadovojav/yii2-search-lucene": "dev-master"
添加到您的 composer.json
文件的 require 部分
配置
- 如果需要在模型类接口中实现 sadovojav\search\PageLink
use yii\helpers\Url; use sadovojav\search\PageLink; class News extends \yii\db\ActiveRecord implements PageLink { public function getUrl() { return Url::to(['/news/news/view', 'id' => $this->id]); } }
如果您想使用交互式添加/更新/删除索引
- 在您的配置文件中附加组件
'components' => [ 'search' => [ 'class' => 'sadovojav\search\components\SearchLucene', 'indexDirectory' => '@console/runtime/search' ] ]
- 将行为附加到您的模型
use sadovojav\search\behaviors\SearchBehavior; class News extends \yii\db\ActiveRecord { public function behaviors() { return [ 'search' => [ 'class' => SearchBehavior::className(), 'attributes' => [ 'name' => [ 'name' => SearchLucene::FIELD_TEXT ], 'text_intro' => [ 'text_intro' => SearchLucene::FIELD_UN_STORED ], 'text_full' => [ 'text_full' => SearchLucene::FIELD_UN_STORED ], ], 'conditions' => [ 'status_id' => self::STATUS_ACTIVE ], 'urlManagerRule' => [ 'news/<id:\d+>' => '/news/news/view' ] ] ]; } }
参数
- array
attributes
必需 - 要索引的属性 - array
conditions
- 创建搜索索引的条件 - string
baseUrl
=''
- 基础 URL - array
urlManagerRule
- 美化 URL 规则
注意
SearchBehavior can work correctly only with one language website. Otherwise, it will be indexed only one language.
如果您想使用控制台索引
- 在您的配置文件中附加组件
'components' => [ 'search' => [ 'class' => 'sadovojav\search\components\SearchLucene', 'indexDirectory' => '@console/runtime/search', 'models' => [ [ 'dataProviderOptions' => [ 'query' => common\modules\news\models\News::find() ->localized('en') ->active() ], 'attributes' => [ 'lang' => 'en', // Custom fild to search 'name' => [ 'name' => SearchLucene::FIELD_TEXT ], 'text_intro' => [ 'text_intro' => SearchLucene::FIELD_UN_STORED ], 'text_full' => [ 'text_full' => SearchLucene::FIELD_UN_STORED ], ], ] ] ] ]
- 在您的控制台配置文件中附加模块
'modules' => [ 'search' => 'sadovojav\search\Module' ]
-
如果需要,添加对您的 urlManager 的规则
-
创建搜索索引
在控制台
php yii search/search/index
- 优化搜索索引
在控制台
php yii search/search/optimyze
使用
搜索控制器
use Yii; use yii\data\ArrayDataProvider; class SearchController extends \yii\web\Controller { const ITEMS_PER_PAGE = 24; public function actionIndex($q) { $query = html_entity_decode(trim($q)); // Search documents without custom conditions // $results = Yii::$app->search->search($query); // Search documents with custom conditions (lang) $results = Yii::$app->search->search($query, [ 'lang' => Yii::$app->language ]); $dataProvider = new ArrayDataProvider([ 'allModels' => $results, 'pagination' => [ 'defaultPageSize' => self::ITEMS_PER_PAGE, 'forcePageParam' => false ] ]); return $this->render('index', [ 'query' => $query, 'dataProvider' => $dataProvider ]); } }
注意
搜索组件现在使用默认字段
class
- 带命名空间的模型类名pk
- 模型主键