voycey/cakephp-sphinxsearch

CakePHP 3.x 的 Sphinxsearch 插件

安装: 110

依赖: 0

建议者: 0

安全性: 0

星标: 3

关注者: 1

分支: 3

开放问题: 1

类型:cakephp-plugin

0.1.5 2015-11-19 05:45 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:47:27 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

对于 CakePHP 早期版本(2.x)

请使用 2.x 分支,并遵循该分支下的说明文档以获取使用说明

安装

您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。

安装 composer 包的推荐方法是

composer require voycey/cakephp-sphinxsearch

##基本文档

我将这个设计为替换我在 2.x 上使用的二进制 API 访问 Sphinxsearch(https://github.com/voycey/sphinxsearch-cakephp2

它目前有一个功能,即查询提供的索引,并以 CakePHP 友好的格式返回匹配的记录(在这种情况下为查询对象和实体)。

##如何使用

  • 如上所述使用 composer 安装此包
  • Plugin::load('Sphinx'); 添加到您的 bootstrap.php
  • 将行为附加到您希望搜索的表上(必须由该模型生成索引 - 行为通过从 Sphinx 拉取 ID 然后从 DB 获取它们来工作(请参阅 TODO 的改进)
  • 行为配置选项
    • 'connection' => ['host' => 'hostname', 'port' => 'port'](默认主机名是 'localhost',默认端口是 9306)
    • 'defaultIndex' => 'index_name'
<?php 
class PostsTable extends Table
{

    /**
     * Initialize method
     *
     * @param  array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('posts');
        $this->displayField('title');
        $this->primaryKey('id');

        $this->addBehavior('Timestamp');
        $this->addBehavior('Sphinx.Sphinx');
    }
}
  • 直接通过行为执行搜索(这将返回一个查询对象),它接受以下参数数组

    • index - 这是您想要搜索的索引
    • term - 这是您想要搜索的术语
    • match_fields - 这些是要搜索的字段(默认是搜索整个索引)
    • pagination - 这是一个标准的 Cake 3 分页数组 - 允许您定义数据返回的方式,包含哪些字段以及包含哪些模型。
    • limit - 这是限制结果的数量,目前默认为 1000

这是一个为我工作的单元测试示例。

public function testBehaviour()
{
    $paginate = [
        'order' => [
            'Posts.id asc'
        ],
        'fields' => [
            'id', 'title', 'user_id'
        ],
        'contain' => [
            'Comments' => [
                'fields' => ['id', 'post_id']
            ],
            'Categories' => [
                'fields' => ['id', 'CategoriesPosts.post_id']
            ],
            'Types' => [
                'fields' => ['id', 'name']
            ]
        ]
    ];

    $query = $this->Posts->search([
                                    'index' => 'idx_toolkit', 
                                    'term' => 'Ten', 
                                    'match_fields' => 'title', 
                                    'paginate' => $paginate,
                                    'limit' => 50
                                ]);
    
    $row = $query->first();

    $this->assertInstanceOf('Cake\ORM\Query', $query);
    $this->assertInstanceOf('Cake\ORM\Entity', $row);

}

###TODO

  • 提供选项,直接从 Sphinxsearch 获取所有数据,而不是查询 DB
  • 钩入 afterSave 并更新 Sphinx 索引(这不是我的优先事项,因为我的索引不需要实时更新,但如果您想添加此功能,请提交 pull request)
  • 找出如何在 Travis 上轻松测试此功能(再次感谢帮助)