-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add advanced type definition for Atlas Search Indexes #3234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+334
−11
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ parameters: | |
|
|
||
| paths: | ||
| - src | ||
| - tests/PHPStan | ||
|
|
||
| level: 2 | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,233 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace MongoDB\Laravel\Tests\PHPStan; | ||
|
|
||
| use MongoDB\Laravel\Schema\Blueprint; | ||
|
|
||
| /** | ||
| * PHPStan type-level tests for search index definitions. | ||
| * These functions are never executed at runtime — they exist to let PHPStan | ||
| * validate that complex index definitions match the declared @phpstan-type shapes. | ||
| * | ||
| * Examples are taken verbatim from the MongoDB Atlas documentation: | ||
| * | ||
| * @link https://www.mongodb.com/docs/atlas/atlas-search/define-field-mappings.md | ||
| * @link https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-type.md | ||
| * | ||
| * @phpstan-import-type TypeSearchIndexDefinition from Blueprint | ||
| * @phpstan-import-type TypeVectorSearchIndexDefinition from Blueprint | ||
| */ | ||
| final class SearchIndexTypes | ||
| { | ||
| /** @phpstan-param TypeSearchIndexDefinition $definition */ | ||
| public static function assertSearchIndexDefinition(array $definition): void | ||
| { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add a comment in the body clarifying that the |
||
| } | ||
|
|
||
| /** @phpstan-param TypeVectorSearchIndexDefinition $definition */ | ||
| public static function assertVectorSearchIndexDefinition(array $definition): void | ||
| { | ||
| } | ||
|
|
||
| public static function searchIndexExamples(): void | ||
| { | ||
| // Static mapping with nested document, multi-analyzer string, ignoreAbove | ||
| // Source: https://www.mongodb.com/docs/atlas/atlas-search/define-field-mappings.md | ||
| self::assertSearchIndexDefinition([ | ||
| 'analyzer' => 'lucene.standard', | ||
| 'searchAnalyzer' => 'lucene.standard', | ||
| 'mappings' => [ | ||
| 'dynamic' => false, | ||
| 'fields' => [ | ||
| 'awards' => [ | ||
| 'type' => 'document', | ||
| 'fields' => [ | ||
| 'wins' => ['type' => 'number'], | ||
| 'nominations' => ['type' => 'number', 'representation' => 'int64'], | ||
| 'text' => ['type' => 'string', 'analyzer' => 'lucene.english', 'ignoreAbove' => 255], | ||
| ], | ||
| ], | ||
| 'title' => [ | ||
| 'type' => 'string', | ||
| 'analyzer' => 'lucene.whitespace', | ||
| 'multi' => [ | ||
| 'mySecondaryAnalyzer' => ['type' => 'string', 'analyzer' => 'lucene.french'], | ||
| ], | ||
| ], | ||
| 'genres' => ['type' => 'string', 'analyzer' => 'lucene.standard'], | ||
| ], | ||
| ], | ||
| ]); | ||
|
|
||
| // Synonyms | ||
| // Source: https://www.mongodb.com/docs/atlas/atlas-search/synonyms.md | ||
| self::assertSearchIndexDefinition([ | ||
| 'mappings' => [ | ||
| 'dynamic' => false, | ||
| 'fields' => [ | ||
| 'plot' => ['type' => 'string', 'analyzer' => 'lucene.english'], | ||
| ], | ||
| ], | ||
| 'synonyms' => [ | ||
| [ | ||
| 'analyzer' => 'lucene.english', | ||
| 'name' => 'my_synonyms', | ||
| 'source' => ['collection' => 'synonymous_terms'], | ||
| ], | ||
| ], | ||
| ]); | ||
|
|
||
| // storedSource with include | ||
| // Source: https://www.mongodb.com/docs/atlas/atlas-search/stored-source-definition.md | ||
| self::assertSearchIndexDefinition([ | ||
| 'mappings' => ['dynamic' => true], | ||
| 'storedSource' => ['include' => ['title', 'awards.wins']], | ||
| ]); | ||
|
|
||
| // storedSource with exclude | ||
| // Source: https://www.mongodb.com/docs/atlas/atlas-search/stored-source-definition.md | ||
| self::assertSearchIndexDefinition([ | ||
| 'mappings' => ['dynamic' => true], | ||
| 'storedSource' => ['exclude' => ['directors', 'imdb.rating']], | ||
| ]); | ||
|
|
||
| // Dynamic typeSet-based mapping | ||
| // Source: https://www.mongodb.com/docs/atlas/atlas-search/define-field-mappings.md | ||
| self::assertSearchIndexDefinition([ | ||
| 'analyzer' => 'lucene.standard', | ||
| 'searchAnalyzer' => 'lucene.standard', | ||
| 'mappings' => [ | ||
| 'dynamic' => ['typeSet' => 'indexedTypes'], | ||
| 'fields' => [ | ||
| 'plot' => [], | ||
| ], | ||
| ], | ||
| 'typeSets' => [ | ||
| [ | ||
| 'name' => 'indexedTypes', | ||
| 'types' => [ | ||
| ['type' => 'token'], | ||
| ['type' => 'number'], | ||
| ], | ||
| ], | ||
| ], | ||
| ]); | ||
|
|
||
| // typeSets with autocomplete and multi-analyzer string | ||
| // Source: https://www.mongodb.com/docs/atlas/atlas-search/define-field-mappings.md | ||
| self::assertSearchIndexDefinition([ | ||
| 'analyzer' => 'lucene.standard', | ||
| 'searchAnalyzer' => 'lucene.standard', | ||
| 'mappings' => [ | ||
| 'dynamic' => false, | ||
| 'fields' => [ | ||
| 'awards' => ['type' => 'document', 'fields' => []], | ||
| ], | ||
| ], | ||
| 'typeSets' => [ | ||
| [ | ||
| 'name' => 'movieAwards', | ||
| 'types' => [ | ||
| [ | ||
| 'type' => 'string', | ||
| 'multi' => [ | ||
| 'english' => ['type' => 'string', 'analyzer' => 'lucene.english'], | ||
| 'french' => ['type' => 'string', 'analyzer' => 'lucene.french'], | ||
| ], | ||
| ], | ||
| ['type' => 'number'], | ||
| [ | ||
| 'type' => 'autocomplete', | ||
| 'analyzer' => 'lucene.standard', | ||
| 'tokenization' => 'edgeGram', | ||
| 'minGrams' => 3, | ||
| 'maxGrams' => 5, | ||
| 'foldDiacritics' => false, | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ]); | ||
|
|
||
| // String field with all options including similarity | ||
| // Source: https://www.mongodb.com/docs/atlas/atlas-search/field-types/string-type.md | ||
| self::assertSearchIndexDefinition([ | ||
| 'mappings' => [ | ||
| 'dynamic' => false, | ||
| 'fields' => [ | ||
| 'plot' => [ | ||
| 'type' => 'string', | ||
| 'analyzer' => 'lucene.english', | ||
| 'searchAnalyzer' => 'lucene.standard', | ||
| 'indexOptions' => 'offsets', | ||
| 'store' => true, | ||
| 'ignoreAbove' => 255, | ||
| 'norms' => 'omit', | ||
| 'similarity' => ['type' => 'bm25'], | ||
| ], | ||
| ], | ||
| ], | ||
| ]); | ||
|
|
||
| // Autocomplete field with all options including similarity | ||
| // Source: https://www.mongodb.com/docs/atlas/atlas-search/field-types/autocomplete-type.md | ||
| self::assertSearchIndexDefinition([ | ||
| 'mappings' => [ | ||
| 'dynamic' => false, | ||
| 'fields' => [ | ||
| 'title' => [ | ||
| 'type' => 'autocomplete', | ||
| 'analyzer' => 'lucene.standard', | ||
| 'tokenization' => 'edgeGram', | ||
| 'minGrams' => 2, | ||
| 'maxGrams' => 15, | ||
| 'foldDiacritics' => true, | ||
| 'similarity' => ['type' => 'stableTfl'], | ||
| ], | ||
| ], | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| public static function vectorSearchIndexExamples(): void | ||
| { | ||
| // Vector + quantization + HNSW + two filter fields | ||
| // Source: https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-type.md | ||
| self::assertVectorSearchIndexDefinition([ | ||
| 'fields' => [ | ||
| [ | ||
| 'type' => 'vector', | ||
| 'path' => 'plot_embedding_voyage_3_large', | ||
| 'numDimensions' => 2048, | ||
| 'similarity' => 'dotProduct', | ||
| 'quantization' => 'scalar', | ||
| 'indexingMethod' => 'hnsw', | ||
| ], | ||
| ['type' => 'filter', 'path' => 'genres'], | ||
| ['type' => 'filter', 'path' => 'year'], | ||
| ], | ||
| ]); | ||
|
|
||
| // Vector with hnswOptions (full syntax from docs) | ||
| // Source: https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-type.md | ||
| self::assertVectorSearchIndexDefinition([ | ||
| 'fields' => [ | ||
| [ | ||
| 'type' => 'vector', | ||
| 'path' => 'plot_embedding', | ||
| 'numDimensions' => 1536, | ||
| 'similarity' => 'cosine', | ||
| 'quantization' => 'none', | ||
| 'indexingMethod' => 'hnsw', | ||
| 'hnswOptions' => [ | ||
| 'maxEdges' => 32, | ||
| 'numEdgeCandidates' => 200, | ||
| ], | ||
| ], | ||
| ['type' => 'filter', 'path' => 'genres'], | ||
| ], | ||
| ]); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Claude, but this feels like kind of a given lol