|
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 array |
|
36
|
|
|
*/ |
|
37
|
|
|
private $options; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var int|null |
|
41
|
|
|
* |
|
42
|
|
|
* Used to limit the number of totalHits returned by ES. |
|
43
|
|
|
* For more information, see: https://github.com/whiteoctober/Pagerfanta/pull/213#issue-87631892 |
|
44
|
|
|
*/ |
|
45
|
|
|
private $maxResults; |
|
46
|
|
|
|
|
47
|
5 |
|
public function __construct(SearchableInterface $searchable, Query $query, array $options = array(), $maxResults = null) |
|
48
|
|
|
{ |
|
49
|
5 |
|
$this->searchable = $searchable; |
|
50
|
5 |
|
$this->query = $query; |
|
51
|
5 |
|
$this->options = $options; |
|
52
|
5 |
|
$this->maxResults = $maxResults; |
|
53
|
5 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Returns the number of results. |
|
57
|
|
|
* |
|
58
|
|
|
* @return integer The number of results. |
|
59
|
|
|
*/ |
|
60
|
3 |
|
public function getNbResults() |
|
61
|
|
|
{ |
|
62
|
3 |
|
if (!$this->resultSet) { |
|
63
|
2 |
|
$totalHits = $this->searchable->count($this->query); |
|
64
|
|
|
} else { |
|
65
|
1 |
|
$totalHits = $this->resultSet->getTotalHits(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
3 |
|
if (null === $this->maxResults) { |
|
69
|
1 |
|
return $totalHits; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
2 |
|
return min($totalHits, $this->maxResults); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Returns the Elastica ResultSet. Will return null if getSlice has not yet been |
|
77
|
|
|
* called. |
|
78
|
|
|
* |
|
79
|
|
|
* @return \Elastica\ResultSet|null |
|
80
|
|
|
*/ |
|
81
|
2 |
|
public function getResultSet() |
|
82
|
|
|
{ |
|
83
|
2 |
|
return $this->resultSet; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns an slice of the results. |
|
88
|
|
|
* |
|
89
|
|
|
* @param integer $offset The offset. |
|
90
|
|
|
* @param integer $length The length. |
|
91
|
|
|
* |
|
92
|
|
|
* @return array|\Traversable The slice. |
|
93
|
|
|
*/ |
|
94
|
3 |
|
public function getSlice($offset, $length) |
|
95
|
|
|
{ |
|
96
|
3 |
|
return $this->resultSet = $this->searchable->search($this->query, array_merge($this->options, array( |
|
97
|
3 |
|
'from' => $offset, |
|
98
|
3 |
|
'size' => $length |
|
99
|
|
|
))); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|