-
-
Notifications
You must be signed in to change notification settings - Fork 21
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
d84b153
commit 798f797
Showing
18 changed files
with
2,521 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 |
---|---|---|
|
@@ -20,6 +20,11 @@ | |
"directory": "packages/seal-solr-adapter", | ||
"target": "[email protected]:schranz-search/seal-solr-adapter.git" | ||
}, | ||
{ | ||
"name": "SEALRediSearchAdapter", | ||
"directory": "packages/seal-redisearch-adapter", | ||
"target": "[email protected]:schranz-search/seal-redisearch-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,49 @@ | ||
<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 /> RediSearch Adapter</h1> | ||
|
||
<br /> | ||
<br /> | ||
|
||
The `RediSearchAdapter` write the documents into a [RediSearch](https://redis.io/docs/stack/search/) server instance. The Redis Server requires to run with the RedisSearch and JSON module. | ||
|
||
> **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-redisearch-adapter | ||
``` | ||
|
||
## Usage. | ||
|
||
The following code shows how to create an Engine using this Adapter: | ||
|
||
```php | ||
<?php | ||
|
||
use Redis; | ||
use Schranz\Search\SEAL\Adapter\RediSearch\RediSearchAdapter; | ||
use Schranz\Search\SEAL\Engine; | ||
|
||
$redis = new Redis([ | ||
'host' => '127.0.0.1', | ||
'port' => 6379, | ||
'auth' => ['phpredis', 'phpredis'], | ||
]); | ||
|
||
$engine = new Engine( | ||
new RediSearchAdapter($redis), | ||
$schema, | ||
); | ||
``` | ||
|
||
The `ext-redis` and `ext-json` PHP extension is required for this 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,34 @@ | ||
<?php | ||
|
||
namespace Schranz\Search\SEAL\Adapter\RediSearch; | ||
|
||
use Redis; | ||
use Schranz\Search\SEAL\Adapter\AdapterInterface; | ||
use Schranz\Search\SEAL\Adapter\ConnectionInterface; | ||
use Schranz\Search\SEAL\Adapter\SchemaManagerInterface; | ||
|
||
final class RediSearchAdapter implements AdapterInterface | ||
{ | ||
private readonly ConnectionInterface $connection; | ||
|
||
private readonly SchemaManagerInterface $schemaManager; | ||
|
||
public function __construct( | ||
private readonly Redis $client, | ||
?ConnectionInterface $connection = null, | ||
?SchemaManagerInterface $schemaManager = null, | ||
) { | ||
$this->connection = $connection ?? new RediSearchConnection($client); | ||
$this->schemaManager = $schemaManager ?? new RediSearchSchemaManager($client); | ||
} | ||
|
||
public function getSchemaManager(): SchemaManagerInterface | ||
{ | ||
return $this->schemaManager; | ||
} | ||
|
||
public function getConnection(): ConnectionInterface | ||
{ | ||
return $this->connection; | ||
} | ||
} |
217 changes: 217 additions & 0 deletions
217
packages/seal-redisearch-adapter/RediSearchConnection.php
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,217 @@ | ||
<?php | ||
|
||
namespace Schranz\Search\SEAL\Adapter\RediSearch; | ||
|
||
use Redis; | ||
use Schranz\Search\SEAL\Marshaller\Marshaller; | ||
use Schranz\Search\SEAL\Schema\Exception\FieldByPathNotFoundException; | ||
use Schranz\Search\SEAL\Task\SyncTask; | ||
use Schranz\Search\SEAL\Adapter\ConnectionInterface; | ||
use Schranz\Search\SEAL\Schema\Field; | ||
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\TaskInterface; | ||
|
||
final class RediSearchConnection implements ConnectionInterface | ||
{ | ||
private Marshaller $marshaller; | ||
|
||
public function __construct( | ||
private readonly Redis $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; | ||
|
||
$marshalledDocument = $this->marshaller->marshall($index->fields, $document); | ||
|
||
$jsonSet = $this->client->rawCommand( | ||
'JSON.SET', | ||
$index->name . ':' . $identifier, | ||
'$', | ||
json_encode($marshalledDocument), | ||
); | ||
|
||
if ($jsonSet === false) { | ||
throw $this->createRedisLastErrorException(); | ||
} | ||
|
||
if (true !== ($options['return_slow_promise_result'] ?? false)) { | ||
return null; | ||
} | ||
|
||
return new SyncTask(null); | ||
} | ||
|
||
public function delete(Index $index, string $identifier, array $options = []): ?TaskInterface | ||
{ | ||
$jsonDel = $this->client->rawCommand( | ||
'JSON.DEL', | ||
$index->name . ':' . $identifier, | ||
); | ||
|
||
if ($jsonDel === false) { | ||
throw $this->createRedisLastErrorException(); | ||
} | ||
|
||
if (true !== ($options['return_slow_promise_result'] ?? false)) { | ||
return null; | ||
} | ||
|
||
return new SyncTask(null); | ||
} | ||
|
||
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 | ||
) { | ||
$jsonGet = $this->client->rawCommand( | ||
'JSON.GET', | ||
$search->indexes[\array_key_first($search->indexes)]->name . ':' . $search->filters[0]->identifier, | ||
); | ||
|
||
if ($jsonGet === false) { | ||
return new Result( | ||
$this->hitsToDocuments($search->indexes, []), | ||
0 | ||
); | ||
} | ||
|
||
return new Result( | ||
$this->hitsToDocuments($search->indexes, [\json_decode($jsonGet, true)]), | ||
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/86'); | ||
} | ||
|
||
$index = $search->indexes[\array_key_first($search->indexes)]; | ||
|
||
$queryText = ''; | ||
|
||
$filters = []; | ||
foreach ($search->filters as $filter) { | ||
match (true) { | ||
$filter instanceof Condition\SearchCondition => $filters[] = $this->escape($filter->query), | ||
$filter instanceof Condition\IdentifierCondition => $filters[] = '@' . $index->getIdentifierField()->name . ':(' . $this->escape($filter->identifier) . ')', | ||
$filter instanceof Condition\EqualCondition => $filters[] = '@' . $this->getFilterField($search->indexes, $filter->field) . ':(' . $this->escape($filter->value) . ')', | ||
$filter instanceof Condition\NotEqualCondition => $filters[] = '-@' . $this->getFilterField($search->indexes, $filter->field) . ':(' . $this->escape($filter->value) . ')', | ||
$filter instanceof Condition\GreaterThanCondition => $filters[] = '@' . $this->getFilterField($search->indexes, $filter->field) . ':[(' . $this->escape($filter->value, true) . ' inf]', | ||
$filter instanceof Condition\GreaterThanEqualCondition => $filters[] = '@' . $this->getFilterField($search->indexes, $filter->field) . ':[' . $this->escape($filter->value, true) . ' inf]', | ||
$filter instanceof Condition\LessThanCondition => $filters[] = '@' . $this->getFilterField($search->indexes, $filter->field) . ':[-inf (' . $this->escape($filter->value, true) . ']', | ||
$filter instanceof Condition\LessThanEqualCondition => $filters[] = '@' . $this->getFilterField($search->indexes, $filter->field) . ':[-inf ' . $this->escape($filter->value, true) . ']', | ||
default => throw new \LogicException($filter::class . ' filter not implemented.'), | ||
}; | ||
} | ||
|
||
$query = '*'; | ||
if (count($filters) > 0) { | ||
$query =implode(' ', $filters); | ||
} | ||
|
||
$query = $query; | ||
|
||
$arguments = []; | ||
foreach ($search->sortBys as $field => $direction) { | ||
$arguments[] = 'SORTBY'; | ||
$arguments[] = $this->escape($field); | ||
$arguments[] = strtoupper($this->escape($direction)); | ||
} | ||
|
||
if ($search->offset || $search->limit) { | ||
$arguments[] = 'LIMIT'; | ||
$arguments[] = $search->offset; | ||
$arguments[] = ($search->limit ?: 10); | ||
} | ||
|
||
$arguments[] = 'DIALECT'; | ||
$arguments[] = '3'; | ||
|
||
$result = $this->client->rawCommand( | ||
'FT.SEARCH', | ||
$index->name, | ||
$query, | ||
...$arguments | ||
); | ||
|
||
if ($result === false) { | ||
throw $this->createRedisLastErrorException(); | ||
} | ||
|
||
$total = $result[0]; | ||
|
||
$documents = []; | ||
foreach ($result as $item) { | ||
if (!is_array($item)) { | ||
continue; | ||
} | ||
|
||
$previousValue = null; | ||
foreach ($item as $value) { | ||
if ($previousValue === '$') { | ||
$documents[] = json_decode($value, true)[0]; | ||
} | ||
|
||
$previousValue = $value; | ||
} | ||
} | ||
|
||
return new Result( | ||
$this->hitsToDocuments($search->indexes, $documents), | ||
$total | ||
); | ||
} | ||
|
||
/** | ||
* @param Index[] $indexes | ||
* @param iterable<\Solarium\QueryType\Select\Result\Document> $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); | ||
} | ||
} | ||
|
||
private function getFilterField(array $indexes, string $name): string | ||
{ | ||
return str_replace('.', '__', $name); | ||
} | ||
|
||
private function createRedisLastErrorException(): \RuntimeException | ||
{ | ||
$lastError = $this->client->getLastError(); | ||
$this->client->clearLastError(); | ||
|
||
return new \RuntimeException('Redis: ' . $lastError); | ||
} | ||
|
||
private function escape(string|int|float $text, bool $asNumber = false): string | ||
{ | ||
if ($asNumber) { | ||
return (string) ((float) $text); | ||
} | ||
|
||
return addcslashes($text, ',.<>{}[]"\':;!@#$%^&*()-+=~'); | ||
} | ||
} |
Oops, something went wrong.