cakephp / elastic-search
为 CakePHP 提供的 Elastic Search 数据源和数据映射器
Requires
- cakephp/cakephp: ^5.0.0
- ruflin/elastica: ^7.1
Requires (Dev)
- cakephp/cakephp-codesniffer: ^5.0
- phpunit/phpunit: ^10.1.0
- 4.x-dev
- 4.0.1
- 4.0.0
- 3.x-dev
- 3.4.4
- 3.4.3
- 3.4.2
- 3.4.1
- 3.4.0
- 3.3.0
- 3.2.1
- 3.2.0
- 3.1.1
- 3.1.0
- 3.0.0
- 2.x-dev
- 2.0.2
- 2.0.1
- 2.0.0
- 2.0.0-RC2
- 2.0.0-RC1
- 2.0.0-beta
- 1.5.2
- 1.5.1
- 1.5.0
- 1.0.x-dev
- 1.0.0
- 0.5.x-dev
- 0.5.0
- 0.3.5
- 0.3.4
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.x-dev
- 0.2.1
- 0.1.1
- 0.1.0
- dev-dependabot/composer/ruflin/elastica-tw-8.0
This package is auto-updated.
Last update: 2024-09-12 14:53:20 UTC
README
在 CakePHP 5.0+ 中使用 Elastic Search 作为替代 ORM 后端。
您可以在 Cake Book 中找到有关插件的文档 [文档链接]。
通过 composer 安装 Elasticsearch
您可以使用 composer 将 Elasticsearch 安装到您的项目中。对于现有应用程序,您可以将以下内容添加到您的 composer.json
文件中
"require": {
"cakephp/elastic-search": "^4.0"
}
然后运行 php composer.phar update
版本表
您正在查看 3.x 版本。
将插件连接到您的应用程序
安装后,您应该告诉您的应用程序加载该插件
use Cake\ElasticSearch\Plugin as ElasticSearchPlugin; class Application extends BaseApplication { public function bootstrap() { $this->addPlugin(ElasticSearchPlugin::class); // If you want to disable to automatically configure the Elastic model provider // and FormHelper do the following: // $this->addPlugin(ElasticSearchPlugin::class, [ 'bootstrap' => false ]); } }
定义连接
在您可以与 Elasticsearch 模型进行任何操作之前,您需要定义一个连接
// in config/app.php 'Datasources' => [ // other datasources 'elastic' => [ 'className' => 'Cake\ElasticSearch\Datasource\Connection', 'driver' => 'Cake\ElasticSearch\Datasource\Connection', 'host' => '127.0.0.1', 'port' => 9200 ], ]
作为替代,您可以使用链接格式,如果您喜欢使用环境变量等。
// in config/app.php 'Datasources' => [ // other datasources 'elastic' => [ 'url' => env('ELASTIC_URL', null) ] ] // and make sure the folowing env variable is available: // ELASTIC_URL="Cake\ElasticSearch\Datasource\Connection://127.0.0.1:9200?driver=Cake\ElasticSearch\Datasource\Connection"
您可以通过将 log
配置选项设置为 true 来启用请求日志记录。默认情况下将使用 debug
日志配置文件。您还可以在 Cake\Log\Log
中定义一个 elasticsearch
日志配置文件来自定义 Elasticsearch 查询日志的位置。查询日志记录在 'debug' 级别。
获取索引对象
索引对象与 Elastic Search 中的 ORM\Table
实例相当。您可以使用 IndexRegistry
工厂获取实例,就像 TableRegistry
一样。
use Cake\ElasticSearch\IndexRegistry; $comments = IndexRegistry::get('Comments');
如果您已启用引导加载插件,您可以在控制器中使用模型工厂加载索引。
class SomeController extends AppController { public function initialize() { $this->loadModel('Comments', 'Elastic'); } public function index() { $comments = $this->Comments->find(); } ...
每个 Index
对象都需要一个对应的 Elasticsearch index,就像大多数 ORM\Table
需要一个数据库 table 一样。
在上面的示例中,如果您定义了一个名为 CommentsIndex
的类,并且 IndexRegistry
可以找到它,则 $comments
将接收一个带有连接和索引内部配置的初始化对象。但是,如果您没有那个类,将初始化一个默认对象,并将 Elasticsearch 中的索引名称映射到该类。
索引类
您必须创建自己的 Index
类来定义 Elasticsearch 的内部 index 名称,以及定义映射类型和定义您需要的任何实体属性,例如虚拟属性。由于您必须为每个 index 使用仅一个映射类型,因此您可以使用相同的名称(当 type 未定义时,默认行为是使用 index 名称的单数形式)。索引类型在 ElasticSearch 7 中已删除。
use Cake\ElasticSearch\Index; class CommentsIndex extends Index { /** * The name of index in Elasticsearch * * @return string */ public function getName() { return 'comments'; } }
运行测试
我们建议使用包含的 docker-compose.yml
进行本地开发。Dockerfile
包含开发环境,并将下载并启动一个 Elasticsearch 容器,该容器在端口 9200 上。
# Start elasticsearch docker-compose up -d # Open an terminal in the development environment docker-compose run console bash
一旦进入容器,您就可以安装依赖关系并运行测试。
./composer.phar install vendor/bin/phpunit
警告:请谨慎运行测试,因为 Fixtures 将为内部结构创建和删除 Elasticsearch 索引。不要在生产或开发机器上运行测试,其中包含您在 Elasticsearch 实例中的重要数据。
假设您已通过以下方法之一在系统范围内安装了 PHPUnit:[安装说明链接],您可以通过以下方式运行 CakePHP 的测试:
- 将
phpunit.xml.dist
复制到phpunit.xml
- 运行
phpunit