1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under `AGPL-3.0-only, proprietary` license[s]. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/manganel |
7
|
|
|
* @license AGPL-3.0-only, proprietary |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
10
|
|
|
* @link https://maslosoft.com/manganel/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Maslosoft\Manganel\Adapters\Finder; |
14
|
|
|
|
15
|
|
|
use Maslosoft\Mangan\Interfaces\Adapters\FinderCursorInterface; |
16
|
|
|
use Maslosoft\Manganel\Decorators\IndexDecorator; |
17
|
|
|
use Maslosoft\Manganel\Decorators\MaxScoreDecorator; |
18
|
|
|
use Maslosoft\Manganel\Decorators\ScoreDecorator; |
19
|
|
|
use Maslosoft\Manganel\QueryBuilder; |
20
|
|
|
use Maslosoft\Manganel\SearchCriteria; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* ElasticSearchCursor |
24
|
|
|
* |
25
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
26
|
|
|
*/ |
27
|
|
|
class ElasticSearchCursor implements FinderCursorInterface |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Whether query was sent to search server |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
private $isExecuted = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @var QueryBuilder |
39
|
|
|
*/ |
40
|
|
|
private $qb = null; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
private $data = []; |
47
|
|
|
|
48
|
60 |
|
public function __construct(QueryBuilder $qb) |
49
|
|
|
{ |
50
|
60 |
|
$this->qb = $qb; |
51
|
60 |
|
$criteria = $qb->getCriteria(); |
52
|
60 |
|
if (empty($criteria)) |
53
|
|
|
{ |
54
|
|
|
$this->qb->setCriteria(new SearchCriteria()); |
55
|
|
|
} |
56
|
60 |
|
} |
57
|
|
|
|
58
|
47 |
|
public function limit($num) |
59
|
|
|
{ |
60
|
47 |
|
$this->qb->getCriteria()->limit($num); |
61
|
47 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
42 |
|
public function skip($num) |
65
|
|
|
{ |
66
|
42 |
|
$this->qb->getCriteria()->offset($num); |
67
|
42 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
48 |
|
public function sort(array $fields) |
71
|
|
|
{ |
72
|
48 |
|
$this->qb->getCriteria()->setSort($fields); |
73
|
48 |
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
public function fields(array $fields) |
77
|
|
|
{ |
78
|
1 |
|
$this->qb->getCriteria()->select($fields); |
79
|
1 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// <editor-fold defaultstate="collapsed" desc="Countable impl"> |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* |
86
|
|
|
* @return int |
87
|
|
|
*/ |
88
|
6 |
|
public function count() |
89
|
|
|
{ |
90
|
6 |
|
$this->execute(); |
91
|
6 |
|
return count($this->data); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// </editor-fold> |
95
|
|
|
// <editor-fold defaultstate="collapsed" desc="Iterator impl"> |
96
|
51 |
|
public function current() |
97
|
|
|
{ |
98
|
51 |
|
$this->execute(); |
99
|
51 |
|
return current($this->data); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function key() |
103
|
|
|
{ |
104
|
|
|
$this->execute(); |
105
|
|
|
return key($this->data); |
106
|
|
|
} |
107
|
|
|
|
108
|
44 |
|
public function next() |
109
|
|
|
{ |
110
|
44 |
|
$this->execute(); |
111
|
44 |
|
next($this->data); |
112
|
44 |
|
} |
113
|
|
|
|
114
|
48 |
|
public function rewind() |
115
|
|
|
{ |
116
|
48 |
|
$this->execute(); |
117
|
48 |
|
return reset($this->data); |
118
|
|
|
} |
119
|
|
|
|
120
|
48 |
|
public function valid() |
121
|
|
|
{ |
122
|
48 |
|
$this->execute(); |
123
|
48 |
|
$key = key($this->data); |
124
|
48 |
|
return ($key !== null && $key !== false); |
125
|
|
|
} |
126
|
|
|
|
127
|
60 |
|
private function execute() |
128
|
|
|
{ |
129
|
60 |
|
if (!$this->isExecuted) |
130
|
|
|
{ |
131
|
60 |
|
$this->isExecuted = true; |
132
|
60 |
|
$results = []; |
133
|
60 |
|
$data = $this->qb->search(null, $results); |
134
|
|
|
|
135
|
|
|
// Something went wrong |
136
|
60 |
|
if(empty($results['hits'])) |
137
|
|
|
{ |
138
|
|
|
$this->data = []; |
139
|
|
|
return; |
140
|
|
|
} |
141
|
60 |
|
$maxScore = $results['hits']['max_score']; |
142
|
60 |
|
foreach ($data as $result) |
143
|
|
|
{ |
144
|
56 |
|
$document = $result['_source']; |
145
|
|
|
/** |
146
|
|
|
* TODO Maybe refactor it into plugable interface, with params: |
147
|
|
|
* $document, |
148
|
|
|
* $result, |
149
|
|
|
* $results |
150
|
|
|
*/ |
151
|
56 |
|
$document[IndexDecorator::Key] = $result['_index']; |
152
|
56 |
|
$document[ScoreDecorator::Key] = $result['_score']; |
153
|
56 |
|
$document[MaxScoreDecorator::Key] = $maxScore; |
154
|
|
|
|
155
|
56 |
|
$this->data[] = $document; |
156
|
|
|
} |
157
|
|
|
} |
158
|
60 |
|
} |
159
|
|
|
|
160
|
|
|
// </editor-fold> |
161
|
|
|
} |
162
|
|
|
|