1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under `AGPL, Commercial` license[s]. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/manganel |
7
|
|
|
* @license AGPL, Commercial |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
10
|
|
|
* @link http://maslosoft.com/manganel/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Maslosoft\Manganel; |
14
|
|
|
|
15
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
16
|
|
|
use Maslosoft\Mangan\Helpers\CollectionNamer; |
17
|
|
|
use Maslosoft\Mangan\Interfaces\CriteriaAwareInterface; |
18
|
|
|
use Maslosoft\Mangan\Traits\CriteriaAwareTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* QueryBuilder |
22
|
|
|
* @method SearchCriteria getCriteria() |
23
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
24
|
|
|
*/ |
25
|
|
|
class QueryBuilder implements CriteriaAwareInterface |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
use CriteriaAwareTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Manganel instance |
32
|
|
|
* @var Manganel |
33
|
|
|
*/ |
34
|
|
|
private $manganel = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Annotated model |
38
|
|
|
* @var AnnotatedInterface[] |
39
|
|
|
*/ |
40
|
|
|
private $models = []; |
41
|
|
|
|
42
|
7 |
|
public function __construct($model = null) |
43
|
|
|
{ |
44
|
7 |
|
if (!empty($model)) |
45
|
7 |
|
{ |
46
|
7 |
|
$this->models[] = $model; |
47
|
7 |
|
} |
48
|
7 |
|
if (!empty($model)) |
49
|
7 |
|
{ |
50
|
7 |
|
$this->manganel = Manganel::create($model); |
51
|
7 |
|
} |
52
|
|
|
else |
53
|
|
|
{ |
54
|
4 |
|
$this->manganel = Manganel::fly(); |
55
|
|
|
} |
56
|
7 |
|
} |
57
|
|
|
|
58
|
4 |
|
public function add($model) |
59
|
|
|
{ |
60
|
4 |
|
if (is_array($model)) |
61
|
4 |
|
{ |
62
|
|
|
foreach ($model as $m) |
63
|
|
|
{ |
64
|
|
|
$this->models[] = $m; |
65
|
|
|
} |
66
|
|
|
return; |
67
|
|
|
} |
68
|
4 |
|
$this->models[] = $model; |
69
|
4 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* |
73
|
|
|
* @param string $q |
74
|
|
|
* @return int |
75
|
|
|
*/ |
76
|
6 |
|
public function count($q = null) |
77
|
|
|
{ |
78
|
6 |
|
$params = $this->getParams($q); |
79
|
6 |
|
$result = $this->manganel->getClient()->count($params); |
80
|
6 |
|
if (empty($result) && empty($result['count'])) |
81
|
6 |
|
{ |
82
|
|
|
return 0; // @codeCoverageIgnore |
83
|
|
|
} |
84
|
6 |
|
return $result['count']; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get search results |
89
|
|
|
* @param string $q |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
5 |
|
public function search($q = null) |
93
|
|
|
{ |
94
|
5 |
|
$params = $this->getParams($q); |
95
|
5 |
|
$result = $this->manganel->getClient()->search($params); |
96
|
5 |
|
if (empty($result) && empty($result['hits']) && empty($result['hits']['hits'])) |
97
|
5 |
|
{ |
98
|
|
|
return []; // @codeCoverageIgnore |
99
|
|
|
} |
100
|
5 |
|
return $result['hits']['hits']; |
101
|
|
|
} |
102
|
|
|
|
103
|
7 |
|
private function getParams($q = null) |
104
|
|
|
{ |
105
|
7 |
|
$body = []; |
106
|
|
|
// Try to get query from criteria if empty |
107
|
7 |
|
$criteria = $this->getCriteria(); |
108
|
7 |
|
if (null === $q && !empty($criteria)) |
109
|
7 |
|
{ |
110
|
|
|
$q = $criteria->getSearch(); |
111
|
|
|
} |
112
|
|
|
|
113
|
7 |
|
if (null === $q) |
114
|
7 |
|
{ |
115
|
|
|
// Match all documents if query is null |
116
|
|
|
$query = [ |
117
|
1 |
|
'match_all' => [] |
118
|
1 |
|
]; |
119
|
1 |
|
} |
120
|
|
|
else |
121
|
|
|
{ |
122
|
|
|
// Use query string matching |
123
|
|
|
$query = [ |
124
|
|
|
'simple_query_string' => [ |
125
|
|
|
'query' => $q |
126
|
6 |
|
] |
127
|
6 |
|
]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// |
131
|
|
|
// TODO: Build somewhat similar query, if criteria has |
132
|
|
|
// ANY conditions. Add conditions to `filter` clause: |
133
|
|
|
// { |
134
|
|
|
// "query": { |
135
|
|
|
// "filtered": { |
136
|
|
|
// "query": { |
137
|
|
|
// "query_string": { |
138
|
|
|
// "query": "jkow OR features" |
139
|
|
|
// } |
140
|
|
|
// }, |
141
|
|
|
// "filter": { |
142
|
|
|
// "term": { |
143
|
|
|
// "published.en": true |
144
|
|
|
// } |
145
|
|
|
// } |
146
|
|
|
// } |
147
|
|
|
// } |
148
|
|
|
// } |
149
|
|
|
// |
150
|
|
|
|
151
|
|
|
|
152
|
7 |
|
$body['query'] = $query; |
153
|
|
|
|
154
|
7 |
|
if (!empty($criteria)) |
155
|
7 |
|
{ |
156
|
4 |
|
if ($criteria->getLimit() || $criteria->getOffset()) |
157
|
4 |
|
{ |
158
|
4 |
|
$body['from'] = $criteria->getOffset(); |
159
|
4 |
|
$body['size'] = $criteria->getLimit(); |
160
|
4 |
|
} |
161
|
4 |
|
} |
162
|
|
|
|
163
|
7 |
|
if (empty($this->models)) |
164
|
7 |
|
{ |
165
|
|
|
$type = '_all'; |
166
|
|
|
} |
167
|
|
|
else |
168
|
|
|
{ |
169
|
7 |
|
$types = []; |
170
|
7 |
|
foreach ($this->models as $model) |
171
|
|
|
{ |
172
|
7 |
|
if (!$model instanceof AnnotatedInterface) |
173
|
7 |
|
{ |
174
|
|
|
throw new \UnexpectedValueException(sprintf('Expected `%s` instance, got `%s`', AnnotatedInterface::class, is_object($model) ? get_class($model) : gettype($model))); |
175
|
|
|
} |
176
|
7 |
|
$types[] = CollectionNamer::nameCollection($model); |
177
|
7 |
|
} |
178
|
7 |
|
$type = implode(',', array_unique($types)); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$params = [ |
182
|
7 |
|
'index' => strtolower($this->manganel->index), |
183
|
7 |
|
'type' => $type, |
184
|
|
|
'body' => $body |
185
|
7 |
|
]; |
186
|
7 |
|
return $params; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
} |
190
|
|
|
|