1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Pagerfanta project. |
5
|
|
|
* |
6
|
|
|
* (c) Tim Nagel <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Pagerfanta\Adapter; |
13
|
|
|
|
14
|
|
|
use Elastica\Query; |
15
|
|
|
use Elastica\SearchableInterface; |
16
|
|
|
|
17
|
|
|
class ElasticaAdapter implements AdapterInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Query |
21
|
|
|
*/ |
22
|
|
|
private $query; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \Elastica\ResultSet |
26
|
|
|
*/ |
27
|
|
|
private $resultSet; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var SearchableInterface |
31
|
|
|
*/ |
32
|
|
|
private $searchable; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var int|null |
36
|
|
|
*/ |
37
|
|
|
private $maxResults; |
38
|
|
|
|
39
|
4 |
|
public function __construct(SearchableInterface $searchable, Query $query, $maxResults = null) |
40
|
|
|
{ |
41
|
4 |
|
$this->searchable = $searchable; |
42
|
4 |
|
$this->query = $query; |
43
|
4 |
|
$this->maxResults = $maxResults; |
44
|
4 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns the number of results. |
48
|
|
|
* |
49
|
|
|
* @return integer The number of results. |
50
|
|
|
*/ |
51
|
2 |
|
public function getNbResults() |
52
|
|
|
{ |
53
|
2 |
|
if (!$this->resultSet) { |
54
|
2 |
|
$totalHits = $this->searchable->search($this->query)->getTotalHits(); |
55
|
2 |
|
} else { |
56
|
|
|
$totalHits = $this->resultSet->getTotalHits(); |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
if (null === $this->maxResults) { |
60
|
1 |
|
return $totalHits; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
return min($totalHits, $this->maxResults); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns the Elastica ResultSet. Will return null if getSlice has not yet been |
68
|
|
|
* called. |
69
|
|
|
* |
70
|
|
|
* @return \Elastica\ResultSet|null |
71
|
|
|
*/ |
72
|
2 |
|
public function getResultSet() |
73
|
|
|
{ |
74
|
2 |
|
return $this->resultSet; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns an slice of the results. |
79
|
|
|
* |
80
|
|
|
* @param integer $offset The offset. |
81
|
|
|
* @param integer $length The length. |
82
|
|
|
* |
83
|
|
|
* @return array|\Traversable The slice. |
84
|
|
|
*/ |
85
|
2 |
|
public function getSlice($offset, $length) |
86
|
|
|
{ |
87
|
2 |
|
return $this->resultSet = $this->searchable->search($this->query, array( |
88
|
2 |
|
'from' => $offset, |
89
|
|
|
'size' => $length |
90
|
2 |
|
)); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|