Skip to content

Commit 3cac0ec

Browse files
committed
Show dynamic search index and set type null for standard indexes
1 parent d30ab34 commit 3cac0ec

File tree

3 files changed

+56
-18
lines changed

3 files changed

+56
-18
lines changed

src/Schema/Builder.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public function getIndexes($table)
243243
$index->isText() => 'text',
244244
$index->is2dSphere() => '2dsphere',
245245
$index->isTtl() => 'ttl',
246-
default => 'default',
246+
default => null,
247247
},
248248
'unique' => $index->isUnique(),
249249
];
@@ -255,7 +255,8 @@ public function getIndexes($table)
255255
$indexList[] = [
256256
'name' => $index['name'],
257257
'columns' => match ($index['type']) {
258-
'search' => array_keys($index['latestDefinition']['mappings']['fields'] ?? []),
258+
'search' => ($index['latestDefinition']['mappings']['dynamic'] ?? false ? ['dynamic'] : [])
259+
+ array_keys($index['latestDefinition']['mappings']['fields'] ?? []),
259260
'vectorSearch' => array_column($index['latestDefinition']['fields'], 'path'),
260261
},
261262
'type' => $index['type'],

tests/AtlasSearchTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use function assert;
1212
use function usleep;
13+
use function usort;
1314

1415
class AtlasSearchTest extends TestCase
1516
{
@@ -54,6 +55,10 @@ public function setUp(): void
5455
],
5556
]);
5657

58+
$collection->createSearchIndex([
59+
'mappings' => ['dynamic' => true],
60+
], ['name' => 'dynamic_search']);
61+
5762
$collection->createSearchIndex([
5863
'fields' => [
5964
['type' => 'vector', 'numDimensions' => 16, 'path' => 'vector16', 'similarity' => 'cosine'],
@@ -91,14 +96,18 @@ public function testGetIndexes()
9196
{
9297
$indexes = Schema::getIndexes('books');
9398

94-
self::assertCount(3, $indexes);
99+
self::assertIsArray($indexes);
100+
self::assertCount(4, $indexes);
101+
102+
// Order of indexes is not guaranteed
103+
usort($indexes, fn ($a, $b) => $a['name'] <=> $b['name']);
95104

96105
$expected = [
97106
[
98107
'name' => '_id_',
99108
'columns' => ['_id'],
100109
'primary' => true,
101-
'type' => 'default',
110+
'type' => null,
102111
'unique' => false,
103112
],
104113
[
@@ -108,6 +117,13 @@ public function testGetIndexes()
108117
'primary' => false,
109118
'unique' => false,
110119
],
120+
[
121+
'name' => 'dynamic_search',
122+
'columns' => ['dynamic'],
123+
'type' => 'search',
124+
'primary' => false,
125+
'unique' => false,
126+
],
111127
[
112128
'name' => 'vector',
113129
'columns' => ['vector16', 'vector32'],

tests/SchemaTest.php

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -482,20 +482,41 @@ public function testGetIndexes()
482482
$collection->string('mykey3')->index();
483483
});
484484
$indexes = Schema::getIndexes('newcollection');
485-
$this->assertIsArray($indexes);
486-
$this->assertCount(4, $indexes);
487-
488-
$indexes = collect($indexes)->keyBy('name');
489-
490-
$indexes->each(function ($index) {
491-
$this->assertIsString($index['name']);
492-
$this->assertIsString($index['type']);
493-
$this->assertIsArray($index['columns']);
494-
$this->assertIsBool($index['unique']);
495-
$this->assertIsBool($index['primary']);
496-
});
497-
$this->assertTrue($indexes->get('_id_')['primary']);
498-
$this->assertTrue($indexes->get('unique_index_1')['unique']);
485+
self::assertIsArray($indexes);
486+
self::assertCount(4, $indexes);
487+
488+
$expected = [
489+
[
490+
'name' => '_id_',
491+
'columns' => ['_id'],
492+
'primary' => true,
493+
'type' => null,
494+
'unique' => false,
495+
],
496+
[
497+
'name' => 'mykey1_1',
498+
'columns' => ['mykey1'],
499+
'primary' => false,
500+
'type' => null,
501+
'unique' => false,
502+
],
503+
[
504+
'name' => 'unique_index_1',
505+
'columns' => ['unique_index'],
506+
'primary' => false,
507+
'type' => null,
508+
'unique' => true,
509+
],
510+
[
511+
'name' => 'mykey3_1',
512+
'columns' => ['mykey3'],
513+
'primary' => false,
514+
'type' => null,
515+
'unique' => false,
516+
],
517+
];
518+
519+
self::assertSame($expected, $indexes);
499520

500521
// Non-existent collection
501522
$indexes = Schema::getIndexes('missing');

0 commit comments

Comments
 (0)