Skip to content

Commit

Permalink
Show dynamic search index and set type null for standard indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Jan 2, 2025
1 parent d30ab34 commit 3cac0ec
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public function getIndexes($table)
$index->isText() => 'text',
$index->is2dSphere() => '2dsphere',
$index->isTtl() => 'ttl',
default => 'default',
default => null,
},
'unique' => $index->isUnique(),
];
Expand All @@ -255,7 +255,8 @@ public function getIndexes($table)
$indexList[] = [
'name' => $index['name'],
'columns' => match ($index['type']) {
'search' => array_keys($index['latestDefinition']['mappings']['fields'] ?? []),
'search' => ($index['latestDefinition']['mappings']['dynamic'] ?? false ? ['dynamic'] : [])
+ array_keys($index['latestDefinition']['mappings']['fields'] ?? []),
'vectorSearch' => array_column($index['latestDefinition']['fields'], 'path'),
},
'type' => $index['type'],
Expand Down
20 changes: 18 additions & 2 deletions tests/AtlasSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use function assert;
use function usleep;
use function usort;

class AtlasSearchTest extends TestCase
{
Expand Down Expand Up @@ -54,6 +55,10 @@ public function setUp(): void
],
]);

$collection->createSearchIndex([
'mappings' => ['dynamic' => true],
], ['name' => 'dynamic_search']);

$collection->createSearchIndex([
'fields' => [
['type' => 'vector', 'numDimensions' => 16, 'path' => 'vector16', 'similarity' => 'cosine'],
Expand Down Expand Up @@ -91,14 +96,18 @@ public function testGetIndexes()
{
$indexes = Schema::getIndexes('books');

self::assertCount(3, $indexes);
self::assertIsArray($indexes);
self::assertCount(4, $indexes);

// Order of indexes is not guaranteed
usort($indexes, fn ($a, $b) => $a['name'] <=> $b['name']);

$expected = [
[
'name' => '_id_',
'columns' => ['_id'],
'primary' => true,
'type' => 'default',
'type' => null,
'unique' => false,
],
[
Expand All @@ -108,6 +117,13 @@ public function testGetIndexes()
'primary' => false,
'unique' => false,
],
[
'name' => 'dynamic_search',
'columns' => ['dynamic'],
'type' => 'search',
'primary' => false,
'unique' => false,
],
[
'name' => 'vector',
'columns' => ['vector16', 'vector32'],
Expand Down
49 changes: 35 additions & 14 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,20 +482,41 @@ public function testGetIndexes()
$collection->string('mykey3')->index();
});
$indexes = Schema::getIndexes('newcollection');
$this->assertIsArray($indexes);
$this->assertCount(4, $indexes);

$indexes = collect($indexes)->keyBy('name');

$indexes->each(function ($index) {
$this->assertIsString($index['name']);
$this->assertIsString($index['type']);
$this->assertIsArray($index['columns']);
$this->assertIsBool($index['unique']);
$this->assertIsBool($index['primary']);
});
$this->assertTrue($indexes->get('_id_')['primary']);
$this->assertTrue($indexes->get('unique_index_1')['unique']);
self::assertIsArray($indexes);
self::assertCount(4, $indexes);

$expected = [
[
'name' => '_id_',
'columns' => ['_id'],
'primary' => true,
'type' => null,
'unique' => false,
],
[
'name' => 'mykey1_1',
'columns' => ['mykey1'],
'primary' => false,
'type' => null,
'unique' => false,
],
[
'name' => 'unique_index_1',
'columns' => ['unique_index'],
'primary' => false,
'type' => null,
'unique' => true,
],
[
'name' => 'mykey3_1',
'columns' => ['mykey3'],
'primary' => false,
'type' => null,
'unique' => false,
],
];

self::assertSame($expected, $indexes);

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

0 comments on commit 3cac0ec

Please sign in to comment.