PHP code example of seffeng / xunsearch
1. Go to this page and download the library: Download seffeng/xunsearch library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once ('vendor/autoload.php' );
seffeng / xunsearch example snippets
文件目录为 vendor/hightman/xunsearch/app
define ('XS_APP_ROOT' , '/path/to/ini' )
$xs = new \XS('demo' );
'components' => array (
'search' => array (
'class' => 'EXunSearch' ,
'project' => 'demo' ,
'charset' => 'utf-8' ,
),
),
$data = array ('pid' => 1234 , 'subject' => '标题' , 'message' => '内容' );
Yii::app()->search->add($data);
Yii::app()->search->update($data);
Yii::app()->search->del('1234' );
Yii::app()->search->setQuery('subject:标题' );
$docs = Yii::app()->search->setLimit(5 , 10 )->search();
'components => [
// ... other components ...
' xunsearch' => [
' class ' => 'hightman \xunsearch \Connection ', // 此行必须
'iniDirectory ' => '@app /config ', // 搜索 ini 文件目录,默认:@vendor /hightman /xunsearch /app
'charset ' => 'utf -8', // 指定项目使用的默认编码,默认即时 utf -8,可不指定
],
],
$db = \Yii::$app->xunsearch->getDatabase('demo' );
$db = (\Yii::$app->xunsearch)('demo' );
$xs = $db->xs;
$search = $db->getSearch();
$index = $db->getIndex();
class Demo extends \hightman \xunsearch \ActiveRecord
{
}
$model = new Demo;
$model->pid = 321 ;
$model->subject = 'hello world' ;
$model->message = 'just for testing...' ;
$model->save();
$model = Demo::findOne(321 );
$model->message .= ' + updated' ;
$model->save();
$model->addTerm('subject' , 'hi' );
$model->addIndex('subject' , '你好,世界' );
$model->delete();
$query = Demo::find();
$condition = 'hello world' ;
$condition = ['WILD' , 'key1' , 'key2' ... ];
$condition = ['AND' , 'key1' , 'key2' ... ];
$condition = ['OR' , 'key1' , 'key2' ... ];
$condition = ['XOR' , 'key1' , 'key2' ... ];
$condition = ['NOT' , 'key' ];
$condition = ['pid' => '123' , 'subject' => 'hello' ];
$condition = ['pid' => ['123' , '456' ]];
$condition = ['IN' , 'pid' , ['123' , '456' ]];
$condition = ['NOT IN' , 'pid' , ['123' , '456' ]];
$condition = ['BETWEEN' , 'chrono' , 14918161631 , 15918161631 ];
$condition = ['WEIGHT' , 'subject' , 'hello' , 0.5 ];
$query->where($condition);
$model = Demo::findOne(321 );
$model->docid();
$model->rank();
$model->percent();
$model->ccount();
$model->matched();
$db = Demo::getDb();
$search = $db->getSearch();
$index = $db->getIndex();
$scws = $db->getScws();
'bootstrap' => ['debug' ],
'modules' => [
'debug' => [
'class' => 'yii\\debug\\Module' ,
'panels' => [
'xunsearch' => [
'class' => 'hightman\\xunsearch\\DebugPanel' ,
],
],
],
],