2017 © Pedro Peláez
 

library cakephp-sphinxsearch

image

voycey/cakephp-sphinxsearch

  • Thursday, August 2, 2018
  • by voycey
  • Repository
  • 1 Watchers
  • 3 Stars
  • 45 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 7 Versions
  • 5 % Grown

The README.md

Sphinx plugin for CakePHP

Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

For earlier versions of CakePHP (2.x)

Please use the 2.x branch and follow the readme instructions under that for usage instructions, (*2)

Installation

You can install this plugin into your CakePHP application using composer., (*3)

The recommended way to install composer packages is:, (*4)

composer require voycey/cakephp-sphinxsearch

Basic Documentation

I designed this as a replacement for the binary API access for Sphinxsearch that I was using on 2.x (https://github.com/voycey/sphinxsearch-cakephp2), (*5)

It currently has one function and that is to query the provided index and return the matching records in a CakePHP friendly format (In this case as Query objects and Entities)., (*6)

How to use

  • Install the package with composer as above
  • Add Plugin::load('Sphinx'); to your bootstrap.php
  • Attach the behaviour to a table you wish to search on (There must be an index that is generated from this model - the behaviour works by pulling the ID's from Sphinx and then fetching them from the DB (See TODO's for improving this)
  • Behavior config options:
    • 'connection' => ['host' => 'hostname', 'port' => 'port'] (default hostname is 'localhost' and default port is 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');
    }
}
  • Perform a search through the behaviour directly (This will return a query object), it takes an array of the following parameters:, (*7)

    • index - this is the index you want to search against
    • term - this is the term you want to search for
    • match_fields - these are the fields you want to search against (default is search whole index)
    • pagination - this is a standard Cake 3 pagination array - allows you to define how your data comes back, what fields it contains and what Models are contained.
    • limit - this is the number of results to limit to, currently defaults to 1000

Here is an example unit test that works for me., (*8)


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

  • Give option for all data to be pulled from Sphinxsearch directly rather than then querying DB
  • Hook into afterSave and have the Sphinx index updated (this isn't a priority for me as my indexes don't need to be live but please submit a pull request if you want to add this)
  • Work out how to test this easily on Travis (again - help appreciated)

The Versions

02/08 2018

2.x-dev

2.9999999.9999999.9999999-dev

  Sources   Download

17/06 2016

dev-master

9999999-dev https://github.com/voycey/cakephp-sphinxsearch

Sphinxsearch plugin for CakePHP 3.x

  Sources   Download

MIT

The Requires

 

The Development Requires

19/11 2015

0.1.5

0.1.5.0 https://github.com/voycey/sphinxsearch-cakephp3

Sphinxsearch plugin for CakePHP 3.x

  Sources   Download

MIT

The Requires

 

The Development Requires

17/11 2015

0.1.4

0.1.4.0 https://github.com/voycey/sphinxsearch-cakephp3

Sphinxsearch plugin for CakePHP 3.x

  Sources   Download

MIT

The Requires

 

The Development Requires

17/11 2015

0.1.2

0.1.2.0 https://github.com/voycey/sphinxsearch-cakephp3

Sphinxsearch plugin for CakePHP 3.x

  Sources   Download

MIT

The Requires

 

The Development Requires

17/11 2015

0.1.3

0.1.3.0 https://github.com/voycey/sphinxsearch-cakephp3

Sphinxsearch plugin for CakePHP 3.x

  Sources   Download

MIT

The Requires

 

The Development Requires

17/11 2015

0.1.1

0.1.1.0 https://github.com/voycey/sphinxsearch-cakephp3

Sphinxsearch plugin for CakePHP 3.x

  Sources   Download

MIT

The Requires

 

The Development Requires