dev-master
9999999-devYii2 search lucene
The Requires
- php >=5.4.0
- yiisoft/yii2 *
- zendframework/zendsearch *@RC
yii2 search lucene
Yii2 search lucene
The preferred way to install this extension is through Composer., (*1)
Either run php composer.phar require sadovojav/yii2-search-lucene "dev-master"
, (*2)
or add "sadovojav/yii2-search-lucene": "dev-master"
to the require section of your composer.json
, (*3)
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' ] ] ]; } }
attributes
required - attributes to indexconditions
- Conditions for creating search indexbaseUrl
= ''
- Base urlurlManagerRule
- Pretty url rulesSearchBehavior 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' ]
Add rules for your urlManager if you need, (*4)
Create search index, (*5)
In console:, (*6)
php yii search/search/index
, (*7)
In console:, (*8)
php yii search/search/optimyze
, (*9)
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 ]); } }
Search component now use default fields
* class
- model class name with namespase
* pk
- model primary key, (*10)
Yii2 search lucene
yii2 search lucene