shopapps / scout-solr-engine
Apache Solr 驱动程序,用于 Laravel Scout
Requires
- php: ^8.0|^8.1|^8.2
- ext-json: *
- laravel/scout: ^9.0|^10.0
- solarium/solarium: ^6.2
Requires (Dev)
- orchestra/testbench: ^6.21|^7.0
- phpmd/phpmd: ^2.10
- phpunit/phpunit: ^8.5.8|^9.3.3
- squizlabs/php_codesniffer: ^3.6
README
此包在 Laravel Scout 中提供了 Apache Solr 搜索引擎的基本实现。
安装
composer require shopapps/scout-solr-engine
配置
此包提供了一个配置文件,可以使用 .env 变量进行修改。
您可以使用以下命令初始化自己的配置文件:
php artisan vendor:publish --provider="Scout\Solr\ScoutSolrServiceProvider"
scout:index
默认情况下,Solr 不允许在没有先在文件系统上提供适当的文件夹和文件的情况下创建索引(核心)。但是,如果在 Solr 实例中设置了默认配置集,则可以通过 API 实现这一点。scout:index
命令只能在 Solr 实例正确配置并且配置文件具有对应配置集文件夹的相应名称时才能工作。有关更多信息,请参阅https://solr.apache.ac.cn/guide/8_9/config-sets.html#config-sets
要获取 _default
配置集到您的服务器,请尝试以下操作(调整命令以匹配您的 solr 版本/位置)
sudo cp -r /opt/solr-9.2.1/server/solr/configsets /var/solr/data sudo chown -R solr:solr /var/solr/data/configsets
检查该文件夹,您应该会看到类似以下内容
sudo ls -lah /var/solr/data/configsets/
total 16K
drwxr-xr-x 4 solr solr 4.0K Jul 13 07:03 .
drwxr-x--- 13 solr solr 4.0K Jul 13 07:03 ..
drwxr-xr-x 3 solr solr 4.0K Jul 13 07:03 _default
drwxr-xr-x 3 solr solr 4.0K Jul 13 07:03 sample_techproducts_configs
除非您正在使用它,否则您可以从那里安全地删除 sample_techproducts_configs
文件夹)
核心(索引)
在配置文件中未提供核心(索引)。引擎将使用模型上的 searchableAs()
方法来确定要连接到哪个核心。
或者,如果特定模型位于不同的 Solr 实例上,则可以为该模型提供另一个配置。配置密钥必须与模型的 searchableAs()
匹配。
在云模式下,我无法完全实现这一点,因此您可以通过以下方式尝试
步骤 1:通过管理面板在 solr 中创建一个集合。
http://127.0.0.1:8983/solr/#/~collections
选择添加集合并配置跨您的分片和副本(对于演示模式,请使用分片:2 和副本:2)
步骤 2:在模型中设置您的模式
确保您的模型配置为使用正确的 Searchable 特性
<?php namespace App\Models\User; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Str; use Scout\Solr\Traits\Searchable; // <--- THIS IS IMPORTANT class User extends Model { use HasFactory; use Searchable;
在您的模型上配置 searchable_fields 属性,以使用您要在 Solr 中创建的模式。
protected $searchable_fields = [ 'id' => ['type' => 'string', 'indexed' => true, 'stored' => true], 'role_id' => ['type' => 'plong', 'indexed' => true, 'stored' => true], 'name' => ['type' => 'string', 'indexed' => true, 'stored' => true], 'description' => ['type' => 'text_general', 'indexed' => true, 'stored' => true], 'is_active' => ['type' => 'boolean', 'indexed' => true, 'stored' => true], 'created_at' => ['type' => 'pdate', 'indexed' => true, 'stored' => true], 'updated_at' => ['type' => 'pdate', 'indexed' => true, 'stored' => true], ];
步骤 3:构建模式
$model = new \App\Models\User(); $model->buildSolrSchema();
这将记录任何问题到 Laravel 日志文件中
Solarium
此包使用 solarium/solarium 来处理对 Solr 实例的请求。此应用程序旨在是 laravel/scout 引擎的简单实现。对于对 Solr 实例的复杂查询,我建议初始化自己的 Solarium 客户端并使用该包。请访问 https://solarium.readthedocs.io/en/stable/ 查看 solarium 包的文档。
为了方便起见,任何在引擎上使用的未知方法都将转发到 solarium 客户端。
$model = new \App\Models\SearchableModel(); /** @var \Scout\Solr\Engines\SolrEngine $engine */ $engine = app(\Laravel\Scout\EngineManager::class)->engine(); $select = $engine->setCore($model)->createSelect(); $select->setQuery('*:*'); $result = $engine->select($select, $engine->getEndpointFromConfig($model->searchableAs())); // getEndpointFromConfig() is only necessary when your model does not use the default solr instance.
用法
这不是最好的例子,但您应该明白这个概念
$res = Product::search() ->where('owner', 1021) ->paginate();
或者...
$res = Product::search('description: "red" AND name: "car"') ->where('owner', 1021) ->paginate();
事件
Solr 引擎会分发多个事件,允许您挂钩到引擎的特定点。
致谢
这是众多优秀开发者工作的完整汇编。我需要一个项目可以工作的东西,但大多数包对于 laravel 10、scout 10 和 solr 9 而言都已经过时。如果您在这里发现了您的代码,请接受我的道歉,并告诉我,我会将其添加到列表中。