From eeef0cb65159cb686a4286045301cfa08278f3ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maikel=20Doez=C3=A9?= Date: Wed, 21 Dec 2016 02:45:33 +0100 Subject: [PATCH 1/8] Create SphinxAdapter.php Added a new adapter to support searches through SphinxClient --- src/Pagerfanta/Adapter/SphinxAdapter.php | 62 ++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/Pagerfanta/Adapter/SphinxAdapter.php diff --git a/src/Pagerfanta/Adapter/SphinxAdapter.php b/src/Pagerfanta/Adapter/SphinxAdapter.php new file mode 100644 index 00000000..9ee8c77d --- /dev/null +++ b/src/Pagerfanta/Adapter/SphinxAdapter.php @@ -0,0 +1,62 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Pagerfanta\Adapter; + +use SphinxClient; + +class SphinxAdapter implements AdapterInterface +{ + private $client; + private $query; + private $index; + private $comment; + private $results; + + /** + * Constructor. + * + * @param SphinxClient $client A Sphinx client. + * @param $query A Sphinx query. + * @param $index A Sphinx index. + * @param $comment A Sphinx comment. + */ + public function __construct(SphinxClient $client, $query, $index = "*", $comment = "") + { + $this->client = $client; + $this->query = $query; + $this->index = $index; + $this->comment = $comment; + } + + /** + * {@inheritdoc} + */ + public function getNbResults() + { + if (!$this->results) { + return $this->client->query($this->query, $this->index, $this->comment)['total_found']; + } + + return $this->results['total_found']; + } + + /** + * {@inheritdoc} + */ + public function getSlice($offset, $limit) + { + // Set limit + $this->client->setLimits($offset,$limit); + + return $this->results = $this->client + ->query($this->query, $this->index, $this->comment); + } +} From 582589c1d8a3c93cfb549cb5a83fa545ee833aad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maikel=20Doez=C3=A9?= Date: Wed, 21 Dec 2016 02:52:11 +0100 Subject: [PATCH 2/8] Update README.md and added Sphinx documentation Added Sphinx adapter documentation. --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index b52f4941..614223d2 100644 --- a/README.md +++ b/README.md @@ -315,6 +315,21 @@ $query->setQuery('search term'); $adapter = new SolariumAdapter($solarium, $query); ``` +### SphinxAdapter +To paginate a [Sphinx](http://php.net/manual/en/class.sphinxclient.php) query: + +```php + Date: Wed, 21 Dec 2016 02:54:34 +0100 Subject: [PATCH 3/8] Added comment param to Sphinx docs Added comment param to Sphinx docs. --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 614223d2..4b92a3a3 100644 --- a/README.md +++ b/README.md @@ -323,11 +323,12 @@ To paginate a [Sphinx](http://php.net/manual/en/class.sphinxclient.php) query: use Pagerfanta\Adapter\SphinxAdapter; -$index = 'example_index'; $query = '*'; +$index = 'example_index'; +$comment = ''; $sphinxClient = new SphinxClient(); -$adapter = new SphinxAdapter($sphinxClient, $query, $index); +$adapter = new SphinxAdapter($sphinxClient, $query, $index, $comment); ``` ### FixedAdapter From 6613d834395cb841ae1258bd6c715b9c80d3659f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maikel=20Doez=C3=A9?= Date: Wed, 21 Dec 2016 03:16:40 +0100 Subject: [PATCH 4/8] Added missing setCutoff + setMaxMatches Added missing setCutoff + setMaxMatches into Adapter to control query output. --- src/Pagerfanta/Adapter/SphinxAdapter.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Pagerfanta/Adapter/SphinxAdapter.php b/src/Pagerfanta/Adapter/SphinxAdapter.php index 9ee8c77d..ca61a301 100644 --- a/src/Pagerfanta/Adapter/SphinxAdapter.php +++ b/src/Pagerfanta/Adapter/SphinxAdapter.php @@ -19,6 +19,8 @@ class SphinxAdapter implements AdapterInterface private $index; private $comment; private $results; + private $maxMatches = 0; + private $cutoff = 0; /** * Constructor. @@ -48,13 +50,33 @@ public function getNbResults() return $this->results['total_found']; } + /* + * setMaxMatches + * + * @param $maxMatches Controls how much matches searchd will keep in RAM while searching. + */ + public function setMaxMatches($maxMatches) + { + $this->maxMatches = $maxMatches; + } + + /* + * setCutoff + * + * @param $maxMatches Used for advanced performance control. It tells searchd to forcibly stop search query once cutoff matches have been found and processed. + */ + public function setCutoff($cutoff) + { + $this->cutoff = $cutoff; + } + /** * {@inheritdoc} */ public function getSlice($offset, $limit) { // Set limit - $this->client->setLimits($offset,$limit); + $this->client->setLimits($offset, $limit, $this->maxMatches, $this->cutoff); return $this->results = $this->client ->query($this->query, $this->index, $this->comment); From b33308a084550da91341eed1f42813258eae4b4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maikel=20Doez=C3=A9?= Date: Wed, 21 Dec 2016 03:23:32 +0100 Subject: [PATCH 5/8] Use total instead of total_found Total_found is not corresponding with the actual available pages, but with the possible available results. Changed to total. --- src/Pagerfanta/Adapter/SphinxAdapter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Pagerfanta/Adapter/SphinxAdapter.php b/src/Pagerfanta/Adapter/SphinxAdapter.php index ca61a301..f7948c98 100644 --- a/src/Pagerfanta/Adapter/SphinxAdapter.php +++ b/src/Pagerfanta/Adapter/SphinxAdapter.php @@ -44,10 +44,10 @@ public function __construct(SphinxClient $client, $query, $index = "*", $comment public function getNbResults() { if (!$this->results) { - return $this->client->query($this->query, $this->index, $this->comment)['total_found']; + return $this->client->query($this->query, $this->index, $this->comment)['total']; } - return $this->results['total_found']; + return $this->results['total']; } /* From fa816e58e29c216f48ac6c3fccac1e6bf42937f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maikel=20Doez=C3=A9?= Date: Thu, 7 Sep 2017 08:49:51 +0200 Subject: [PATCH 6/8] Implemented feedback of sampart Removed copyright notice, fixed docblocks added getters and changed readme. --- README.md | 2 +- src/Pagerfanta/Adapter/SphinxAdapter.php | 56 ++++++++++++++++++------ 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 4b92a3a3..6f86ab10 100644 --- a/README.md +++ b/README.md @@ -316,7 +316,7 @@ $adapter = new SolariumAdapter($solarium, $query); ``` ### SphinxAdapter -To paginate a [Sphinx](http://php.net/manual/en/class.sphinxclient.php) query: +To paginate a [Sphinx](http://sphinxsearch.com/) query: ```php - * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ @@ -25,19 +21,19 @@ class SphinxAdapter implements AdapterInterface /** * Constructor. * - * @param SphinxClient $client A Sphinx client. - * @param $query A Sphinx query. - * @param $index A Sphinx index. - * @param $comment A Sphinx comment. - */ + * @param SphinxClient $client A Sphinx client. + * @param string $query A Sphinx query. + * @param string $index A Sphinx index. + * @param string $comment A Sphinx comment. + */ public function __construct(SphinxClient $client, $query, $index = "*", $comment = "") { - $this->client = $client; + $this->client = $client; $this->query = $query; $this->index = $index; $this->comment = $comment; - } - + } + /** * {@inheritdoc} */ @@ -53,21 +49,53 @@ public function getNbResults() /* * setMaxMatches * - * @param $maxMatches Controls how much matches searchd will keep in RAM while searching. + * @param int $maxMatches Controls how much matches searchd will keep in RAM while searching. + * + * @return SphinxAdapter */ public function setMaxMatches($maxMatches) { $this->maxMatches = $maxMatches; + + return $this; + } + + /* + * getMaxMatches + * + * @param void + * + * @return int + */ + public function getMaxMatches() + { + return $this->maxMatches; } /* * setCutoff * - * @param $maxMatches Used for advanced performance control. It tells searchd to forcibly stop search query once cutoff matches have been found and processed. + * @param $cutoff Used for advanced performance control. It tells searchd to forcibly stop search query once cutoff matches have been found and processed. + * + * @return SphinxAdapter */ public function setCutoff($cutoff) { $this->cutoff = $cutoff; + + return $this; + } + + /* + * getCutoff + * + * @param void + * + * @return int + */ + public function getCutoff() + { + return $this->cutoff; } /** From e22167ead2a6f2fe31871e2b0488e1fe08ad600f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maikel=20Doez=C3=A9?= Date: Sat, 9 Sep 2017 15:53:18 +0200 Subject: [PATCH 7/8] Fixed tabbing --- src/Pagerfanta/Adapter/SphinxAdapter.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Pagerfanta/Adapter/SphinxAdapter.php b/src/Pagerfanta/Adapter/SphinxAdapter.php index a920dcf1..6cbf6a5c 100644 --- a/src/Pagerfanta/Adapter/SphinxAdapter.php +++ b/src/Pagerfanta/Adapter/SphinxAdapter.php @@ -10,31 +10,31 @@ class SphinxAdapter implements AdapterInterface { - private $client; - private $query; + private $client; + private $query; private $index; private $comment; private $results; private $maxMatches = 0; private $cutoff = 0; - - /** - * Constructor. - * + + /** + * Constructor. + * * @param SphinxClient $client A Sphinx client. * @param string $query A Sphinx query. * @param string $index A Sphinx index. * @param string $comment A Sphinx comment. */ - public function __construct(SphinxClient $client, $query, $index = "*", $comment = "") - { + public function __construct(SphinxClient $client, $query, $index = "*", $comment = "") + { $this->client = $client; $this->query = $query; $this->index = $index; $this->comment = $comment; } - /** + /** * {@inheritdoc} */ public function getNbResults() @@ -109,4 +109,4 @@ public function getSlice($offset, $limit) return $this->results = $this->client ->query($this->query, $this->index, $this->comment); } -} +} \ No newline at end of file From afc43c4e474614285074e4d5c0c86a68a90d9b49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maikel=20Doez=C3=A9?= Date: Sat, 9 Sep 2017 16:25:35 +0200 Subject: [PATCH 8/8] Removed use SphinxClient --- src/Pagerfanta/Adapter/SphinxAdapter.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Pagerfanta/Adapter/SphinxAdapter.php b/src/Pagerfanta/Adapter/SphinxAdapter.php index 6cbf6a5c..f83b535d 100644 --- a/src/Pagerfanta/Adapter/SphinxAdapter.php +++ b/src/Pagerfanta/Adapter/SphinxAdapter.php @@ -3,10 +3,8 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ - -namespace Pagerfanta\Adapter; -use SphinxClient; +namespace Pagerfanta\Adapter; class SphinxAdapter implements AdapterInterface { @@ -26,7 +24,7 @@ class SphinxAdapter implements AdapterInterface * @param string $index A Sphinx index. * @param string $comment A Sphinx comment. */ - public function __construct(SphinxClient $client, $query, $index = "*", $comment = "") + public function __construct(\SphinxClient $client, $query, $index = "*", $comment = "") { $this->client = $client; $this->query = $query;