Skip to content

Commit b844e6e

Browse files
committed
Prepare field names for search indexes and vector search indexes
1 parent c813a57 commit b844e6e

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

lib/Doctrine/ODM/MongoDB/SchemaManager.php

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
/**
4040
* @phpstan-import-type IndexMapping from ClassMetadata
4141
* @phpstan-import-type IndexOptions from ClassMetadata
42+
* @phpstan-import-type SearchIndexMapping from ClassMetadata
4243
*/
4344
final class SchemaManager
4445
{
@@ -355,7 +356,7 @@ public function createDocumentSearchIndexes(string $documentName): void
355356
throw new InvalidArgumentException('Cannot create search indexes for mapped super classes, embedded documents, query result documents, or views.');
356357
}
357358

358-
$searchIndexes = $class->getSearchIndexes();
359+
$searchIndexes = $this->prepareSearchIndexes($class);
359360

360361
if (empty($searchIndexes)) {
361362
return;
@@ -410,7 +411,7 @@ public function updateDocumentSearchIndexes(string $documentName): void
410411
throw new InvalidArgumentException('Cannot update search indexes for mapped super classes, embedded documents, query result documents, or views.');
411412
}
412413

413-
$searchIndexes = $class->getSearchIndexes();
414+
$searchIndexes = $this->prepareSearchIndexes($class);
414415
$collection = $this->dm->getDocumentCollection($class->name);
415416

416417
$definedNames = array_column($searchIndexes, 'name');
@@ -486,6 +487,59 @@ public function deleteDocumentSearchIndexes(string $documentName): void
486487
}
487488
}
488489

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+
489543
/**
490544
* Ensure collection validators are up to date for all mapped document classes.
491545
*/

0 commit comments

Comments
 (0)