diff --git a/src/Driver/OpenSearch/OpenSearch.php b/src/Driver/OpenSearch/OpenSearch.php index d99a64a..4ca5672 100644 --- a/src/Driver/OpenSearch/OpenSearch.php +++ b/src/Driver/OpenSearch/OpenSearch.php @@ -11,6 +11,7 @@ use Aternos\Model\Driver\OpenSearch\Exception\OpenSearchException; use Aternos\Model\Driver\OpenSearch\Exception\SerializeException; use Aternos\Model\ModelInterface; +use Aternos\Model\Search\CountRelation; use Aternos\Model\Search\Search; use Aternos\Model\Search\SearchResult; use Psr\Http\Client\ClientInterface; @@ -192,6 +193,18 @@ public function delete(ModelInterface $model): bool return true; } + /** + * @param string $name + * @return CountRelation|null + */ + protected function getHitCountRelation(string $name): ?CountRelation + { + return match ($name) { + "eq" => CountRelation::EQUALS, + "gte" => CountRelation::GREATER_THAN_OR_EQUALS, + }; + } + /** * @param Search $search * @return SearchResult @@ -215,6 +228,19 @@ public function search(Search $search): SearchResult } $result = new SearchResult(true); + if (isset($response->took) && is_int($response->took)) { + $result->setSearchTime($response->took); + } + + if (isset($response->hits->total) && is_object($response->hits->total)) { + if (isset($response->hits->total->value) && is_int($response->hits->total->value)) { + $result->setTotalCount($response->hits->total->value); + } + if (isset($response->hits->total->relation) && is_string($response->hits->total->relation)) { + $result->setTotalCountRelation($this->getHitCountRelation($response->hits->total->relation)); + } + } + foreach ($response->hits->hits as $resultDocument) { if (!isset($resultDocument->_id) || !is_string($resultDocument->_id)) { throw new SerializeException("Received invalid document _id from OpenSearch"); diff --git a/src/Search/CountRelation.php b/src/Search/CountRelation.php new file mode 100644 index 0000000..5a4c5f7 --- /dev/null +++ b/src/Search/CountRelation.php @@ -0,0 +1,9 @@ +searchTime; + } + + /** + * @param int|null $searchTime + * @return $this + */ + public function setSearchTime(?int $searchTime): static + { + $this->searchTime = $searchTime; + return $this; + } + + /** + * @return int|null + */ + public function getTotalCount(): ?int + { + return $this->totalCount; + } + + /** + * @param int|null $totalCount + * @return $this + */ + public function setTotalCount(?int $totalCount): static + { + $this->totalCount = $totalCount; + return $this; + } + + /** + * @return CountRelation|null + */ + public function getTotalCountRelation(): ?CountRelation + { + return $this->totalCountRelation; + } + + /** + * @param CountRelation|null $totalCountRelation + * @return $this + */ + public function setTotalCountRelation(?CountRelation $totalCountRelation): static + { + $this->totalCountRelation = $totalCountRelation; + return $this; + } +}