Explorer 是 Laravel Scout 的下一代 Elasticsearch 驱动程序,具有 Elasticsearch 查询的功能。它提供了兼容的 Scout 驱动程序以及额外的便利。例如, Explored
接口定义了一个 mappableAs()
方法来获取配置:
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use JeroenG\Explorer\Application\Explored;
use Laravel\Scout\Searchable;
class Post extends Model implements Explored
{
use HasFactory;
use Searchable;
protected $fillable = ['title', 'published'];
public function mappableAs(): array
{
return [
'id' => 'keyword',
'title' => 'text',
'published' => 'boolean',
'created_at' => 'date',
];
}
}
然后在您的资源管理器配置中,您可以在索引下引用模型,如下所示:
return [
'indexes' => [
\App\Models\Post::class
],
];
除了 Laravel Scout 开箱即用的标准功能之外,Explorer 还提供了一种使用查询构建器编写更复杂查询的方法。高级查询文档有一个使用此包的查询生成器功能的示例:
$posts = Post::search('lorem')
->must(new Matching('title', 'ipsum'))
->should(new Terms('tags', ['featured'], 2))
->filter(new Term('published', true))
->get();
该软件包还提供了一些通过 CLI 管理索引和搜索的命令:
# create indexes
php artisan elastic:create
# delete indexes
php artisan elastic:delete
# search indexes
php artisan elastic:search "App\Models\Post" lorem
详情见:https://jeroeng.dev/blog/exploring-elasticsearch-with-laravel-scout/