|
10 | 10 | use Vladvildanov\PredisVl\Enum\StorageType; |
11 | 11 | use Vladvildanov\PredisVl\Factory; |
12 | 12 | use Vladvildanov\PredisVl\FactoryInterface; |
| 13 | +use Vladvildanov\PredisVl\Query\QueryInterface; |
13 | 14 |
|
14 | 15 | class SearchIndex implements IndexInterface |
15 | 16 | { |
@@ -94,6 +95,10 @@ public function create(bool $isOverwrite = false): bool |
94 | 95 | */ |
95 | 96 | public function load(string $key, mixed $values): bool |
96 | 97 | { |
| 98 | + $key = (array_key_exists('prefix', $this->schema['index'])) |
| 99 | + ? $this->schema['index']['prefix'] . $key |
| 100 | + : $key; |
| 101 | + |
97 | 102 | if (is_string($values)) { |
98 | 103 | $response = $this->client->jsonset($key, '$', $values); |
99 | 104 | } elseif (is_array($values)) { |
@@ -122,6 +127,44 @@ public function fetch(string $id): mixed |
122 | 127 | return $this->client->hgetall($key); |
123 | 128 | } |
124 | 129 |
|
| 130 | + /** |
| 131 | + * @inheritDoc |
| 132 | + */ |
| 133 | + public function query(QueryInterface $query) |
| 134 | + { |
| 135 | + $response = $this->client->ftsearch( |
| 136 | + $this->schema['index']['name'], |
| 137 | + $query->getQueryString(), |
| 138 | + $query->getSearchArguments() |
| 139 | + ); |
| 140 | + |
| 141 | + $processedResponse = ['count' => $response[0]]; |
| 142 | + $withScores = in_array('WITHSCORES', $query->getSearchArguments()->toArray(), true); |
| 143 | + |
| 144 | + if (count($response) > 1) { |
| 145 | + for ($i = 1, $iMax = count($response); $i < $iMax; $i++) { |
| 146 | + $processedResponse['results'][$response[$i]] = []; |
| 147 | + |
| 148 | + // Different return type depends on WITHSCORE condition |
| 149 | + if ($withScores) { |
| 150 | + $processedResponse['results'][$response[$i]]['score'] = $response[$i + 1]; |
| 151 | + $step = 2; |
| 152 | + } else { |
| 153 | + $step = 1; |
| 154 | + } |
| 155 | + |
| 156 | + for ($j = 0, $jMax = count($response[$i + $step]); $j < $jMax; $j++) { |
| 157 | + $processedResponse['results'][$response[$i]][$response[$i + $step][$j]] = $response[$i + $step][$j + 1]; |
| 158 | + ++$j; |
| 159 | + } |
| 160 | + |
| 161 | + $i += $step; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return $processedResponse; |
| 166 | + } |
| 167 | + |
125 | 168 | /** |
126 | 169 | * Validates schema array. |
127 | 170 | * |
|
0 commit comments