|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Laravel\Tests; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\Schema; |
| 6 | +use MongoDB\Collection as MongoDBCollection; |
| 7 | +use MongoDB\Driver\Exception\ServerException; |
| 8 | +use MongoDB\Laravel\Schema\Builder; |
| 9 | +use MongoDB\Laravel\Tests\Models\Book; |
| 10 | + |
| 11 | +use function assert; |
| 12 | +use function usleep; |
| 13 | +use function usort; |
| 14 | + |
| 15 | +class AtlasSearchTest extends TestCase |
| 16 | +{ |
| 17 | + public function setUp(): void |
| 18 | + { |
| 19 | + parent::setUp(); |
| 20 | + |
| 21 | + Book::insert([ |
| 22 | + ['title' => 'Introduction to Algorithms'], |
| 23 | + ['title' => 'Clean Code: A Handbook of Agile Software Craftsmanship'], |
| 24 | + ['title' => 'Design Patterns: Elements of Reusable Object-Oriented Software'], |
| 25 | + ['title' => 'The Pragmatic Programmer: Your Journey to Mastery'], |
| 26 | + ['title' => 'Artificial Intelligence: A Modern Approach'], |
| 27 | + ['title' => 'Structure and Interpretation of Computer Programs'], |
| 28 | + ['title' => 'Code Complete: A Practical Handbook of Software Construction'], |
| 29 | + ['title' => 'The Art of Computer Programming'], |
| 30 | + ['title' => 'Computer Networks'], |
| 31 | + ['title' => 'Operating System Concepts'], |
| 32 | + ['title' => 'Database System Concepts'], |
| 33 | + ['title' => 'Compilers: Principles, Techniques, and Tools'], |
| 34 | + ['title' => 'Introduction to the Theory of Computation'], |
| 35 | + ['title' => 'Modern Operating Systems'], |
| 36 | + ['title' => 'Computer Organization and Design'], |
| 37 | + ['title' => 'The Mythical Man-Month: Essays on Software Engineering'], |
| 38 | + ['title' => 'Algorithms'], |
| 39 | + ['title' => 'Understanding Machine Learning: From Theory to Algorithms'], |
| 40 | + ['title' => 'Deep Learning'], |
| 41 | + ['title' => 'Pattern Recognition and Machine Learning'], |
| 42 | + ]); |
| 43 | + |
| 44 | + $collection = $this->getConnection('mongodb')->getCollection('books'); |
| 45 | + assert($collection instanceof MongoDBCollection); |
| 46 | + try { |
| 47 | + $collection->createSearchIndex([ |
| 48 | + 'mappings' => [ |
| 49 | + 'fields' => [ |
| 50 | + 'title' => [ |
| 51 | + ['type' => 'string', 'analyzer' => 'lucene.english'], |
| 52 | + ['type' => 'autocomplete', 'analyzer' => 'lucene.english'], |
| 53 | + ], |
| 54 | + ], |
| 55 | + ], |
| 56 | + ]); |
| 57 | + |
| 58 | + $collection->createSearchIndex([ |
| 59 | + 'mappings' => ['dynamic' => true], |
| 60 | + ], ['name' => 'dynamic_search']); |
| 61 | + |
| 62 | + $collection->createSearchIndex([ |
| 63 | + 'fields' => [ |
| 64 | + ['type' => 'vector', 'numDimensions' => 16, 'path' => 'vector16', 'similarity' => 'cosine'], |
| 65 | + ['type' => 'vector', 'numDimensions' => 32, 'path' => 'vector32', 'similarity' => 'euclidean'], |
| 66 | + ], |
| 67 | + ], ['name' => 'vector', 'type' => 'vectorSearch']); |
| 68 | + } catch (ServerException $e) { |
| 69 | + if (Builder::isAtlasSearchNotSupportedException($e)) { |
| 70 | + self::markTestSkipped('Atlas Search not supported. ' . $e->getMessage()); |
| 71 | + } |
| 72 | + |
| 73 | + throw $e; |
| 74 | + } |
| 75 | + |
| 76 | + // Wait for the index to be ready |
| 77 | + do { |
| 78 | + $ready = true; |
| 79 | + usleep(10_000); |
| 80 | + foreach ($collection->listSearchIndexes() as $index) { |
| 81 | + if ($index['status'] !== 'READY') { |
| 82 | + $ready = false; |
| 83 | + } |
| 84 | + } |
| 85 | + } while (! $ready); |
| 86 | + } |
| 87 | + |
| 88 | + public function tearDown(): void |
| 89 | + { |
| 90 | + $this->getConnection('mongodb')->getCollection('books')->drop(); |
| 91 | + |
| 92 | + parent::tearDown(); |
| 93 | + } |
| 94 | + |
| 95 | + public function testGetIndexes() |
| 96 | + { |
| 97 | + $indexes = Schema::getIndexes('books'); |
| 98 | + |
| 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']); |
| 104 | + |
| 105 | + $expected = [ |
| 106 | + [ |
| 107 | + 'name' => '_id_', |
| 108 | + 'columns' => ['_id'], |
| 109 | + 'primary' => true, |
| 110 | + 'type' => null, |
| 111 | + 'unique' => false, |
| 112 | + ], |
| 113 | + [ |
| 114 | + 'name' => 'default', |
| 115 | + 'columns' => ['title'], |
| 116 | + 'type' => 'search', |
| 117 | + 'primary' => false, |
| 118 | + 'unique' => false, |
| 119 | + ], |
| 120 | + [ |
| 121 | + 'name' => 'dynamic_search', |
| 122 | + 'columns' => ['dynamic'], |
| 123 | + 'type' => 'search', |
| 124 | + 'primary' => false, |
| 125 | + 'unique' => false, |
| 126 | + ], |
| 127 | + [ |
| 128 | + 'name' => 'vector', |
| 129 | + 'columns' => ['vector16', 'vector32'], |
| 130 | + 'type' => 'vectorSearch', |
| 131 | + 'primary' => false, |
| 132 | + 'unique' => false, |
| 133 | + ], |
| 134 | + ]; |
| 135 | + |
| 136 | + self::assertSame($expected, $indexes); |
| 137 | + } |
| 138 | +} |
0 commit comments