Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
ignoreInternalFunctionFalseReturn="true"
>
<projectFiles>
<directory name="src" />
Expand Down
16 changes: 16 additions & 0 deletions src/Elasticsearch/Adapter/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,22 @@ public function updateByQuery(string $index, array $query, array $options = []):
*/
public function search(string $index, array $query): SearchResults;

/**
* Perform a search query on an index
*
* @param string $index Index to search on
* @param array<array> $queries Queries to perform. Each items use same format as $query parameter of {@see ClientInterface::search()}.
*
* @return list<SearchResults>
*
* @throws NotFoundException When the index does not exist
* @throws InternalServerException When http 500 error occurs
* @throws InvalidRequestException When request is malformed
* @throws NoNodeAvailableException If elasticsearch server is down
* @throws ElasticsearchExceptionInterface When requested cannot be performed
*/
public function multiSearch(string $index, array $queries): array;

/**
* Perform a search query on an index and return the matching documents count
*
Expand Down
52 changes: 52 additions & 0 deletions src/Elasticsearch/Adapter/ES7Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,58 @@ public function search(string $index, array $query): SearchResults
);
}

/**
* {@inheritdoc}
*/
public function multiSearch(string $index, array $queries): array
{
$body = [];

foreach ($queries as $query) {
$body[] = [];
$body[] = $query;
}

try {
$response = $this->client->msearch(['index' => $index, 'body' => $body]);
} catch (ElasticsearchException $e) {
$this->handleException($e);
}

$results = [];

foreach ($response['responses'] as $key => $result) {
if (isset($result['error'])) {
$status = (int) $result['status'];

switch (intdiv($status, 100)) {
case 4:
throw $status === 404 ? new NotFoundException($result['error']['reason']) : new InvalidRequestException($result['error']['reason']);

case 5:
throw new InternalServerException($result['error']['reason']);

default:
throw new RuntimeException($result['error']['reason']);
}
}

$results[$key] = new SearchResults(
$result['_scroll_id'] ?? null,
$result['took'],
$result['timed_out'],
$result['_shards'],
$result['hits']['total']['value'],
$result['hits']['total']['relation'] === 'eq',
$result['hits']['max_score'] ?? null,
$result['hits']['hits'],
$result
);
}

return $results;
}

/**
* {@inheritdoc}
*/
Expand Down
54 changes: 54 additions & 0 deletions src/Elasticsearch/Adapter/ES8Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Elastic\Elasticsearch\Exception\ServerResponseException;
use Elastic\Transport\Exception\NoNodeAvailableException as DriverNoNodeAvailableException;

use function intdiv;

/**
* Client adapter for PHP elasticsearch client v8
*/
Expand Down Expand Up @@ -241,6 +243,58 @@ public function search(string $index, array $query): SearchResults
);
}

/**
* {@inheritdoc}
*/
public function multiSearch(string $index, array $queries): array
{
$body = [];

foreach ($queries as $query) {
$body[] = [];
$body[] = $query;
}

try {
$response = $this->client->msearch(['index' => $index, 'body' => $body])->asArray();
} catch (ElasticsearchException $e) {
$this->handleException($e);
}

$results = [];

foreach ($response['responses'] as $key => $result) {
if (isset($result['error'])) {
$status = (int) $result['status'];

switch (intdiv($status, 100)) {
case 4:
throw $status === 404 ? new NotFoundException($result['error']['reason']) : new InvalidRequestException($result['error']['reason']);

case 5:
throw new InternalServerException($result['error']['reason']);

default:
throw new RuntimeException($result['error']['reason']);
}
}

$results[$key] = new SearchResults(
$result['_scroll_id'] ?? null,
$result['took'],
$result['timed_out'],
$result['_shards'],
$result['hits']['total']['value'],
$result['hits']['total']['relation'] === 'eq',
$result['hits']['max_score'] ?? null,
$result['hits']['hits'],
$result
);
}

return $results;
}

/**
* {@inheritdoc}
*/
Expand Down
8 changes: 5 additions & 3 deletions src/Elasticsearch/Adapter/Response/SearchResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class SearchResults implements ArrayAccess
* @param int $total
* @param bool $isAccurateCount
* @param float|null $maxScore
* @param array $hits
* @param list<array> $hits
* @param array $raw
*/
public function __construct(?string $scrollId, int $took, bool $timedOut, array $shards, int $total, bool $isAccurateCount, ?float $maxScore, array $hits, array $raw)
Expand Down Expand Up @@ -130,13 +130,15 @@ public function maxScore(): ?float
/**
* Array of returned document objects
*
* @return array{
* @return list<array{
* _index: string,
* _id: string,
* _score: float,
* _source: array,
* fields: array
* }
* }>
* @psalm-suppress MoreSpecificReturnType
* @psalm-suppress LessSpecificReturnStatement
*/
public function hits(): array
{
Expand Down
11 changes: 11 additions & 0 deletions src/Elasticsearch/ElasticsearchIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Bdf\Prime\Indexer\Elasticsearch\Query\Bulk\ElasticsearchBulkQuery;
use Bdf\Prime\Indexer\Elasticsearch\Mapper\Property\PropertyInterface;
use Bdf\Prime\Indexer\Elasticsearch\Query\ElasticsearchCreateQuery;
use Bdf\Prime\Indexer\Elasticsearch\Query\ElasticsearchMultiSearchQuery;
use Bdf\Prime\Indexer\Elasticsearch\Query\ElasticsearchQuery;
use Bdf\Prime\Indexer\Elasticsearch\Query\ElasticsearchUpdateQuery;
use Bdf\Prime\Indexer\Elasticsearch\Query\Result\BulkResultSet;
Expand Down Expand Up @@ -251,6 +252,16 @@ public function bulk(): ElasticsearchBulkQuery
;
}

/**
* Get a query object for performing multi search
*
* @return ElasticsearchMultiSearchQuery
*/
public function multi(): ElasticsearchMultiSearchQuery
{
return new ElasticsearchMultiSearchQuery($this->client, $this->mapper->configuration()->index(), $this->mapper);
}

/**
* Refresh the current index
* Make all operations performed since the last refresh available for search
Expand Down
Loading