PHP code example of sdfsky / tipask-xunsearch

1. Go to this page and download the library: Download sdfsky/tipask-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');

/* Start to develop here. Best regards https://php-download.com/ */

    

sdfsky / tipask-xunsearch example snippets


'providers' => [
	Sdfsky\TipaskXunSearch\ServiceProvider::class,
],

'aliases' => [
	'Search' => Sdfsky\TipaskXunSearch\Facade::class,
],


//@see http://www.xunsearch.com/doc/php/guide/ini.guide
"project" => [
    "project.name" => "tipask",
    "project.default_charset" => "utf-8",
    "server.index" => "127.0.0.1:8383",
    "server.search" => "127.0.0.1:8384",
    //remember change FIELD_LABEL_DEFAULT_SEARCH_PK value in Config.php
    "primary_key" => [
        "type" => "id"
    ],
    //remember change FIELD_LABEL_DEFAULT_CLASS_ID value in Config.php
    "class_uid" => [
        "index" => "both"
    ],
    //remember change FIELD_LABEL_DEFAULT_DB_PK value in Config.php
    "id" => [
        "type" => "numeric"
    ],
    "subject" => [
        "type" => "title"
    ],
    "status" => [
        'type' => "numeric"
    ],
    "content" => [
        "type" => "body"
    ]
],

'index' => [
	
	// ...

	namespace\FirstModel::class => [
		'fields' => [
			'name', 'full_description', // fields for indexing
		],
		'primary_key' => 'id'  //primary_key name in DB, default 'id'
	],
	
	namespace\SecondModel::class => [
		'fields' => [
			'name', 'short_description', // fields for indexing
		]
	],
	
	// ...
	
],



use Illuminate\Database\Eloquent\Model;
use DavinBao\LaravelXunSearch\Model\SearchableInterface;

class Dummy extends Model implements SearchableInterface
{
        // ...

        /**
         * Get id list for all searchable models.
         */
        public static function searchableIds()
        {
            return self::wherePublish(true)->lists('id');
        }

        // ...
}



    use Illuminate\Database\Eloquent\Model;
    use DavinBao\LaravelXunSearch\Model\SearchableInterface;
    use DavinBao\LaravelXunSearch\Model\SearchTrait;

    class Dummy extends Model implements SearchableInterface
    {
        use SearchTrait;
    
        // ...
    }


$query = Model::getSearch()->addQuery("clock"); // search by all fields.
// or 
$query = Model::getSearch()->addQuery('name:clock'); // search by 'name' field.
// or
$query = Model::getSearch()->addQuery('name:clock'); // filter by 'short_description' field.

$Ids = Model::getSearch()->addQuery('name:clock')->getIDList(); // filter by 'short_description' field.

$models = $query->search();

$models = $query->getIDList();

$count = $query->count();

$models = $query->limit(5, 10)->get(); // Limit = 5 and offset = 10

$query = $query->setSort('chrono', true);


$docs = $search->setQuery('测试')->setLimit(5)->search();
foreach ($docs as $doc)
{
   $subject = $search->highlight($doc->subject); // 高亮处理 subject 字段
   $message = $search->highlight($doc->message); // 高亮处理 message 字段
   echo $doc->rank() . '. ' . $subject . " [" . $doc->percent() . "%] - ";
   echo date("Y-m-d", $doc->chrono) . "\n" . $message . "\n";
}

bash
php artisan vendor:publish --provider="Sdfsky\TipaskXunSearch\ServiceProvider"
bash
php artisan search:rebuild --verbose
bash
php artisan search:clear