Skip to content
This repository was archived by the owner on Jun 25, 2025. It is now read-only.

Commit 135cdf5

Browse files
authored
Merge pull request #93 from apisearch-io/feature/index-can-be-forced-in-query
Added indexUUID in query
2 parents 587580e + 2b8f4f2 commit 135cdf5

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

Query/Query.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Apisearch\Geo\LocationRange;
2020
use Apisearch\Model\Coordinate;
2121
use Apisearch\Model\HttpTransportable;
22+
use Apisearch\Model\IndexUUID;
2223
use Apisearch\Model\Item;
2324
use Apisearch\Model\ItemUUID;
2425
use Apisearch\Model\User;
@@ -210,6 +211,13 @@ class Query implements HttpTransportable
210211
*/
211212
private $subqueries = [];
212213

214+
/**
215+
* @var IndexUUID
216+
*
217+
* Index UUID
218+
*/
219+
private $indexUUID;
220+
213221
/**
214222
* Construct.
215223
*
@@ -1409,6 +1417,30 @@ public function getUUID(): ? string
14091417
return $this->UUID;
14101418
}
14111419

1420+
/**
1421+
* Force Index UUID.
1422+
*
1423+
* @param IndexUUID $indexUUID
1424+
*
1425+
* @return Query
1426+
*/
1427+
public function forceIndexUUID(IndexUUID $indexUUID): Query
1428+
{
1429+
$this->indexUUID = $indexUUID;
1430+
1431+
return $this;
1432+
}
1433+
1434+
/**
1435+
* Get index UUID.
1436+
*
1437+
* @return IndexUUID|null
1438+
*/
1439+
public function getIndexUUID(): ? IndexUUID
1440+
{
1441+
return $this->indexUUID;
1442+
}
1443+
14121444
/**
14131445
* To array.
14141446
*
@@ -1477,6 +1509,9 @@ public function toArray(): array
14771509
return $itemUUID->toArray();
14781510
}, $this->itemsPromoted)
14791511
),
1512+
'index_uuid' => ($this->indexUUID instanceof IndexUUID)
1513+
? $this->indexUUID->toArray()
1514+
: null,
14801515
], function ($element) {
14811516
return
14821517
!(
@@ -1551,6 +1586,9 @@ public static function createFromArray(array $array): self
15511586
if (isset($array['uuid'])) {
15521587
$query->UUID = $array['uuid'];
15531588
}
1589+
if (isset($array['index_uuid'])) {
1590+
$query->indexUUID = IndexUUID::createFromArray($array['index_uuid']);
1591+
}
15541592

15551593
return $query;
15561594
}

Tests/Query/QueryTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace Apisearch\Tests\Query;
1717

18+
use Apisearch\Model\IndexUUID;
1819
use Apisearch\Query\Query;
1920
use Apisearch\Query\ScoreStrategies;
2021
use Apisearch\Query\SortBy;
@@ -71,6 +72,7 @@ public function testDefaults()
7172
$this->assertEquals([], $query->getMetadata());
7273
$this->assertEquals($query, HttpHelper::emulateHttpTransport($query));
7374
$this->assertNull($query->getUUID());
75+
$this->assertNull($query->getIndexUUID());
7476
}
7577

7678
/**
@@ -202,4 +204,18 @@ public function testIdentifier()
202204
$query = HttpHelper::emulateHttpTransport($query);
203205
$this->assertEquals('123', $query->getUUID());
204206
}
207+
208+
/**
209+
* Test indexUUID.
210+
*/
211+
public function testIndexUUID()
212+
{
213+
$indexUUID = IndexUUID::createById('123');
214+
$query = Query::createMatchAll()->forceIndexUUID($indexUUID);
215+
$this->assertEquals($indexUUID, $query->getIndexUUID());
216+
$this->assertEquals($indexUUID->toArray(), $query->toArray()['index_uuid']);
217+
$query = HttpHelper::emulateHttpTransport($query);
218+
$this->assertEquals($indexUUID, $query->getIndexUUID());
219+
$this->assertEquals($indexUUID->toArray(), $query->toArray()['index_uuid']);
220+
}
205221
}

0 commit comments

Comments
 (0)