|
42 | 42 | use function is_iterable;
|
43 | 43 | use function is_string;
|
44 | 44 | use function iterator_to_array;
|
| 45 | +use function method_exists; |
45 | 46 | use function preg_quote;
|
46 | 47 | use function sleep;
|
47 | 48 | use function sprintf;
|
@@ -188,20 +189,28 @@ protected function performSearch(Builder $builder, ?int $offset = null): array
|
188 | 189 | return $result instanceof CursorInterface ? $result->toArray() : $result;
|
189 | 190 | }
|
190 | 191 |
|
191 |
| - $compound = []; |
192 |
| - |
193 |
| - // Query String |
| 192 | + $compound = [ |
| 193 | + 'must' => [ |
| 194 | + [ |
| 195 | + 'text' => [ |
| 196 | + 'query' => $builder->query, |
| 197 | + 'path' => ['wildcard' => '*'], |
| 198 | + 'fuzzy' => ['maxEdits' => 2], |
| 199 | + ], |
| 200 | + ], |
| 201 | + ], |
| 202 | + ]; |
194 | 203 |
|
195 | 204 | foreach ($builder->wheres as $field => $value) {
|
196 |
| - $compound['filter']['equals'][] = ['path' => $field, 'value' => $value]; |
| 205 | + $compound['filter'][] = ['equals' => ['path' => $field, 'value' => $value]]; |
197 | 206 | }
|
198 | 207 |
|
199 | 208 | foreach ($builder->whereIns as $field => $value) {
|
200 |
| - $compound['filter']['in'][] = ['path' => $field, 'value' => $value]; |
| 209 | + $compound['filter'][] = ['in' => ['path' => $field, 'value' => $value]]; |
201 | 210 | }
|
202 | 211 |
|
203 | 212 | foreach ($builder->whereNotIns as $field => $value) {
|
204 |
| - $compound['mustNot']['in'][] = ['path' => $field, 'value' => $value]; |
| 213 | + $compound['mustNot'][] = ['in' => ['path' => $field, 'value' => $value]]; |
205 | 214 | }
|
206 | 215 |
|
207 | 216 | $pipeline = [
|
@@ -236,27 +245,6 @@ protected function performSearch(Builder $builder, ?int $offset = null): array
|
236 | 245 | return $collection->aggregate($pipeline, $options)->toArray();
|
237 | 246 | }
|
238 | 247 |
|
239 |
| - /** |
240 |
| - * Get the filter array for the query. |
241 |
| - * |
242 |
| - * @return string |
243 |
| - */ |
244 |
| - protected function filters(Builder $builder): array |
245 |
| - { |
246 |
| - $filters = $builder->wheres; |
247 |
| - |
248 |
| - // https://www.mongodb.com/docs/atlas/atlas-search/in/ |
249 |
| - foreach ($builder->whereIns as $field => $values) { |
250 |
| - $filters['in'][] = ['path' => $field, 'value' => $values]; |
251 |
| - } |
252 |
| - |
253 |
| - foreach ($builder->whereNotIns as $field => $values) { |
254 |
| - $filters['in'][] = ['path' => $field, 'value' => $values]; |
255 |
| - } |
256 |
| - |
257 |
| - return $filters; |
258 |
| - } |
259 |
| - |
260 | 248 | /**
|
261 | 249 | * Pluck and return the primary keys of the given results.
|
262 | 250 | *
|
@@ -494,6 +482,26 @@ private function performSearchIndexOperation(Closure $closure): void
|
494 | 482 | }
|
495 | 483 | }
|
496 | 484 |
|
| 485 | + private function getMapping(Model $model): array |
| 486 | + { |
| 487 | + $mapping = self::DEFAULT_DEFINITION; |
| 488 | + |
| 489 | + if (method_exists($model, 'searchableMapping')) { |
| 490 | + $mapping = $model->searchableMapping(); |
| 491 | + } |
| 492 | + |
| 493 | + if ($this->usesSoftDelete($model)) { |
| 494 | + // This field is a boolean represented with the integers 0 and 1 |
| 495 | + $mapping['fields']['__soft_deleted'] = [ |
| 496 | + 'type' => 'number', |
| 497 | + 'representation' => 'int64', |
| 498 | + 'indexDoubles' => false, |
| 499 | + ]; |
| 500 | + } |
| 501 | + |
| 502 | + return $mapping; |
| 503 | + } |
| 504 | + |
497 | 505 | /**
|
498 | 506 | * Wait for the callback to return true, use it for asynchronous
|
499 | 507 | * Atlas Search index management operations.
|
|
0 commit comments