Skip to content
This repository was archived by the owner on Jun 25, 2025. It is now read-only.
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
38 changes: 38 additions & 0 deletions Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Apisearch\Geo\LocationRange;
use Apisearch\Model\Coordinate;
use Apisearch\Model\HttpTransportable;
use Apisearch\Model\IndexUUID;
use Apisearch\Model\Item;
use Apisearch\Model\ItemUUID;
use Apisearch\Model\User;
Expand Down Expand Up @@ -210,6 +211,13 @@ class Query implements HttpTransportable
*/
private $subqueries = [];

/**
* @var IndexUUID
*
* Index UUID
*/
private $indexUUID;

/**
* Construct.
*
Expand Down Expand Up @@ -1409,6 +1417,30 @@ public function getUUID(): ? string
return $this->UUID;
}

/**
* Force Index UUID.
*
* @param IndexUUID $indexUUID
*
* @return Query
*/
public function forceIndexUUID(IndexUUID $indexUUID): Query
{
$this->indexUUID = $indexUUID;

return $this;
}

/**
* Get index UUID.
*
* @return IndexUUID|null
*/
public function getIndexUUID(): ? IndexUUID
{
return $this->indexUUID;
}

/**
* To array.
*
Expand Down Expand Up @@ -1477,6 +1509,9 @@ public function toArray(): array
return $itemUUID->toArray();
}, $this->itemsPromoted)
),
'index_uuid' => ($this->indexUUID instanceof IndexUUID)
? $this->indexUUID->toArray()
: null,
], function ($element) {
return
!(
Expand Down Expand Up @@ -1551,6 +1586,9 @@ public static function createFromArray(array $array): self
if (isset($array['uuid'])) {
$query->UUID = $array['uuid'];
}
if (isset($array['index_uuid'])) {
$query->indexUUID = IndexUUID::createFromArray($array['index_uuid']);
}

return $query;
}
Expand Down
16 changes: 16 additions & 0 deletions Tests/Query/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace Apisearch\Tests\Query;

use Apisearch\Model\IndexUUID;
use Apisearch\Query\Query;
use Apisearch\Query\ScoreStrategies;
use Apisearch\Query\SortBy;
Expand Down Expand Up @@ -71,6 +72,7 @@ public function testDefaults()
$this->assertEquals([], $query->getMetadata());
$this->assertEquals($query, HttpHelper::emulateHttpTransport($query));
$this->assertNull($query->getUUID());
$this->assertNull($query->getIndexUUID());
}

/**
Expand Down Expand Up @@ -202,4 +204,18 @@ public function testIdentifier()
$query = HttpHelper::emulateHttpTransport($query);
$this->assertEquals('123', $query->getUUID());
}

/**
* Test indexUUID.
*/
public function testIndexUUID()
{
$indexUUID = IndexUUID::createById('123');
$query = Query::createMatchAll()->forceIndexUUID($indexUUID);
$this->assertEquals($indexUUID, $query->getIndexUUID());
$this->assertEquals($indexUUID->toArray(), $query->toArray()['index_uuid']);
$query = HttpHelper::emulateHttpTransport($query);
$this->assertEquals($indexUUID, $query->getIndexUUID());
$this->assertEquals($indexUUID->toArray(), $query->toArray()['index_uuid']);
}
}