Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented SphinxAdapter #218

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,22 @@ $query->setQuery('search term');
$adapter = new SolariumAdapter($solarium, $query);
```

### SphinxAdapter
To paginate a [Sphinx](http://sphinxsearch.com/) query:

```php
<?php

use Pagerfanta\Adapter\SphinxAdapter;

$query = '*';
$index = 'example_index';
$comment = '';

$sphinxClient = new SphinxClient();
$adapter = new SphinxAdapter($sphinxClient, $query, $index, $comment);
```

### FixedAdapter

Best used when you need to do a custom paging solution and
Expand Down
110 changes: 110 additions & 0 deletions src/Pagerfanta/Adapter/SphinxAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
/*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Pagerfanta\Adapter;

class SphinxAdapter implements AdapterInterface
{
private $client;
private $query;
private $index;
private $comment;
private $results;
private $maxMatches = 0;
private $cutoff = 0;

/**
* 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 = "")
{
$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'];
}

return $this->results['total'];
}

/*
* setMaxMatches
*
* @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 $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;
}

/**
* {@inheritdoc}
*/
public function getSlice($offset, $limit)
{
// Set limit
$this->client->setLimits($offset, $limit, $this->maxMatches, $this->cutoff);

return $this->results = $this->client
->query($this->query, $this->index, $this->comment);
}
}