Skip to content

Add some metadata from OpenSearch responses to SearchResult #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2025
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
26 changes: 26 additions & 0 deletions src/Driver/OpenSearch/OpenSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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");
Expand Down
9 changes: 9 additions & 0 deletions src/Search/CountRelation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Aternos\Model\Search;

enum CountRelation
{
case EQUALS;
case GREATER_THAN_OR_EQUALS;
}
58 changes: 57 additions & 1 deletion src/Search/SearchResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,61 @@
*/
class SearchResult extends ModelCollectionResult
{
protected ?int $searchTime = null;
protected ?int $totalCount = null;
protected ?CountRelation $totalCountRelation = null;

}
/**
* @return int|null
*/
public function getSearchTime(): ?int
{
return $this->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;
}
}
Loading