Skip to content

Commit 3407033

Browse files
authored
Merge pull request #18 from KurtThiemann/opensearch-meta
Add some metadata from OpenSearch responses to SearchResult
2 parents 59f7fc1 + f59ffb1 commit 3407033

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

src/Driver/OpenSearch/OpenSearch.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Aternos\Model\Driver\OpenSearch\Exception\OpenSearchException;
1212
use Aternos\Model\Driver\OpenSearch\Exception\SerializeException;
1313
use Aternos\Model\ModelInterface;
14+
use Aternos\Model\Search\CountRelation;
1415
use Aternos\Model\Search\Search;
1516
use Aternos\Model\Search\SearchResult;
1617
use Psr\Http\Client\ClientInterface;
@@ -192,6 +193,18 @@ public function delete(ModelInterface $model): bool
192193
return true;
193194
}
194195

196+
/**
197+
* @param string $name
198+
* @return CountRelation|null
199+
*/
200+
protected function getHitCountRelation(string $name): ?CountRelation
201+
{
202+
return match ($name) {
203+
"eq" => CountRelation::EQUALS,
204+
"gte" => CountRelation::GREATER_THAN_OR_EQUALS,
205+
};
206+
}
207+
195208
/**
196209
* @param Search $search
197210
* @return SearchResult
@@ -215,6 +228,19 @@ public function search(Search $search): SearchResult
215228
}
216229

217230
$result = new SearchResult(true);
231+
if (isset($response->took) && is_int($response->took)) {
232+
$result->setSearchTime($response->took);
233+
}
234+
235+
if (isset($response->hits->total) && is_object($response->hits->total)) {
236+
if (isset($response->hits->total->value) && is_int($response->hits->total->value)) {
237+
$result->setTotalCount($response->hits->total->value);
238+
}
239+
if (isset($response->hits->total->relation) && is_string($response->hits->total->relation)) {
240+
$result->setTotalCountRelation($this->getHitCountRelation($response->hits->total->relation));
241+
}
242+
}
243+
218244
foreach ($response->hits->hits as $resultDocument) {
219245
if (!isset($resultDocument->_id) || !is_string($resultDocument->_id)) {
220246
throw new SerializeException("Received invalid document _id from OpenSearch");

src/Search/CountRelation.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Aternos\Model\Search;
4+
5+
enum CountRelation
6+
{
7+
case EQUALS;
8+
case GREATER_THAN_OR_EQUALS;
9+
}

src/Search/SearchResult.php

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,61 @@
1313
*/
1414
class SearchResult extends ModelCollectionResult
1515
{
16+
protected ?int $searchTime = null;
17+
protected ?int $totalCount = null;
18+
protected ?CountRelation $totalCountRelation = null;
1619

17-
}
20+
/**
21+
* @return int|null
22+
*/
23+
public function getSearchTime(): ?int
24+
{
25+
return $this->searchTime;
26+
}
27+
28+
/**
29+
* @param int|null $searchTime
30+
* @return $this
31+
*/
32+
public function setSearchTime(?int $searchTime): static
33+
{
34+
$this->searchTime = $searchTime;
35+
return $this;
36+
}
37+
38+
/**
39+
* @return int|null
40+
*/
41+
public function getTotalCount(): ?int
42+
{
43+
return $this->totalCount;
44+
}
45+
46+
/**
47+
* @param int|null $totalCount
48+
* @return $this
49+
*/
50+
public function setTotalCount(?int $totalCount): static
51+
{
52+
$this->totalCount = $totalCount;
53+
return $this;
54+
}
55+
56+
/**
57+
* @return CountRelation|null
58+
*/
59+
public function getTotalCountRelation(): ?CountRelation
60+
{
61+
return $this->totalCountRelation;
62+
}
63+
64+
/**
65+
* @param CountRelation|null $totalCountRelation
66+
* @return $this
67+
*/
68+
public function setTotalCountRelation(?CountRelation $totalCountRelation): static
69+
{
70+
$this->totalCountRelation = $totalCountRelation;
71+
return $this;
72+
}
73+
}

0 commit comments

Comments
 (0)