Skip to content

Commit f0ca860

Browse files
authored
Merge branch '2.x' into 337-rethink-index-queue-services-with-workers
2 parents 0d8439d + af97989 commit f0ca860

File tree

8 files changed

+92
-31
lines changed

8 files changed

+92
-31
lines changed

doc/01_Installation/02_Upgrade.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Following steps are necessary during updating to newer versions.
44

55
## Upgrade to 2.2.0
66
- [Indexing] Added `id` column as new primary key to `generic_data_index_queue`. Please make sure to execute migrations.
7+
- [Searching] Added `trackTotalHits` parameter to `DefaultSearchService` and `SearchExecutionService`. The default value is true, which means that total hits will always be computed accurately, even if they exceed the search engines threshold for accurate hit calculation. Change this parameter to `null`, to use the default threshold, pass an integer value to set a specific one.
78

89
## Upgrade to 2.1.0
910
- Added support for Symfony 7

src/Model/DefaultSearch/Query/DateFilter.php

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,24 +98,19 @@ public function isEmpty(): bool
9898

9999
public function getParams(): array
100100
{
101-
$params = [
102-
'format' => "yyyy-MM-dd'T'HH:mm:ssz",
103-
];
101+
$params = ['format' => "yyyy-MM-dd'T'HH:mm:ssz"];
102+
104103
if ($this->onDate) {
105104
$params['gte'] = $this->getStartOfDay($this->onDate)->format(DateTimeInterface::ATOM);
106105
$params['lte'] = $this->getEndOfDay($this->onDate)->format(DateTimeInterface::ATOM);
107-
} else {
108-
if ($this->startDate) {
109-
$params['gte'] = $this->getStartOfDay($this->startDate)->format(DateTimeInterface::ATOM);
110-
}
111-
if ($this->endDate) {
112-
$params['lte'] = $this->getEndOfDay($this->endDate)->format(DateTimeInterface::ATOM);
113-
}
106+
107+
return [$this->field => $params];
114108
}
115109

116-
return [
117-
$this->field => $params,
118-
];
110+
$params = $this->addStartParams($params);
111+
$params = $this->addEndParams($params);
112+
113+
return [$this->field => $params];
119114
}
120115

121116
public function toArray(bool $withType = false): array
@@ -162,6 +157,40 @@ public function getOnDate(): Carbon
162157
return $this->onDate;
163158
}
164159

