qlsgroup/elastic-search

为 CakePHP 3.0 提供的 Elastic Search 数据源和数据映射器

安装次数: 7,764

依赖者: 0

建议者: 0

安全性: 0

星标: 0

关注者: 0

分支: 54

类型:cakephp-plugin

3.4.4 2022-09-09 14:24 UTC

README

Build Status License

在 CakePHP 3.6+ 中使用 Elastic Search 作为替代 ORM 后端。

您可以在 Cake Book 中找到插件的文档。

使用 composer 安装 Elasticsearch

您可以使用 composer 将 Elasticsearch 安装到您的项目中。对于现有应用程序,您可以在您的 composer.json 文件中添加以下内容

"require": {
    "cakephp/elastic-search": "^2.0"
}

然后运行 php composer.phar update

版本表

您正在查看 2.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 使用仅有一个映射类型,因此您可以使用相同的名称(当类型未定义时,这是默认行为,使用索引名称的单数版本)。索引类型已在 ElasticSearch 7 中删除。

use Cake\ElasticSearch\Index;

class CommentsIndex extends Index
{
    /**
     * The name of index in Elasticsearch
     *
     * @return  string
     */
    public function getName()
    {
        return 'comments';
    }

    /**
     * The name of mapping type in Elasticsearch
     *
     * @return  string
     */
    public function getType()
    {
        return 'comments';
    }
}

运行测试

警告:请非常小心地运行测试,因为Fixture 将为内部结构创建和删除 Elasticsearch 索引。不要在生产或开发机器上运行测试,在这些机器上您的 Elasticsearch 实例中有重要数据。

假设您已通过以下方法之一在系统范围内安装了 PHPUnit,您可以按照以下方法运行 CakePHP 的测试

  1. phpunit.xml.dist 复制到 phpunit.xml
  2. 运行 phpunit