Skip to content

Commit 876203f

Browse files
committed
QueryUUID is added in query and result
- Result will not return the query anymore, but its reference
1 parent d8854b8 commit 876203f

File tree

7 files changed

+82
-51
lines changed

7 files changed

+82
-51
lines changed

Http/Http.php

-7
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,6 @@ class Http
100100
*/
101101
const TO_FIELD = 'to';
102102

103-
/**
104-
* @var string
105-
*
106-
* Purge Query object from response
107-
*/
108-
const PURGE_QUERY_FROM_RESPONSE_FIELD = 'incl_query';
109-
110103
/**
111104
* Get common query values.
112105
*

Query/Query.php

+36
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ class Query implements HttpTransportable
5656
*/
5757
const NO_MIN_SCORE = 0.0;
5858

59+
/**
60+
* @var string
61+
*
62+
* UUID
63+
*/
64+
private $UUID;
65+
5966
/**
6067
* @var Coordinate
6168
*
@@ -1378,6 +1385,30 @@ public function getSubqueries(): array
13781385
return $this->subqueries;
13791386
}
13801387

1388+
/**
1389+
* Identify it.
1390+
*
1391+
* @param string $UUID
1392+
*
1393+
* @return Query
1394+
*/
1395+
public function identifyWith(string $UUID)
1396+
{
1397+
$this->UUID = $UUID;
1398+
1399+
return $this;
1400+
}
1401+
1402+
/**
1403+
* Get identification.
1404+
*
1405+
* @return string|null
1406+
*/
1407+
public function getUUID(): ? string
1408+
{
1409+
return $this->UUID;
1410+
}
1411+
13811412
/**
13821413
* To array.
13831414
*
@@ -1386,6 +1417,7 @@ public function getSubqueries(): array
13861417
public function toArray(): array
13871418
{
13881419
return array_filter([
1420+
'uuid' => $this->UUID,
13891421
'q' => '' !== $this->getQueryText()
13901422
? $this->getQueryText()
13911423
: null,
@@ -1518,6 +1550,10 @@ public static function createFromArray(array $array): self
15181550
return Query::createFromArray($query);
15191551
}, $array['subqueries'] ?? []);
15201552

1553+
if (isset($array['uuid'])) {
1554+
$query->UUID = $array['uuid'];
1555+
}
1556+
15211557
return $query;
15221558
}
15231559
}

Repository/InMemoryRepository.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function query(
7676
);
7777

7878
$items = $this->items[$this->getIndexKey()] ?? [];
79-
$result = new Result($query, count($items), count($resultingItems));
79+
$result = new Result($query->getUUID(), count($items), count($resultingItems));
8080
foreach ($resultingItems as $resultingItem) {
8181
$result->addItem($resultingItem);
8282
}

Repository/TransformableRepository.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ private function applyTransformersToResult(Result $result): Result
148148
{
149149
return empty($result->getSubresults())
150150
? Result::create(
151-
$result->getQuery(),
151+
$result->getQueryUUID(),
152152
$result->getTotalItems(),
153153
$result->getTotalHits(),
154154
$result->getAggregations(),
@@ -160,7 +160,6 @@ private function applyTransformersToResult(Result $result): Result
160160
)
161161
)
162162
: Result::createMultiResult(
163-
$result->getQuery(),
164163
array_map(function (Result $subresult) {
165164
return $this->applyTransformersToResult($subresult);
166165
}, $result->getSubresults())

Result/Result.php

+20-24
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@
1717

1818
use Apisearch\Model\HttpTransportable;
1919
use Apisearch\Model\Item;
20-
use Apisearch\Query\Query;
2120

2221
/**
2322
* Class Result.
2423
*/
2524
class Result implements HttpTransportable
2625
{
2726
/**
28-
* @var Query
27+
* @var string
2928
*
30-
* Query associated
29+
* UUID
3130
*/
32-
private $query;
31+
private $queryUUID;
3332

3433
/**
3534
* @var Item[]
@@ -83,24 +82,24 @@ class Result implements HttpTransportable
8382
/**
8483
* Result constructor.
8584
*
86-
* @param Query $query
87-
* @param int $totalItems
88-
* @param int $totalHits
85+
* @param string|null $queryUUID
86+
* @param int $totalItems
87+
* @param int $totalHits
8988
*/
9089
public function __construct(
91-
Query $query,
90+
?string $queryUUID,
9291
int $totalItems,
9392
int $totalHits
9493
) {
95-
$this->query = $query;
94+
$this->queryUUID = $queryUUID;
9695
$this->totalItems = $totalItems;
9796
$this->totalHits = $totalHits;
9897
}
9998

10099
/**
101100
* Create by.
102101
*
103-
* @param Query $query
102+
* @param string|null $queryUUID
104103
* @param int $totalItems
105104
* @param int $totalHits
106105
* @param Aggregations|null $aggregations
@@ -110,15 +109,15 @@ public function __construct(
110109
* @return Result
111110
*/
112111
public static function create(
113-
Query $query,
112+
?string $queryUUID,
114113
int $totalItems,
115114
int $totalHits,
116115
? Aggregations $aggregations,
117116
array $suggests,
118117
array $items
119118
): self {
120119
$result = new self(
121-
$query,
120+
$queryUUID,
122121
$totalItems,
123122
$totalHits
124123
);
@@ -133,16 +132,13 @@ public static function create(
133132
/**
134133
* Create multiquery Result.
135134
*
136-
* @param Query $query
137135
* @param Result[] $subresults
138136
*
139137
* @return Result
140138
*/
141-
public static function createMultiResult(
142-
Query $query,
143-
array $subresults
144-
) {
145-
$result = new Result($query, 0, 0);
139+
public static function createMultiResult(array $subresults)
140+
{
141+
$result = new Result(null, 0, 0);
146142
$result->subresults = $subresults;
147143

148144
return $result;
@@ -320,13 +316,13 @@ public function getSuggests(): array
320316
}
321317

322318
/**
323-
* Get query.
319+
* Get query UUID.
324320
*
325-
* @return Query
321+
* @return string|null
326322
*/
327-
public function getQuery(): Query
323+
public function getQueryUUID(): ? string
328324
{
329-
return $this->query;
325+
return $this->queryUUID;
330326
}
331327

332328
/**
@@ -367,7 +363,7 @@ public function getSubresults(): array
367363
public function toArray(): array
368364
{
369365
return array_filter([
370-
'query' => $this->query->toArray(),
366+
'query_uuid' => $this->queryUUID,
371367
'total_items' => $this->totalItems,
372368
'total_hits' => $this->totalHits,
373369
'items' => array_map(function (Item $item) {
@@ -399,7 +395,7 @@ public function toArray(): array
399395
public static function createFromArray(array $array): self
400396
{
401397
$result = self::create(
402-
Query::createFromArray($array['query'] ?? []),
398+
$array['query_uuid'] ?? '',
403399
$array['total_items'] ?? 0,
404400
$array['total_hits'] ?? 0,
405401
isset($array['aggregations'])

Tests/Query/QueryTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function testDefaults()
7070
$this->assertEquals(Query::NO_MIN_SCORE, $query->getMinScore());
7171
$this->assertEquals([], $query->getMetadata());
7272
$this->assertEquals($query, HttpHelper::emulateHttpTransport($query));
73+
$this->assertNull($query->getUUID());
7374
}
7475

7576
/**
@@ -187,4 +188,15 @@ public function testSubqueries()
187188
]);
188189
$this->assertCount(3, $query->getSubqueries());
189190
}
191+
192+
/**
193+
* Test identifier.
194+
*/
195+
public function testIdentifier()
196+
{
197+
$query = Query::createMatchAll()->identifyWith('123');
198+
$this->assertEquals('123', $query->getUUID());
199+
$query = HttpHelper::emulateHttpTransport($query);
200+
$this->assertEquals('123', $query->getUUID());
201+
}
190202
}

Tests/Result/ResultTest.php

+12-17
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
use Apisearch\Model\Item;
1919
use Apisearch\Model\ItemUUID;
20-
use Apisearch\Query\Query;
2120
use Apisearch\Result\Aggregation;
2221
use Apisearch\Result\Aggregations;
2322
use Apisearch\Result\Result;
@@ -35,15 +34,14 @@ class ResultTest extends TestCase
3534
public function testToArray()
3635
{
3736
$result = new Result(
38-
Query::createMatchAll(),
37+
'123',
3938
2, 1
4039
);
4140
$resultArray = $result->toArray();
4241
$this->assertFalse(array_key_exists('items', $resultArray));
4342
$this->assertFalse(array_key_exists('aggregations', $resultArray));
4443
$this->assertFalse(array_key_exists('suggests', $resultArray));
45-
$this->assertEquals(Query::createMatchAll(), $result->getQuery());
46-
$this->assertFalse(array_key_exists('query', $resultArray));
44+
$this->assertEquals('123', $result->getQueryUUID());
4745
$this->assertEquals(1, $result->getTotalHits());
4846
$this->assertEquals(1, $resultArray['total_hits']);
4947
$this->assertEquals(2, $result->getTotalItems());
@@ -61,7 +59,7 @@ public function testToArray()
6159
public function testItems()
6260
{
6361
$result = new Result(
64-
Query::createMatchAll(),
62+
'123',
6563
2, 1
6664
);
6765
$result->addItem(Item::create(ItemUUID::createByComposedUUID('1~product')));
@@ -79,7 +77,7 @@ public function testItems()
7977
public function testAggregations()
8078
{
8179
$result = new Result(
82-
Query::createMatchAll(),
80+
'123',
8381
2, 1
8482
);
8583
$aggregations = new Aggregations(2);
@@ -103,7 +101,7 @@ public function testAggregations()
103101
public function testSuggests()
104102
{
105103
$result = new Result(
106-
Query::createMatchAll(),
104+
'123',
107105
2, 1
108106
);
109107
$result->addSuggest('hola');
@@ -118,7 +116,7 @@ public function testSuggests()
118116
public function testGetItemsGroupedByType()
119117
{
120118
$result = new Result(
121-
Query::createMatchAll(),
119+
'123',
122120
1, 1
123121
);
124122

@@ -153,9 +151,7 @@ public function testGetItemsGroupedByType()
153151
public function testCreateFromArrayAllValues()
154152
{
155153
$resultAsArray = [
156-
'query' => [
157-
'q' => 'engonga',
158-
],
154+
'query_uuid' => '123',
159155
'total_items' => 10,
160156
'total_hits' => 20,
161157
'aggregations' => [
@@ -186,7 +182,7 @@ public function testCreateFromArrayAllValues()
186182
];
187183

188184
$result = Result::createFromArray($resultAsArray);
189-
$this->assertEquals(Query::create('engonga'), $result->getQuery());
185+
$this->assertEquals('123', $result->getQueryUUID());
190186
$this->assertEquals(10, $result->getTotalItems());
191187
$this->assertEquals(20, $result->getTotalHits());
192188
$this->assertInstanceof(Aggregation::class, $result->getAggregation('gogo'));
@@ -200,11 +196,10 @@ public function testCreateFromArrayAllValues()
200196
*/
201197
public function testMultiResult()
202198
{
203-
$query = Query::createMatchAll();
204-
$result = Result::createMultiResult($query, [
205-
'res1' => Result::create($query, 10, 3, null, [], []),
206-
'res2' => Result::create($query, 10, 4, null, [], []),
207-
'res3' => Result::create($query, 10, 5, null, [], []),
199+
$result = Result::createMultiResult([
200+
'res1' => Result::create('1', 10, 3, null, [], []),
201+
'res2' => Result::create('2', 10, 4, null, [], []),
202+
'res3' => Result::create('3', 10, 5, null, [], []),
208203
]);
209204

210205
$this->assertCount(3, $result->getSubresults());

0 commit comments

Comments
 (0)