|
1
|
|
|
<?php |
|
2
|
|
|
namespace Triadev\Es\Dsl\Dsl; |
|
3
|
|
|
|
|
4
|
|
|
use ONGR\ElasticsearchDSL\BuilderInterface; |
|
5
|
|
|
use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery; |
|
6
|
|
|
use ONGR\ElasticsearchDSL\Sort\FieldSort; |
|
7
|
|
|
use Triadev\Es\Dsl\Dsl\Query\Compound; |
|
8
|
|
|
use Triadev\Es\Dsl\Dsl\Query\Fulltext; |
|
9
|
|
|
use Triadev\Es\Dsl\Dsl\Query\Geo; |
|
10
|
|
|
use Triadev\Es\Dsl\Dsl\Query\InnerHit; |
|
11
|
|
|
use Triadev\Es\Dsl\Dsl\Query\Joining; |
|
12
|
|
|
use Triadev\Es\Dsl\Dsl\Query\Specialized; |
|
13
|
|
|
use Triadev\Es\Dsl\Dsl\Query\TermLevel; |
|
14
|
|
|
use Triadev\Es\Dsl\Facade\ElasticDsl; |
|
15
|
|
|
use Triadev\Es\Dsl\Model\SearchResult; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class AbstractDsl |
|
19
|
|
|
* @package Triadev\Es\Dsl\Dsl |
|
20
|
|
|
* |
|
21
|
|
|
* @method TermLevel termLevel() |
|
22
|
|
|
* @method Fulltext fulltext() |
|
23
|
|
|
* @method Geo geo() |
|
24
|
|
|
* @method Compound compound() |
|
25
|
|
|
* @method Joining joining() |
|
26
|
|
|
* @method Specialized specialized() |
|
27
|
|
|
* @method InnerHit innerHit() |
|
28
|
|
|
*/ |
|
29
|
|
|
abstract class AbstractDsl extends AbstractSearch |
|
30
|
|
|
{ |
|
31
|
|
|
/** @var string */ |
|
32
|
|
|
private $boolState = BoolQuery::MUST; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Append |
|
36
|
|
|
* |
|
37
|
|
|
* @param BuilderInterface $query |
|
38
|
|
|
* @return AbstractDsl|Search|TermLevel|Compound|Fulltext|Geo|InnerHit|Joining|Specialized |
|
39
|
|
|
*/ |
|
40
|
20 |
|
public function append(BuilderInterface $query) : AbstractDsl |
|
41
|
|
|
{ |
|
42
|
20 |
|
$this->search->addQuery($query, $this->boolState); |
|
43
|
20 |
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Bool state: must |
|
48
|
|
|
* |
|
49
|
|
|
* @return AbstractDsl|Search|TermLevel|Compound|Fulltext|Geo|InnerHit|Joining|Specialized |
|
50
|
|
|
*/ |
|
51
|
3 |
|
public function must(): AbstractDsl |
|
52
|
|
|
{ |
|
53
|
3 |
|
$this->boolState = BoolQuery::MUST; |
|
54
|
3 |
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Bool state: must not |
|
59
|
|
|
* |
|
60
|
|
|
* @return AbstractDsl|Search|TermLevel|Compound|Fulltext|Geo|InnerHit|Joining|Specialized |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function mustNot(): AbstractDsl |
|
63
|
|
|
{ |
|
64
|
1 |
|
$this->boolState = BoolQuery::MUST_NOT; |
|
65
|
1 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Bool state: should |
|
70
|
|
|
* |
|
71
|
|
|
* @return AbstractDsl|Search|TermLevel|Compound|Fulltext|Geo|InnerHit|Joining|Specialized |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function should(): AbstractDsl |
|
74
|
|
|
{ |
|
75
|
1 |
|
$this->boolState = BoolQuery::SHOULD; |
|
76
|
1 |
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Bool state: filter |
|
81
|
|
|
* |
|
82
|
|
|
* @return AbstractDsl|Search|TermLevel|Compound|Fulltext|Geo|InnerHit|Joining|Specialized |
|
83
|
|
|
*/ |
|
84
|
6 |
|
public function filter(): AbstractDsl |
|
85
|
|
|
{ |
|
86
|
6 |
|
$this->boolState = BoolQuery::FILTER; |
|
87
|
6 |
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Paginate |
|
92
|
|
|
* |
|
93
|
|
|
* @param int $page |
|
94
|
|
|
* @param int $limit |
|
95
|
|
|
* @return AbstractDsl|Search|TermLevel|Compound|Fulltext|Geo|InnerHit|Joining|Specialized |
|
96
|
|
|
*/ |
|
97
|
1 |
|
public function paginate(int $page, int $limit = 25) : AbstractDsl |
|
98
|
|
|
{ |
|
99
|
1 |
|
$this->search->setFrom($limit * ($page - 1))->setSize($limit); |
|
100
|
1 |
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Min score |
|
105
|
|
|
* |
|
106
|
|
|
* @param int $minScore |
|
107
|
|
|
* @return AbstractDsl|Search|TermLevel|Compound|Fulltext|Geo|InnerHit|Joining|Specialized |
|
108
|
|
|
*/ |
|
109
|
|
|
public function minScore(int $minScore) : AbstractDsl |
|
110
|
|
|
{ |
|
111
|
|
|
$this->search->setMinScore($minScore); |
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Sort |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $field |
|
119
|
|
|
* @param string $order |
|
120
|
|
|
* @param array $params |
|
121
|
|
|
* @return AbstractDsl|Search|TermLevel|Compound|Fulltext|Geo|InnerHit|Joining|Specialized |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public function sort(string $field, string $order = FieldSort::DESC, array $params = []) : AbstractDsl |
|
124
|
|
|
{ |
|
125
|
1 |
|
$this->search->addSort(new FieldSort( |
|
126
|
1 |
|
$field, |
|
127
|
1 |
|
$order, |
|
128
|
1 |
|
$params |
|
129
|
|
|
)); |
|
130
|
|
|
|
|
131
|
1 |
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Aggregation |
|
136
|
|
|
* |
|
137
|
|
|
* @param \Closure $aggregation |
|
138
|
|
|
* @return AbstractDsl|Search|TermLevel|Compound|Fulltext|Geo|InnerHit|Joining|Specialized |
|
139
|
|
|
*/ |
|
140
|
1 |
|
public function aggregation(\Closure $aggregation) : AbstractDsl |
|
141
|
|
|
{ |
|
142
|
1 |
|
$aggregation(new Aggregation($this->search)); |
|
143
|
1 |
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Search |
|
148
|
|
|
* |
|
149
|
|
|
* @param \Closure $search |
|
150
|
|
|
* @return AbstractDsl|Search|TermLevel|Compound|Fulltext|Geo|InnerHit|Joining|Specialized |
|
151
|
|
|
*/ |
|
152
|
1 |
|
public function bool(\Closure $search) : AbstractDsl |
|
153
|
|
|
{ |
|
154
|
1 |
|
$searchBuilder = ElasticDsl::search(); |
|
155
|
1 |
|
$search($searchBuilder); |
|
156
|
|
|
|
|
157
|
1 |
|
$this->append($searchBuilder->getQuery()); |
|
158
|
|
|
|
|
159
|
1 |
|
return $this; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Get |
|
164
|
|
|
* |
|
165
|
|
|
* @return SearchResult |
|
166
|
|
|
*/ |
|
167
|
1 |
|
public function get() : SearchResult |
|
168
|
|
|
{ |
|
169
|
1 |
|
return new SearchResult($this->getRaw()); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Get raw search result |
|
174
|
|
|
* |
|
175
|
|
|
* @return array |
|
176
|
|
|
*/ |
|
177
|
1 |
|
public function getRaw() : array |
|
178
|
|
|
{ |
|
179
|
|
|
$params = [ |
|
180
|
1 |
|
'index' => $this->getEsIndex(), |
|
181
|
1 |
|
'body' => $this->toDsl() |
|
182
|
|
|
]; |
|
183
|
|
|
|
|
184
|
1 |
|
if ($this->getEsType()) { |
|
185
|
1 |
|
$params['type'] = $this->getEsType(); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
1 |
|
return ElasticDsl::getEsClient()->search($params); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Call |
|
193
|
|
|
* |
|
194
|
|
|
* @param string $name |
|
195
|
|
|
* @param array $arguments |
|
196
|
|
|
* |
|
197
|
|
|
* @return AbstractDsl|null |
|
198
|
|
|
*/ |
|
199
|
1 |
|
public function __call(string $name, array $arguments) : ?AbstractDsl |
|
200
|
|
|
{ |
|
201
|
|
|
$validFunctions = [ |
|
202
|
1 |
|
'termLevel', |
|
203
|
|
|
'fulltext', |
|
204
|
|
|
'geo', |
|
205
|
|
|
'compound', |
|
206
|
|
|
'joining', |
|
207
|
|
|
'specialized', |
|
208
|
|
|
'innerHit' |
|
209
|
|
|
]; |
|
210
|
|
|
|
|
211
|
1 |
|
if (in_array($name, $validFunctions)) { |
|
212
|
1 |
|
return ElasticDsl::search( |
|
213
|
1 |
|
$this->search, |
|
214
|
1 |
|
$this->getEsIndex(), |
|
215
|
1 |
|
$this->getEsType() |
|
216
|
1 |
|
)->$name(); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
return null; |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|