160+
private function addStartParams(array $params): array
161+
{
162+
if (!$this->startDate) {
163+
return $params;
164+
}
165+
166+
if ($this->endDate) {
167+
$params['gte'] = $this->getStartOfDay($this->startDate)->format(DateTimeInterface::ATOM);
168+
169+
return $params;
170+
}
171+
172+
$params['gte'] = $this->getEndOfDay($this->startDate)->format(DateTimeInterface::ATOM);
173+
174+
return $params;
175+
}
176+
177+
private function addEndParams(array $params): array
178+
{
179+
if (!$this->endDate) {
180+
return $params;
181+
}
182+
183+
if ($this->startDate) {
184+
$params['lte'] = $this->getEndOfDay($this->endDate)->format(DateTimeInterface::ATOM);
185+
186+
return $params;
187+
}
188+
189+
$params['lte'] = $this->getStartOfDay($this->endDate)->format(DateTimeInterface::ATOM);
190+
191+
return $params;
192+
}
193+
165194
private function getStartOfDay(Carbon $date): Carbon
166195
{
167196
if (!$this->roundToDay) {

src/SearchIndexAdapter/DefaultSearch/DefaultSearchService.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,17 @@ public function createPaginatedSearch(
244244
/**
245245
* @throws SearchFailedException
246246
*/
247-
public function search(AdapterSearchInterface $search, string $indexName): SearchResult
248-
{
249-
return $this->searchExecutionService->executeSearch($search, $indexName);
247+
public function search(
248+
AdapterSearchInterface $search,
249+
string $indexName,
250+
int|bool|null $trackTotalHits = true
251+
): SearchResult {
252+
return $this->searchExecutionService->executeSearch(
253+
$search,
254+
$indexName,
255+
$trackTotalHits
256+
257+
);
250258
}
251259

252260
/**

src/SearchIndexAdapter/DefaultSearch/Search/SearchExecutionService.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,29 @@ public function __construct(
4242
/**
4343
* @throws SearchFailedException
4444
*/
45-
public function executeSearch(AdapterSearchInterface $search, string $indexName): SearchResult
46-
{
45+
public function executeSearch(
46+
AdapterSearchInterface $search,
47+
string $indexName,
48+
int|bool|null $trackTotalHits = true
49+
): SearchResult {
4750
try {
4851
$stopWatch = new Stopwatch();
4952
$stopWatch->start('search');
5053

54+
$searchParams = [
55+
'index' => $indexName,
56+
'body' => $search->toArray(),
57+
];
58+
59+
if ($trackTotalHits !== null) {
60+
$searchParams['body']['track_total_hits'] = $trackTotalHits;
61+
}
62+
5163
$defaultSearchResult = $this
5264
->client
53-
->search([
54-
'index' => $indexName,
55-
'body' => $search->toArray(),
56-
]);
65+
->search($searchParams);
5766

5867
$executionTime = $stopWatch->stop('search')->getDuration();
59-
6068
} catch (Exception $e) {
6169
$searchInformation = new SearchInformation(
6270
$search,

src/SearchIndexAdapter/DefaultSearch/Search/SearchExecutionServiceInterface.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,16 @@
2424
interface SearchExecutionServiceInterface
2525
{
2626
/**
27+
* Execute a search query.
28+
* Set $trackTotalHits = true to enable accurate hit counts, an integer to set a maximum count or leave it at null to use the engines default value.
29+
*
2730
* @throws SearchFailedException
2831
*/
29-
public function executeSearch(AdapterSearchInterface $search, string $indexName): SearchResult;
32+
public function executeSearch(
33+
AdapterSearchInterface $search,
34+
string $indexName,
35+
int|bool|null $trackTotalHits = true
36+
): SearchResult;
3037

3138
/**
3239
* @return SearchInformation[]

src/SearchIndexAdapter/SearchIndexServiceInterface.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,15 @@ public function createPaginatedSearch(
5656
bool $aggregationsOnly = false
5757
): DefaultSearchInterface;
5858

59-
public function search(AdapterSearchInterface $search, string $indexName): SearchResult;
59+
/**
60+
* Execute a search query.
61+
* Set $trackTotalHits = true to enable accurate hit counts, an integer to set a maximum count or leave it at null to use the engines default value.
62+
*/
63+
public function search(
64+
AdapterSearchInterface $search,
65+
string $indexName,
66+
int|bool|null $trackTotalHits = true
67+
): SearchResult;
6068

6169
public function getStats(string $indexName): array;
6270

tests/Unit/Model/DefaultSearch/Query/DateFilterTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function testToArray(): void
5555
'range' => [
5656
'datefield' => [
5757
'format' => "yyyy-MM-dd'T'HH:mm:ssz",
58-
'gte' => '2000-01-01T00:00:00+00:00',
58+
'gte' => '2000-01-01T23:59:59+00:00',
5959
],
6060
],
6161
], $dateFilter->toArray(true));
@@ -66,7 +66,7 @@ public function testToArray(): void
6666
'range' => [
6767
'datefield' => [
6868
'format' => "yyyy-MM-dd'T'HH:mm:ssz",
69-
'lte' => '2000-01-01T23:59:59+00:00',
69+
'lte' => '2000-01-01T00:00:00+00:00',
7070
],
7171
],
7272
], $dateFilter->toArray(true));
@@ -108,7 +108,7 @@ public function testGetParams(): void
108108
self::assertSame([
109109
'datefield' => [
110110
'format' => "yyyy-MM-dd'T'HH:mm:ssz",
111-
'gte' => '2000-01-01T00:00:00+00:00',
111+
'gte' => '2000-01-01T23:59:59+00:00',
112112
],
113113
], $dateFilter->getParams());
114114

@@ -117,7 +117,7 @@ public function testGetParams(): void
117117
self::assertSame([
118118
'datefield' => [
119119
'format' => "yyyy-MM-dd'T'HH:mm:ssz",
120-
'lte' => '2000-01-01T23:59:59+00:00',
120+
'lte' => '2000-01-01T00:00:00+00:00',
121121
],
122122
], $dateFilter->getParams());
123123

tests/Unit/SearchIndexAdapter/Asset/FieldDefinitionAdapter/DateAdapterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function testApplySearchFilter()
123123
'range' => [
124124
'standard_fields.test.en' => [
125125
'format' => "yyyy-MM-dd'T'HH:mm:ssz",
126-
'gte' => '2000-01-01T00:00:00+00:00',
126+
'gte' => '2000-01-01T23:59:59+00:00',
127127
],
128128
],
129129
],
@@ -138,7 +138,7 @@ public function testApplySearchFilter()
138138
'range' => [
139139
'standard_fields.test.en' => [
140140
'format' => "yyyy-MM-dd'T'HH:mm:ssz",
141-
'lte' => '2000-01-01T23:59:59+00:00',
141+
'lte' => '2000-01-01T00:00:00+00:00',
142142
],
143143
],
144144
],

0 commit comments

Comments
 (0)