-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5752b25
commit a26b212
Showing
18 changed files
with
2,823 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -15,6 +15,11 @@ | |
"directory": "packages/seal-opensearch-adapter", | ||
"target": "[email protected]:schranz-search/seal-opensearch-adapter.git" | ||
}, | ||
{ | ||
"name": "SEALSolrAdapter", | ||
"directory": "packages/seal-solr-adapter", | ||
"target": "[email protected]:schranz-search/seal-solr-adapter.git" | ||
}, | ||
{ | ||
"name": "SEALMeilisearchAdapter", | ||
"directory": "packages/seal-meilisearch-adapter", | ||
|
This file contains 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,4 @@ | ||
.gitattributes export-ignore | ||
.gitignore export-ignore | ||
composer.lock export-ignore | ||
/Tests export-ignore |
This file contains 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 @@ | ||
github: [alexander-schranz] |
This file contains 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,6 @@ | ||
/vendor/ | ||
/composer.phar | ||
/phpunit.xml | ||
/.phpunit.result.cache | ||
/Tests/var | ||
/docker-compose.override.yml |
This file contains 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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Alexander Schranz | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains 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,43 @@ | ||
<div align="center"> | ||
<img alt="Schranz Search Logo with a Seal on it with a magnifying glass" src="https://avatars.githubusercontent.com/u/120221538?s=400&v=5" width="200" height="200"> | ||
</div> | ||
|
||
<h1 align="center">Schranz Search SEAL <br /> Solr Adapter</h1> | ||
|
||
<br /> | ||
<br /> | ||
|
||
The `SolrAdapter` write the documents into a [Apache Solr](https://github.com/apache/solr) server instance. | ||
|
||
> **Note**: | ||
> This is part of the `schranz-search/schranz-search` project create issues in the [main repository](https://github.com/schranz-search/schranz-search). | ||
> **Warning**: | ||
> This project is heavily under development and not ready for production. | ||
## Installation | ||
|
||
Use [composer](https://getcomposer.org/) for install the package: | ||
|
||
```bash | ||
composer require schranz-search/seal schranz-search/seal-solr-adapter | ||
``` | ||
|
||
## Usage. | ||
|
||
The following code shows how to create an Engine using this Adapter: | ||
|
||
```php | ||
<?php | ||
|
||
use Solr\Client; | ||
use Schranz\Search\SEAL\Adapter\Solr\SolrAdapter; | ||
use Schranz\Search\SEAL\Engine; | ||
|
||
$client = new Client('http://127.0.0.1:7700'); | ||
|
||
$engine = new Engine( | ||
new SolrAdapter($client), | ||
$schema, | ||
); | ||
``` |
This file contains 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,34 @@ | ||
<?php | ||
|
||
namespace Schranz\Search\SEAL\Adapter\Solr; | ||
|
||
use Solarium\Client; | ||
use Schranz\Search\SEAL\Adapter\AdapterInterface; | ||
use Schranz\Search\SEAL\Adapter\ConnectionInterface; | ||
use Schranz\Search\SEAL\Adapter\SchemaManagerInterface; | ||
|
||
final class SolrAdapter implements AdapterInterface | ||
{ | ||
private readonly ConnectionInterface $connection; | ||
|
||
private readonly SchemaManagerInterface $schemaManager; | ||
|
||
public function __construct( | ||
private readonly Client $client, | ||
?ConnectionInterface $connection = null, | ||
?SchemaManagerInterface $schemaManager = null, | ||
) { | ||
$this->connection = $connection ?? new SolrConnection($client); | ||
$this->schemaManager = $schemaManager ?? new SolrSchemaManager($client); | ||
} | ||
|
||
public function getSchemaManager(): SchemaManagerInterface | ||
{ | ||
return $this->schemaManager; | ||
} | ||
|
||
public function getConnection(): ConnectionInterface | ||
{ | ||
return $this->connection; | ||
} | ||
} |
This file contains 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,157 @@ | ||
<?php | ||
|
||
namespace Schranz\Search\SEAL\Adapter\Solr; | ||
|
||
use Solarium\Client; | ||
use Schranz\Search\SEAL\Adapter\ConnectionInterface; | ||
use Schranz\Search\SEAL\Marshaller\Marshaller; | ||
use Schranz\Search\SEAL\Schema\Index; | ||
use Schranz\Search\SEAL\Search\Condition; | ||
use Schranz\Search\SEAL\Search\Result; | ||
use Schranz\Search\SEAL\Search\Search; | ||
use Schranz\Search\SEAL\Task\AsyncTask; | ||
use Schranz\Search\SEAL\Task\TaskInterface; | ||
|
||
final class SolrConnection implements ConnectionInterface | ||
{ | ||
private Marshaller $marshaller; | ||
|
||
public function __construct( | ||
private readonly Client $client, | ||
) { | ||
$this->marshaller = new Marshaller(); | ||
} | ||
|
||
public function save(Index $index, array $document, array $options = []): ?TaskInterface | ||
{ | ||
$identifierField = $index->getIdentifierField(); | ||
|
||
/** @var string|null $identifier */ | ||
$identifier = ((string) $document[$identifierField->name]) ?? null; | ||
|
||
$indexResponse = $this->client->index($index->name)->addDocuments([ | ||
$this->marshaller->marshall($index->fields, $document), | ||
], $identifierField->name); | ||
|
||
if ($indexResponse['status'] !== 'enqueued') { | ||
throw new \RuntimeException('Unexpected error while save document with identifier "' . $identifier . '" into Index "' . $index->name . '".'); | ||
} | ||
|
||
if (true !== ($options['return_slow_promise_result'] ?? false)) { | ||
return null; | ||
} | ||
|
||
return new AsyncTask(function() use ($indexResponse) { | ||
$this->client->waitForTask($indexResponse['taskUid']); | ||
}); | ||
} | ||
|
||
public function delete(Index $index, string $identifier, array $options = []): ?TaskInterface | ||
{ | ||
$deleteResponse = $this->client->index($index->name)->deleteDocument($identifier); | ||
|
||
if ($deleteResponse['status'] !== 'enqueued') { | ||
throw new \RuntimeException('Unexpected error while delete document with identifier "' . $identifier . '" from Index "' . $index->name . '".'); | ||
} | ||
|
||
if (true !== ($options['return_slow_promise_result'] ?? false)) { | ||
return null; | ||
} | ||
|
||
return new AsyncTask(function() use ($deleteResponse) { | ||
$this->client->waitForTask($deleteResponse['taskUid']); | ||
}); | ||
} | ||
|
||
public function search(Search $search): Result | ||
{ | ||
// optimized single document query | ||
if ( | ||
count($search->indexes) === 1 | ||
&& count($search->filters) === 1 | ||
&& $search->filters[0] instanceof Condition\IdentifierCondition | ||
&& $search->offset === 0 | ||
&& $search->limit === 1 | ||
) { | ||
try { | ||
$data = $this->client->index($search->indexes[\array_key_first($search->indexes)]->name)->getDocument($search->filters[0]->identifier); | ||
} catch (ApiException $e) { | ||
if ($e->httpStatus !== 404) { | ||
throw $e; | ||
} | ||
|
||
return new Result( | ||
$this->hitsToDocuments($search->indexes, []), | ||
0 | ||
); | ||
} | ||
|
||
return new Result( | ||
$this->hitsToDocuments($search->indexes, [$data]), | ||
1 | ||
); | ||
} | ||
|
||
if (count($search->indexes) !== 1) { | ||
throw new \RuntimeException('Solr does not yet support search multiple indexes: https://github.com/schranz-search/schranz-search/issues/28'); | ||
} | ||
|
||
$index = $search->indexes[\array_key_first($search->indexes)]; | ||
$searchIndex = $this->client->index($index->name); | ||
|
||
$query = null; | ||
$filters = []; | ||
foreach ($search->filters as $filter) { | ||
match (true) { | ||
$filter instanceof Condition\IdentifierCondition => $filters[] = $index->getIdentifierField()->name . ' = "' . $filter->identifier . '"', // TODO escape? | ||
$filter instanceof Condition\SearchCondition => $query = $filter->query, | ||
$filter instanceof Condition\EqualCondition => $filters[] = $filter->field . ' = ' . $filter->value, // TODO escape? | ||
$filter instanceof Condition\NotEqualCondition => $filters[] = $filter->field . ' != ' . $filter->value, // TODO escape? | ||
$filter instanceof Condition\GreaterThanCondition => $filters[] = $filter->field . ' > ' . $filter->value, // TODO escape? | ||
$filter instanceof Condition\GreaterThanEqualCondition => $filters[] = $filter->field . ' >= ' . $filter->value, // TODO escape? | ||
$filter instanceof Condition\LessThanCondition => $filters[] = $filter->field . ' < ' . $filter->value, // TODO escape? | ||
$filter instanceof Condition\LessThanEqualCondition => $filters[] = $filter->field . ' <= ' . $filter->value, // TODO escape? | ||
default => throw new \LogicException($filter::class . ' filter not implemented.'), | ||
}; | ||
} | ||
|
||
$searchParams = []; | ||
if (\count($filters) !== 0) { | ||
$searchParams = ['filter' => \implode(' AND ', $filters)]; | ||
} | ||
|
||
if ($search->offset) { | ||
$searchParams['offset'] = $search->offset; | ||
} | ||
|
||
if ($search->limit) { | ||
$searchParams['limit'] = $search->limit; | ||
} | ||
|
||
foreach ($search->sortBys as $field => $direction) { | ||
$searchParams['sort'][] = $field . ':' . $direction; | ||
} | ||
|
||
$data = $searchIndex->search($query, $searchParams)->toArray(); | ||
|
||
return new Result( | ||
$this->hitsToDocuments($search->indexes, $data['hits']), | ||
$data['totalHits'] ?? $data['estimatedTotalHits'] ?? null, | ||
); | ||
} | ||
|
||
/** | ||
* @param Index[] $indexes | ||
* @param iterable<array<string, mixed>> $hits | ||
* | ||
* @return \Generator<array<string, mixed>> | ||
*/ | ||
private function hitsToDocuments(array $indexes, iterable $hits): \Generator | ||
{ | ||
$index = $indexes[\array_key_first($indexes)]; | ||
|
||
foreach ($hits as $hit) { | ||
yield $this->marshaller->unmarshall($index->fields, $hit); | ||
} | ||
} | ||
} |
This file contains 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,81 @@ | ||
<?php | ||
|
||
namespace Schranz\Search\SEAL\Adapter\Solr; | ||
|
||
use Schranz\Search\SEAL\Task\SyncTask; | ||
use Solarium\Client; | ||
use Schranz\Search\SEAL\Adapter\SchemaManagerInterface; | ||
use Schranz\Search\SEAL\Schema\Index; | ||
use Schranz\Search\SEAL\Task\AsyncTask; | ||
use Schranz\Search\SEAL\Task\TaskInterface; | ||
use Solarium\Exception\HttpException; | ||
|
||
final class SolrSchemaManager implements SchemaManagerInterface | ||
{ | ||
public function __construct( | ||
private readonly Client $client, | ||
) { | ||
} | ||
|
||
public function existIndex(Index $index): bool | ||
{ | ||
$ping = $this->client->createPing([ | ||
'collection' => $index->name, | ||
'path' => '/', | ||
'core' => 'index', | ||
]); | ||
|
||
try { | ||
$result = $this->client->ping($ping); | ||
} catch (HttpException $e) { | ||
if ($e->getCode() !== 404) { | ||
throw $e; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function dropIndex(Index $index, array $options = []): ?TaskInterface | ||
{ | ||
$deleteIndexResponse = $this->client->deleteIndex($index->name); | ||
|
||
if (true !== ($options['return_slow_promise_result'] ?? false)) { | ||
return null; | ||
} | ||
|
||
return new AsyncTask(function() use ($deleteIndexResponse) { | ||
$this->client->waitForTask($deleteIndexResponse['taskUid']); | ||
}); | ||
} | ||
|
||
public function createIndex(Index $index, array $options = []): ?TaskInterface | ||
{ | ||
$query = $this->client->createCoreAdmin(); | ||
|
||
$createAction = $query->createCreate(); | ||
$createAction->setCore($index->name); | ||
$query->setAction($createAction); | ||
|
||
// TODO | ||
$attributes = [ | ||
'searchableAttributes' => $index->searchableFields, | ||
'filterableAttributes' => $index->filterableFields, | ||
'sortableAttributes' => $index->sortableFields, | ||
]; | ||
|
||
$response = $this->client->coreAdmin($query); | ||
$result = $response->getStatusResult(); | ||
|
||
var_dump($result); | ||
exit; | ||
|
||
if (true !== ($options['return_slow_promise_result'] ?? false)) { | ||
return null; | ||
} | ||
|
||
return new SyncTask(null); | ||
} | ||
} |
Oops, something went wrong.