omure / scout-advanced-meilisearch
Laravel Scout 扩展,允许使用 meilisearch 高级功能,同时提供了一个扩展的集合驱动程序,用于测试目的。
v0.5.2
2022-11-29 10:19 UTC
Requires
- php: ^8.1
- laravel/framework: ^9.0
- laravel/scout: ^9.4
- meilisearch/meilisearch-php: ^0.26
Requires (Dev)
- orchestra/testbench: 7.0
README
此包提供的内容
扩展的 scout 查询构建器
添加以下更改到基本查询构建器
where('column', '<=', $value)
- 扩展的 where,支持 3 个可选比较参数。也允许两个参数。where(Clusure $query)
- where 可以将闭包作为第一个参数,以分组查询(同样适用于orWhere
)whereBetween('column', [$value1, $value2])
whereNotIn('column', [$value1, $value2, $value3])
- 所有 where 子句都有或变体:
orWhere
、orWhereIn
等
两个 scout 驱动程序
驱动程序与新的 scout 构建器兼容。
- meilisearch_advanced
- collection_advanced
meilisearch_advanced
使用 meilisearch 的所有优势来比较结果,在索引数组中进行搜索。还修复了计算值总数的问题。(当前实现请求整个数据集,如果 scout 构建器使用了 ->query()
方法)
collection_advanced
驱动程序模拟 meilisearch 的工作方式,并完全依赖于集合。它仅应用于测试,因为所有可搜索模型的数据都在内存中。该驱动程序允许测试过滤、排序以及扩展的 scout 构建器功能。
考虑 meilisearch 特定问题
Meilisearch 需要更新可过滤、可排序和可搜索的属性索引,以便正确保存索引数据。此包提供 MeiliSearch
门面,该门面具有 updateIndexSettings
方法,可以自动处理。
如何使用?
- 在
config/scout.php
中将'driver'
设置为meilisearch_advanced
或collection_advanced
- 将
Omure\ScoutAdvancedMeilisearch\Searchable
特性应用到模型上; - 为了正确处理模型,将
Omure\ScoutAdvancedMeilisearch\Interfaces\MeiliSearchSearchableModel
接口实现到模型上 - 在模型的
toSearchableArray()
方法中描述索引参数,以进行搜索(与 Scout 的方式相同) - 通过在模型上定义以下方法来指定可搜索、可过滤和可排序的属性
getSearchableAttributes()
- 用于使用search()
语句getFilterableAttributes()
- 用于使用where()
,包括whereIn
、whereNotIn
等. 语句getSortableAttributes()
- 用于使用orderBy()
语句getTypoToleranceSettings()
- 用于拼写容忍度 所有方法应返回一个字符串数组,这些字符串是toSearchableArray()
中指定的参数的名称。
如果您更改了返回索引的 getSearchableAttributes()
、getFilterableAttributes()
或 getSortableAttributes()
方法,则必须让 meilisearch 知道这些更改。为此,请使用门面: MeiliSearch::updateIndexSettings(new User());
该方法接收一个模型实例,以更新 meilisearch 服务器上的参数。
请注意,toSearchableArray()
不必与数据库列名相同的索引。您可以指定自己的搜索和过滤逻辑。在 Scout 构建器中使用指定的参数。