1. Go to this page and download the library: Download luorenshu/scout 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/ */
luorenshu / scout example snippets
use Hyperf\Database\Model\Model;
use HyperfExt\Scout\Searchable;
class Post extends Model
{
use Searchable;
}
declare(strict_types=1);
namespace App\Models;
use Hyperf\Database\Model\Model;
use HyperfExt\Scout\Searchable;
class Post extends Model
{
use Searchable;
// 搜索 `searchSetting` 配置
public $searchSetting = [
//高亮
'attributesToHighlight' => [
'title' => ["pre_tags" => "<b style='color:red'>", "post_tags" => "</b>"],
'content'
],
];
}
declare(strict_types=1);
namespace App\Models;
use Hyperf\Database\Model\Model;
use HyperfExt\Scout\Searchable;
class Post extends Model
{
use Searchable;
// 使用 `scoutSettings` 属性配置
protected $scoutSettings = [
'analysis' => [
'analyzer' => [
'es_std' => [
'type' => 'standard',
'stopwords' => '_spanish_'
]
]
],
];
// 或者重写 `getScoutSettings` 方法配置
public function getScoutSettings(): ?array
{
return [
'analysis' => [
'analyzer' => [
'es_std' => [
'type' => 'standard',
'stopwords' => '_spanish_'
]
]
],
];
}
}
declare(strict_types=1);
namespace App\Models;
use Hyperf\Database\Model\Model;
use HyperfExt\Scout\Searchable;
class Post extends Model
{
use Searchable;
// 使用 `scoutMapping` 属性配置
protected $scoutMapping = [
'properties' => [
'title' => ['type' => 'text'],
],
];
// 或者重写 `getScoutMapping` 方法配置
public function getScoutMapping(): array
{
return [
'properties' => [
'title' => ['type' => 'text'],
],
];
}
}
declare(strict_types=1);
namespace App\Models;
use Hyperf\Database\Model\Model;
use HyperfExt\Scout\Searchable;
class Post extends Model
{
use Searchable;
/**
* 获取模型的索引名称。
*/
public function searchableAs(): string
{
return 'posts_index';
}
}
declare(strict_types=1);
namespace App\Models;
use Hyperf\Database\Model\Model;
use HyperfExt\Scout\Searchable;
class Post extends Model
{
use Searchable;
/**
* 获取模型的可搜索数据。
*/
public function toSearchableArray(): array
{
$array = $this->toArray();
// 自定义数组...
return $array;
}
}
declare(strict_types=1);
namespace App\Models;
use Hyperf\Database\Model\Model;
use HyperfExt\Scout\Searchable;
class User extends Model
{
use Searchable;
/**
* 获取模型主键。
*
* @return mixed
*/
public function getScoutKey()
{
return $this->email;
}
/**
* 获取模型键名。
*
* @return mixed
*/
public function getScoutKeyName()
{
return 'email';
}
}
$order = new App\Models\Order;
// ...
$order->save();