-
Notifications
You must be signed in to change notification settings - Fork 2
Elasticsearch adapter #208
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| <?php | ||
|
|
||
| namespace Pagerfanta\Adapter; | ||
|
|
||
| use Elasticsearch\Client; | ||
|
|
||
| /** | ||
| * ElasticsearchAdapter. | ||
| * | ||
| * Used with the official Elasticsearch PHP library. | ||
| * | ||
| * @author Danny Sipos <danny@webomelette.com> | ||
| */ | ||
| class ElasticsearchAdapter implements AdapterInterface | ||
| { | ||
|
|
||
| /** | ||
| * The Elasticsearch query parameters. | ||
| * | ||
| * @var array | ||
| */ | ||
| private $params; | ||
|
|
||
| /** | ||
| * The Elasticsearch results | ||
| * | ||
| * @var array | ||
| */ | ||
| private $results; | ||
|
|
||
| /** | ||
| * The Elasticsearch client. | ||
| * | ||
| * @var Client | ||
| */ | ||
| private $client; | ||
|
|
||
| /** | ||
| * Constructor. | ||
| * | ||
| * @param array $params The array of parameters to use for performing the search. | ||
| * @param Client $client The Elasticsearch client. | ||
| */ | ||
| public function __construct(array $params, $client) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing parameter typehint.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed @sstok . Fixed. |
||
| { | ||
| $this->params = $params; | ||
| $this->client = $client; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function getNbResults() | ||
| { | ||
| if (!$this->results) { | ||
| $this->runQuery(); | ||
| } | ||
| return $this->results['hits']['total']; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function getSlice($offset, $length) | ||
| { | ||
| $params = $this->params; | ||
| $params['from'] = $offset; | ||
| $params['size'] = $length; | ||
| $this->runQuery($params); | ||
| return $this->results; | ||
| } | ||
|
|
||
| /** | ||
| * Runs the query and stores the results. | ||
| * | ||
| * @param array $params | ||
| */ | ||
| private function runQuery($params = array()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest typehinting this argument |
||
| { | ||
| if (!$params) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could be simplified, by passing |
||
| $params = $this->params; | ||
| } | ||
| $this->results = $this->client->search($params); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use 4 spaces for the indentation to match the coding standards of the library