Skip to content

Commit 7f8d729

Browse files
committed
Prepare Scout driver for release
1 parent 3295c47 commit 7f8d729

6 files changed

Lines changed: 124 additions & 96 deletions

File tree

composer.json

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,16 @@
2828
},
2929
"require": {
3030
"php": "^8.2",
31-
"directorytree/opensearch-adapter": "dev-master",
32-
"directorytree/opensearch-client": "dev-master",
31+
"directorytree/opensearch-adapter": "^1.0",
32+
"directorytree/opensearch-client": "^1.0",
3333
"laravel/scout": "^10.0|^11.0|^12.0|^13.0"
3434
},
3535
"require-dev": {
3636
"laravel/framework": "^11.0|^12.0|^13.0",
3737
"laravel/pint": "^1.0",
38-
"mockery/mockery": "^1.0",
3938
"orchestra/testbench": "^9.0|^10.0|^11.0",
4039
"pestphp/pest": "^3.0"
4140
},
42-
"repositories": [
43-
{
44-
"type": "path",
45-
"url": "../OpenSearchAdapter"
46-
},
47-
{
48-
"type": "path",
49-
"url": "../OpenSearchClient"
50-
}
51-
],
5241
"config": {
5342
"allow-plugins": {
5443
"php-http/discovery": true,

src/Engine.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace DirectoryTree\OpenSearchScoutDriver;
44

5-
use DirectoryTree\OpenSearchAdapter\Documents\DocumentManager;
5+
use DirectoryTree\OpenSearchAdapter\Documents\DocumentManagerInterface;
66
use DirectoryTree\OpenSearchAdapter\Indices\IndexBlueprint;
7-
use DirectoryTree\OpenSearchAdapter\Indices\IndexManager;
7+
use DirectoryTree\OpenSearchAdapter\Indices\IndexManagerInterface;
88
use DirectoryTree\OpenSearchAdapter\Search\Hit;
99
use DirectoryTree\OpenSearchAdapter\Search\SearchResponse;
1010
use DirectoryTree\OpenSearchScoutDriver\Factories\DocumentFactoryInterface;
@@ -28,15 +28,13 @@ class Engine extends ScoutEngine
2828
* Create a new OpenSearch Scout engine instance.
2929
*/
3030
public function __construct(
31-
protected DocumentManager $documentManager,
31+
protected ModelFactoryInterface $modelFactory,
32+
protected IndexManagerInterface $indexManager,
33+
protected DocumentManagerInterface $documentManager,
3234
protected DocumentFactoryInterface $documentFactory,
3335
protected SearchRequestFactoryInterface $searchRequestFactory,
34-
protected ModelFactoryInterface $modelFactory,
35-
protected IndexManager $indexManager,
3636
protected bool $refreshDocuments = false,
37-
) {
38-
$this->refreshDocuments = (bool) config('opensearch-scout.refresh_documents', $refreshDocuments);
39-
}
37+
) {}
4038

4139
/**
4240
* Update the given models in the index.
@@ -48,6 +46,7 @@ public function update($models): void
4846
}
4947

5048
$index = $models->first()->searchableAs();
49+
5150
$documents = $this->documentFactory->makeFromModels($models);
5251

5352
$this->documentManager->index($index, $documents->all(), $this->refreshDocuments);
@@ -63,6 +62,7 @@ public function delete($models): void
6362
}
6463

6564
$index = $models->first()->searchableAs();
65+
6666
$documentIds = $models->map(fn (Model $model) => (string) $model->getScoutKey())->all();
6767

6868
$this->documentManager->delete($index, $documentIds, $this->refreshDocuments);
@@ -129,6 +129,7 @@ public function getTotalCount($results): ?int
129129
public function flush($model): void
130130
{
131131
$index = $model->searchableAs();
132+
132133
$query = ['match_all' => new stdClass];
133134

134135
$this->documentManager->deleteByQuery($index, $query, $this->refreshDocuments);

src/OpenSearchScoutServiceProvider.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
namespace DirectoryTree\OpenSearchScoutDriver;
44

55
use DirectoryTree\OpenSearchAdapter\Documents\DocumentManager;
6+
use DirectoryTree\OpenSearchAdapter\Documents\DocumentManagerInterface;
67
use DirectoryTree\OpenSearchAdapter\Indices\IndexManager;
8+
use DirectoryTree\OpenSearchAdapter\Indices\IndexManagerInterface;
79
use DirectoryTree\OpenSearchClient\OpenSearchManager;
810
use DirectoryTree\OpenSearchScoutDriver\Factories\DocumentFactory;
911
use DirectoryTree\OpenSearchScoutDriver\Factories\DocumentFactoryInterface;
1012
use DirectoryTree\OpenSearchScoutDriver\Factories\ModelFactory;
1113
use DirectoryTree\OpenSearchScoutDriver\Factories\ModelFactoryInterface;
1214
use DirectoryTree\OpenSearchScoutDriver\Factories\SearchRequestFactory;
1315
use DirectoryTree\OpenSearchScoutDriver\Factories\SearchRequestFactoryInterface;
16+
use Illuminate\Contracts\Foundation\Application;
1417
use Illuminate\Support\ServiceProvider;
1518
use Laravel\Scout\EngineManager;
1619

@@ -26,17 +29,28 @@ public function register(): void
2629
{
2730
$this->mergeConfigFrom(__DIR__.'/../config/opensearch-scout.php', 'opensearch-scout');
2831

29-
$this->app->bindIf(ModelFactoryInterface::class, ModelFactory::class);
30-
$this->app->bindIf(DocumentFactoryInterface::class, DocumentFactory::class);
31-
$this->app->bindIf(SearchRequestFactoryInterface::class, SearchRequestFactory::class);
32+
$this->app->bind(ModelFactoryInterface::class, ModelFactory::class);
33+
$this->app->bind(DocumentFactoryInterface::class, DocumentFactory::class);
34+
$this->app->bind(SearchRequestFactoryInterface::class, SearchRequestFactory::class);
3235

33-
$this->app->singletonIf(DocumentManager::class, function ($app) {
36+
$this->app->singleton(DocumentManagerInterface::class, function (Application $app) {
3437
return new DocumentManager($app->make(OpenSearchManager::class)->default());
3538
});
3639

37-
$this->app->singletonIf(IndexManager::class, function ($app) {
40+
$this->app->singleton(IndexManagerInterface::class, function (Application $app) {
3841
return new IndexManager($app->make(OpenSearchManager::class)->default());
3942
});
43+
44+
$this->app->bind(Engine::class, function (Application $app) {
45+
return new Engine(
46+
$app->make(ModelFactoryInterface::class),
47+
$app->make(IndexManagerInterface::class),
48+
$app->make(DocumentManagerInterface::class),
49+
$app->make(DocumentFactoryInterface::class),
50+
$app->make(SearchRequestFactoryInterface::class),
51+
$app['config']->get('opensearch-scout.refresh_documents', false),
52+
);
53+
});
4054
}
4155

4256
/**
@@ -48,7 +62,7 @@ public function boot(): void
4862
__DIR__.'/../config/opensearch-scout.php' => config_path('opensearch-scout.php'),
4963
]);
5064

51-
$this->app->make(EngineManager::class)->extend('opensearch', function ($app) {
65+
$this->app->make(EngineManager::class)->extend('opensearch', function (Application $app) {
5266
return $app->make(Engine::class);
5367
});
5468
}

tests/Feature/OpenSearchScoutServiceProviderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

3-
use DirectoryTree\OpenSearchAdapter\Documents\DocumentManager;
4-
use DirectoryTree\OpenSearchAdapter\Indices\IndexManager;
3+
use DirectoryTree\OpenSearchAdapter\Documents\DocumentManagerInterface;
4+
use DirectoryTree\OpenSearchAdapter\Indices\IndexManagerInterface;
55
use DirectoryTree\OpenSearchScoutDriver\Engine;
66
use DirectoryTree\OpenSearchScoutDriver\Factories\DocumentFactoryInterface;
77
use DirectoryTree\OpenSearchScoutDriver\Factories\ModelFactoryInterface;
@@ -16,6 +16,6 @@
1616
expect(app(DocumentFactoryInterface::class))->toBeInstanceOf(DocumentFactoryInterface::class)
1717
->and(app(ModelFactoryInterface::class))->toBeInstanceOf(ModelFactoryInterface::class)
1818
->and(app(SearchRequestFactoryInterface::class))->toBeInstanceOf(SearchRequestFactoryInterface::class)
19-
->and(app(DocumentManager::class))->toBeInstanceOf(DocumentManager::class)
20-
->and(app(IndexManager::class))->toBeInstanceOf(IndexManager::class);
19+
->and(app(DocumentManagerInterface::class))->toBeInstanceOf(DocumentManagerInterface::class)
20+
->and(app(IndexManagerInterface::class))->toBeInstanceOf(IndexManagerInterface::class);
2121
});

tests/Integration/OpenSearchScoutDriverTest.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use DirectoryTree\OpenSearchAdapter\Indices\IndexManager;
3+
use DirectoryTree\OpenSearchAdapter\Indices\IndexManagerInterface;
44
use DirectoryTree\OpenSearchAdapter\Indices\Mapping;
55
use DirectoryTree\OpenSearchScoutDriver\Tests\Fixtures\Client;
66
use Illuminate\Database\Schema\Blueprint;
@@ -23,24 +23,27 @@
2323
['id' => 3, 'name' => 'Taylor Otwell', 'email' => 'taylor@example.com'],
2424
]);
2525

26-
$this->indexName = (new Client)->searchableAs();
27-
$this->indexManager = app(IndexManager::class);
26+
$index = (new Client)->searchableAs();
27+
$indexManager = app(IndexManagerInterface::class);
2828

29-
if ($this->indexManager->exists($this->indexName)) {
30-
$this->indexManager->delete($this->indexName);
29+
if ($indexManager->exists($index)) {
30+
$indexManager->delete($index);
3131
}
3232

33-
app(EngineManager::class)->engine('opensearch')->createIndex($this->indexName);
33+
app(EngineManager::class)->engine('opensearch')->createIndex($index);
3434

35-
$this->indexManager->putMapping($this->indexName, (new Mapping)
35+
$indexManager->putMapping($index, (new Mapping)
3636
->text('name', ['fielddata' => true])
3737
->keyword('email')
3838
->integer('__soft_deleted'));
3939
});
4040

4141
afterEach(function (): void {
42-
if (isset($this->indexManager, $this->indexName) && $this->indexManager->exists($this->indexName)) {
43-
$this->indexManager->delete($this->indexName);
42+
$index = (new Client)->searchableAs();
43+
$indexManager = app(IndexManagerInterface::class);
44+
45+
if ($indexManager->exists($index)) {
46+
$indexManager->delete($index);
4447
}
4548

4649
Schema::dropIfExists('clients');

0 commit comments

Comments
 (0)