|
2 | 2 |
|
3 | 3 | use DirectoryTree\OpenSearchAdapter\Documents\Document; |
4 | 4 | use DirectoryTree\OpenSearchAdapter\Documents\DocumentManager; |
| 5 | +use DirectoryTree\OpenSearchAdapter\Documents\Routing; |
5 | 6 | use DirectoryTree\OpenSearchAdapter\Indices\Alias; |
6 | 7 | use DirectoryTree\OpenSearchAdapter\Indices\IndexBlueprint; |
7 | 8 | use DirectoryTree\OpenSearchAdapter\Indices\IndexManager; |
| 9 | +use DirectoryTree\OpenSearchAdapter\Indices\Mapping; |
| 10 | +use DirectoryTree\OpenSearchAdapter\Indices\Settings; |
8 | 11 | use DirectoryTree\OpenSearchAdapter\Search\SearchRequest; |
9 | 12 | use OpenSearch\Client; |
10 | 13 | use OpenSearch\GuzzleClientFactory; |
|
64 | 67 | } |
65 | 68 | }); |
66 | 69 |
|
| 70 | +it('handles mappings settings routing delete by query and rich search responses against opensearch', function (): void { |
| 71 | + $client = openSearchClient(); |
| 72 | + $indexManager = new IndexManager($client); |
| 73 | + $documentManager = new DocumentManager($client); |
| 74 | + |
| 75 | + $indexName = sprintf('adapter_integration_%s', bin2hex(random_bytes(4))); |
| 76 | + |
| 77 | + $mapping = (new Mapping) |
| 78 | + ->text('title') |
| 79 | + ->keyword('status') |
| 80 | + ->keyword('tenant') |
| 81 | + ->integer('views'); |
| 82 | + |
| 83 | + $settings = (new Settings)->index([ |
| 84 | + 'number_of_shards' => 1, |
| 85 | + 'number_of_replicas' => 0, |
| 86 | + ]); |
| 87 | + |
| 88 | + try { |
| 89 | + $indexManager->create(new IndexBlueprint($indexName, $mapping, $settings)); |
| 90 | + |
| 91 | + $indexManager->putMappingRaw($indexName, [ |
| 92 | + 'properties' => [ |
| 93 | + 'category' => ['type' => 'keyword'], |
| 94 | + ], |
| 95 | + ]); |
| 96 | + |
| 97 | + $documentManager->index($indexName, [ |
| 98 | + new Document('1', [ |
| 99 | + 'title' => 'Laravel OpenSearch adapter', |
| 100 | + 'status' => 'published', |
| 101 | + 'tenant' => 'alpha', |
| 102 | + 'views' => 10, |
| 103 | + 'category' => 'packages', |
| 104 | + ]), |
| 105 | + new Document('2', [ |
| 106 | + 'title' => 'OpenSearch draft notes', |
| 107 | + 'status' => 'draft', |
| 108 | + 'tenant' => 'alpha', |
| 109 | + 'views' => 3, |
| 110 | + 'category' => 'notes', |
| 111 | + ]), |
| 112 | + new Document('3', [ |
| 113 | + 'title' => 'OpenSearch routed document', |
| 114 | + 'status' => 'published', |
| 115 | + 'tenant' => 'beta', |
| 116 | + 'views' => 7, |
| 117 | + 'category' => 'packages', |
| 118 | + ]), |
| 119 | + ], refresh: true, routing: (new Routing)->add('3', 'tenant-beta')); |
| 120 | + |
| 121 | + $response = $documentManager->search($indexName, (new SearchRequest([ |
| 122 | + 'match' => [ |
| 123 | + 'title' => 'OpenSearch', |
| 124 | + ], |
| 125 | + ])) |
| 126 | + ->highlight([ |
| 127 | + 'fields' => [ |
| 128 | + 'title' => new stdClass, |
| 129 | + ], |
| 130 | + ]) |
| 131 | + ->aggregations([ |
| 132 | + 'by_status' => [ |
| 133 | + 'terms' => [ |
| 134 | + 'field' => 'status', |
| 135 | + ], |
| 136 | + ], |
| 137 | + ]) |
| 138 | + ->sort([ |
| 139 | + ['views' => 'desc'], |
| 140 | + ]) |
| 141 | + ->source(['title', 'status', 'views']) |
| 142 | + ->trackTotalHits(true)); |
| 143 | + |
| 144 | + expect($response->total())->toBe(3) |
| 145 | + ->and($response->hits()[0]->document()->id())->toBe('1') |
| 146 | + ->and($response->hits()[0]->highlight()?->snippets('title'))->not->toBeEmpty() |
| 147 | + ->and($response->aggregations()['by_status']->buckets()[0]->key())->toBe('published'); |
| 148 | + |
| 149 | + $documentManager->deleteByQuery($indexName, [ |
| 150 | + 'term' => [ |
| 151 | + 'status' => 'draft', |
| 152 | + ], |
| 153 | + ], refresh: true); |
| 154 | + |
| 155 | + expect($documentManager->search($indexName, new SearchRequest([ |
| 156 | + 'term' => [ |
| 157 | + 'status' => 'draft', |
| 158 | + ], |
| 159 | + ]))->total())->toBe(0); |
| 160 | + |
| 161 | + $documentManager->delete($indexName, ['3'], refresh: true, routing: (new Routing)->add('3', 'tenant-beta')); |
| 162 | + |
| 163 | + expect($documentManager->search($indexName, new SearchRequest([ |
| 164 | + 'term' => [ |
| 165 | + 'tenant' => 'beta', |
| 166 | + ], |
| 167 | + ]))->total())->toBe(0); |
| 168 | + } finally { |
| 169 | + if ($indexManager->exists($indexName)) { |
| 170 | + $indexManager->drop($indexName); |
| 171 | + } |
| 172 | + } |
| 173 | +}); |
| 174 | + |
67 | 175 | /** |
68 | 176 | * Create an OpenSearch client for integration tests. |
69 | 177 | */ |
|
0 commit comments