Skip to content

Commit 032616e

Browse files
authored
Merge pull request #144 from Flowpack/139-getTotalItems-fix
BUGFIX: Correctly determine number of total search results This fixes ElasticSearchQueryBuilder::getTotalItems() that was checking a non-existing result index total to retrieve the number of search results. Fixes: #139
2 parents fa136a6 + c94695d commit 032616e

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

Classes/Flowpack/ElasticSearch/ContentRepositoryAdaptor/Eel/ElasticSearchQueryBuilder.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,9 +608,10 @@ public function log($message = null)
608608
*/
609609
public function getTotalItems()
610610
{
611-
if (array_key_exists('total', $this->result)) {
612-
return (int)$this->result['total'];
611+
if (isset($this->result['hits']['total'])) {
612+
return (int)$this->result['hits']['total'];
613613
}
614+
return 0;
614615
}
615616

616617
/**

Tests/Unit/Eel/ElasticSearchQueryBuilderTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,22 @@ public function existingRequestPropertiesCanBeOverridden()
353353
$this->assertEquals($expected, $actual['limit']);
354354
}
355355

356+
/**
357+
* @test
358+
*/
359+
public function getTotalItemsReturnsZeroByDefault()
360+
{
361+
$this->assertSame(0, $this->queryBuilder->getTotalItems());
362+
}
363+
364+
/**
365+
* @test
366+
*/
367+
public function getTotalItemsReturnsTotalHitsIfItExists()
368+
{
369+
$this->inject($this->queryBuilder, 'result', ['hits' => ['total' => 123]]);
370+
$this->assertSame(123, $this->queryBuilder->getTotalItems());
371+
}
356372

357373
/**
358374
* Test helper

0 commit comments

Comments
 (0)