1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace LaravelFreelancerNL\FluentAQL\AQL; |
||
6 | |||
7 | use LaravelFreelancerNL\FluentAQL\Expressions\Expression; |
||
8 | use LaravelFreelancerNL\FluentAQL\Expressions\FunctionExpression; |
||
9 | use LaravelFreelancerNL\FluentAQL\QueryBuilder; |
||
10 | |||
11 | /** |
||
12 | * Geo AQL functions. |
||
13 | * |
||
14 | * @see https://www.arangodb.com/docs/stable/aql/functions-arangosearch.html |
||
15 | */ |
||
16 | trait HasArangoSearchFunctions |
||
17 | { |
||
18 | /** |
||
19 | * set the analyzer for the given search expression |
||
20 | * |
||
21 | * https://www.arangodb.com/docs/stable/aql/functions-arangosearch.html#analyzer |
||
22 | */ |
||
23 | 8 | public function analyzer(): FunctionExpression |
|
24 | { |
||
25 | 8 | $arguments = func_get_args(); |
|
26 | |||
27 | /** @var string $analyzer */ |
||
28 | 8 | $analyzer = array_pop($arguments); |
|
29 | |||
30 | 8 | $predicates = $arguments; |
|
31 | 8 | if (! is_array($predicates[0])) { |
|
32 | 7 | $predicates = [[ |
|
33 | ...$predicates |
||
34 | ]]; |
||
35 | } |
||
36 | 8 | $preppedArguments = [ |
|
37 | 'predicates' => $predicates, |
||
38 | 'analyzer' => $analyzer |
||
39 | ]; |
||
40 | 8 | return new FunctionExpression('ANALYZER', $preppedArguments); |
|
41 | } |
||
42 | |||
43 | /** |
||
44 | * Override the boost value of the search expression |
||
45 | * |
||
46 | * https://www.arangodb.com/docs/stable/aql/functions-arangosearch.html#boost |
||
47 | */ |
||
48 | 2 | public function boost(): FunctionExpression |
|
49 | { |
||
50 | 2 | $arguments = func_get_args(); |
|
51 | |||
52 | /** @var int|float $boost */ |
||
53 | 2 | $boost = array_pop($arguments); |
|
54 | |||
55 | 2 | $predicates = $arguments; |
|
56 | 2 | if (! is_array($predicates[0])) { |
|
57 | 1 | $predicates = [[ |
|
58 | ...$predicates |
||
59 | ]]; |
||
60 | } |
||
61 | 2 | $preppedArguments = [ |
|
62 | 'predicates' => $predicates, |
||
63 | 'boost' => $boost |
||
64 | ]; |
||
65 | 2 | return new FunctionExpression('BOOST', $preppedArguments); |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * Sorts documents using the Best Matching 25 algorithm (Okapi BM25). |
||
70 | * |
||
71 | * https://www.arangodb.com/docs/stable/aql/functions-arangosearch.html#bm25 |
||
72 | */ |
||
73 | 3 | public function bm25( |
|
74 | string|Expression $doc, |
||
75 | int|float|Expression|QueryBuilder $k = null, |
||
76 | int|float|Expression|QueryBuilder $b = null |
||
77 | ): FunctionExpression { |
||
78 | 3 | $arguments = [ |
|
79 | $doc, |
||
80 | $k, |
||
81 | $b |
||
82 | ]; |
||
83 | 3 | $arguments = array_filter($arguments); |
|
84 | |||
85 | 3 | return new FunctionExpression('BM25', $arguments); |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * Sorts documents using the term frequency–inverse document frequency algorithm (TF-IDF). |
||
90 | * |
||
91 | * https://www.arangodb.com/docs/stable/aql/functions-arangosearch.html#tfidf |
||
92 | */ |
||
93 | 2 | public function tfidf( |
|
94 | string|Expression $doc, |
||
95 | bool|Expression|QueryBuilder $normalize = null |
||
96 | ): FunctionExpression { |
||
97 | 2 | $arguments = [ |
|
98 | $doc, |
||
99 | ]; |
||
100 | 2 | if (isset($normalize)) { |
|
101 | 1 | $arguments[] = $normalize; |
|
102 | } |
||
103 | |||
104 | 2 | return new FunctionExpression('TFIDF', $arguments); |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * Match documents where the attribute at path is present and is of the specified data type. |
||
109 | * |
||
110 | * https://www.arangodb.com/docs/stable/aql/functions-arangosearch.html#exists |
||
111 | * |
||
112 | * @param Expression|QueryBuilder|string $path |
||
113 | * @param Expression|QueryBuilder|null|string $type |
||
114 | * |
||
115 | * @return FunctionExpression |
||
116 | */ |
||
117 | 2 | public function exists( |
|
118 | Expression|QueryBuilder|string $path, |
||
119 | Expression|null|string|QueryBuilder $type = null |
||
120 | ): FunctionExpression { |
||
121 | 2 | $arguments = [ |
|
122 | $path, |
||
123 | $type, |
||
124 | ]; |
||
125 | 2 | $arguments = array_filter($arguments); |
|
126 | |||
127 | 2 | return new FunctionExpression('EXISTS', $arguments); |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * Match documents where the attribute at path is present and is of the specified data type. |
||
132 | * |
||
133 | * https://www.arangodb.com/docs/stable/aql/functions-arangosearch.html#in_range |
||
134 | * |
||
135 | * @param string|Expression $path |
||
136 | * @param int|float|string|Expression|QueryBuilder $low |
||
137 | * @param int|float|string|Expression|QueryBuilder $high |
||
138 | * @param bool|Expression|QueryBuilder|null $includeLow |
||
139 | * @param bool|Expression|QueryBuilder|null $includeHigh |
||
140 | * @return FunctionExpression |
||
141 | */ |
||
142 | 2 | public function inRange( |
|
143 | string|Expression $path, |
||
144 | int|float|string|Expression|QueryBuilder $low, |
||
145 | int|float|string|Expression|QueryBuilder $high, |
||
146 | bool|Expression|QueryBuilder $includeLow = null, |
||
147 | bool|Expression|QueryBuilder $includeHigh = null |
||
148 | ): FunctionExpression { |
||
149 | 2 | $arguments = [ |
|
150 | "path" => $path, |
||
151 | "low" => $low, |
||
152 | "high" => $high, |
||
153 | "includeLow" => $includeLow, |
||
154 | "includeHigh" => $includeHigh |
||
155 | ]; |
||
156 | 2 | $arguments = $this->unsetNullValues($arguments); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
157 | |||
158 | 2 | return new FunctionExpression('IN_RANGE', $arguments); |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * Match documents with a Damerau-Levenshtein distance lower than or equal to |
||
163 | * the distance between the stored attribute value and target. |
||
164 | * |
||
165 | * https://www.arangodb.com/docs/stable/aql/functions-arangosearch.html#levenshtein_match |
||
166 | * |
||
167 | * @param string|object $path |
||
168 | * @param string|object $target |
||
169 | * @param int|object $distance |
||
170 | * @param null|bool|object $transpositions |
||
171 | * @param null|int|object $maxTerms |
||
172 | * @param null|string|object $prefix |
||
173 | * @return FunctionExpression |
||
174 | */ |
||
175 | 2 | public function levenshteinMatch( |
|
176 | mixed $path, |
||
177 | mixed $target, |
||
178 | mixed $distance, |
||
179 | mixed $transpositions = null, |
||
180 | mixed $maxTerms = null, |
||
181 | mixed $prefix = null |
||
182 | ): FunctionExpression { |
||
183 | 2 | $arguments = [ |
|
184 | "path" => $path, |
||
185 | "target" => $target, |
||
186 | "distance" => $distance, |
||
187 | "transpositions" => $transpositions, |
||
188 | "maxTerms" => $maxTerms, |
||
189 | "prefix" => $prefix, |
||
190 | ]; |
||
191 | 2 | $arguments = $this->unsetNullValues($arguments); |
|
192 | |||
193 | 2 | return new FunctionExpression('LEVENSHTEIN_MATCH', $arguments); |
|
194 | } |
||
195 | |||
196 | /** |
||
197 | * Check whether the pattern search is contained in the attribute denoted by path, using wildcard matching. |
||
198 | * |
||
199 | * https://www.arangodb.com/docs/stable/aql/functions-arangosearch.html#like |
||
200 | */ |
||
201 | 1 | public function like( |
|
202 | string|object $path, |
||
203 | mixed $search |
||
204 | ): FunctionExpression { |
||
205 | 1 | $arguments = [ |
|
206 | "path" => $path, |
||
207 | "search" => $search, |
||
208 | ]; |
||
209 | |||
210 | 1 | return new FunctionExpression('LIKE', $arguments); |
|
211 | } |
||
212 | |||
213 | /** |
||
214 | * Match documents with a Damerau-Levenshtein distance lower than or equal to |
||
215 | * the distance between the stored attribute value and target. |
||
216 | * |
||
217 | * https://www.arangodb.com/docs/stable/aql/functions-arangosearch.html#levenshtein_match |
||
218 | * |
||
219 | * @param string|object $path |
||
220 | * @param string|object $target |
||
221 | * @param null|int|float|object $threshold |
||
222 | * @param null|string|object $analyzer |
||
223 | * @return FunctionExpression |
||
224 | */ |
||
225 | 2 | public function nGramMatch( |
|
226 | mixed $path, |
||
227 | mixed $target, |
||
228 | mixed $threshold = null, |
||
229 | mixed $analyzer = null |
||
230 | ): FunctionExpression { |
||
231 | 2 | $arguments = [ |
|
232 | "path" => $path, |
||
233 | "target" => $target, |
||
234 | "threshold" => $threshold, |
||
235 | "analyzer" => $analyzer |
||
236 | ]; |
||
237 | 2 | $arguments = $this->unsetNullValues($arguments); |
|
238 | |||
239 | 2 | return new FunctionExpression('NGRAM_MATCH', $arguments); |
|
240 | } |
||
241 | |||
242 | /** |
||
243 | * Search for a phrase in the referenced attribute. |
||
244 | * |
||
245 | * https://www.arangodb.com/docs/stable/aql/functions-arangosearch.html#phrase |
||
246 | */ |
||
247 | 3 | public function phrase(): FunctionExpression |
|
248 | { |
||
249 | 3 | $arguments = func_get_args(); |
|
250 | |||
251 | 3 | return new FunctionExpression('PHRASE', $arguments); |
|
252 | } |
||
253 | } |
||
254 |