Skip to content

Commit af97989

Browse files
authored
[Task] Add trackTotalHits parameter to enable accurate counting of hits (#349)
* Added trackTotalHits parameter to enable accurate counting of hits * Apply php-cs-fixer changes * Added null to union type for trackTotalHits * Apply php-cs-fixer changes * Changed default value to true * Apply php-cs-fixer changes * Added upgrade notes * Adapt version in upgrade note --------- Co-authored-by: mcop1 <[email protected]>
1 parent 60886ff commit af97989

File tree

5 files changed

+46
-12
lines changed

5 files changed

+46
-12
lines changed

doc/01_Installation/02_Upgrade.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
Following steps are necessary during updating to newer versions.
44

5+
## Upgrade to 2.2.0
6+
- 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.
7+
58
## Upgrade to 2.1.0
69
- Added support for Symfony 7
710
- [Indexing] Added sort index for documents

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

0 commit comments

Comments
 (0)