|
39 | 39 | /**
|
40 | 40 | * @phpstan-import-type IndexMapping from ClassMetadata
|
41 | 41 | * @phpstan-import-type IndexOptions from ClassMetadata
|
| 42 | + * @phpstan-import-type SearchIndexMapping from ClassMetadata |
42 | 43 | */
|
43 | 44 | final class SchemaManager
|
44 | 45 | {
|
@@ -355,7 +356,7 @@ public function createDocumentSearchIndexes(string $documentName): void
|
355 | 356 | throw new InvalidArgumentException('Cannot create search indexes for mapped super classes, embedded documents, query result documents, or views.');
|
356 | 357 | }
|
357 | 358 |
|
358 |
| - $searchIndexes = $class->getSearchIndexes(); |
| 359 | + $searchIndexes = $this->prepareSearchIndexes($class); |
359 | 360 |
|
360 | 361 | if (empty($searchIndexes)) {
|
361 | 362 | return;
|
@@ -410,7 +411,7 @@ public function updateDocumentSearchIndexes(string $documentName): void
|
410 | 411 | throw new InvalidArgumentException('Cannot update search indexes for mapped super classes, embedded documents, query result documents, or views.');
|
411 | 412 | }
|
412 | 413 |
|
413 |
| - $searchIndexes = $class->getSearchIndexes(); |
| 414 | + $searchIndexes = $this->prepareSearchIndexes($class); |
414 | 415 | $collection = $this->dm->getDocumentCollection($class->name);
|
415 | 416 |
|
416 | 417 | $definedNames = array_column($searchIndexes, 'name');
|
@@ -486,6 +487,59 @@ public function deleteDocumentSearchIndexes(string $documentName): void
|
486 | 487 | }
|
487 | 488 | }
|
488 | 489 |
|
| 490 | + /** |
| 491 | + * @param ClassMetadata<object> $class |
| 492 | + * |
| 493 | + * @phpstan-return list<SearchIndexMapping> |
| 494 | + */ |
| 495 | + private function prepareSearchIndexes(ClassMetadata $class): array |
| 496 | + { |
| 497 | + $persister = $this->dm->getUnitOfWork()->getDocumentPersister($class->name); |
| 498 | + $indexes = $class->getSearchIndexes(); |
| 499 | + $newIndexes = []; |
| 500 | + |
| 501 | + foreach ($indexes as $index) { |
| 502 | + $definition = $index['definition']; |
| 503 | + if (is_array($definition['fields'] ?? null)) { |
| 504 | + // Vector Search Index |
| 505 | + $fields = []; |
| 506 | + foreach ($definition['fields'] as $field) { |
| 507 | + $key = $persister->prepareFieldName($field['path']); |
| 508 | + if ($class->hasField($key)) { |
| 509 | + $field['path'] = $class->getFieldMapping($key)['name']; |
| 510 | + } else { |
| 511 | + $field['path'] = $key; |
| 512 | + } |
| 513 | + |
| 514 | + $fields[] = $field; |
| 515 | + } |
| 516 | + |
| 517 | + $definition['fields'] = $fields; |
| 518 | + } elseif (is_array($definition['mappings']['fields'] ?? null)) { |
| 519 | + // Search Index with fields mappings |
| 520 | + $fields = []; |
| 521 | + foreach ($definition['mappings']['fields'] as $name => $field) { |
| 522 | + $key = $persister->prepareFieldName($name); |
| 523 | + if ($class->hasField($key)) { |
| 524 | + $fields[$class->getFieldMapping($key)['name']] = $field; |
| 525 | + } else { |
| 526 | + $fields[$key] = $field; |
| 527 | + } |
| 528 | + } |
| 529 | + |
| 530 | + $definition['mappings']['fields'] = $fields; |
| 531 | + } |
| 532 | + |
| 533 | + $newIndexes[] = [ |
| 534 | + 'type' => $index['type'], |
| 535 | + 'name' => $index['name'], |
| 536 | + 'definition' => $definition, |
| 537 | + ]; |
| 538 | + } |
| 539 | + |
| 540 | + return $newIndexes; |
| 541 | + } |
| 542 | + |
489 | 543 | /**
|
490 | 544 | * Ensure collection validators are up to date for all mapped document classes.
|
491 | 545 | */
|
|
0 commit comments