From 2a958df2e6a2392b03f256fe96a1766118e0d5c3 Mon Sep 17 00:00:00 2001 From: Christian Bieg Date: Wed, 29 Apr 2020 13:46:19 +0200 Subject: [PATCH 1/2] Added the count function, that got lost during the 6.0 upgrade --- Service/IndexService.php | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/Service/IndexService.php b/Service/IndexService.php index 51a8dd62..2f52eac8 100644 --- a/Service/IndexService.php +++ b/Service/IndexService.php @@ -308,14 +308,35 @@ private function executeSearch(Search $search): array public function getIndexDocumentCount(): int { - $body = [ - 'index' => $this->getIndexName(), - 'body' => [], - ]; + return $this->count(); + } + + /** + * Counts documents by given search. + * + * @param Search|null $search + * @param array $params + * @param bool $returnRaw If set true returns raw response gotten from client. + * + * @return int|array + */ + public function count(Search $search = null, array $params = [], $returnRaw = false) + { + $body = array_merge( + [ + 'index' => $this->getIndexName(), + 'body' => $search !== null ? $search->toArray() : [], + ], + $params + ); $results = $this->getClient()->count($body); - return $results['count']; + if ($returnRaw) { + return $results; + } else { + return $results['count']; + } } public function remove($id, $routing = null) From a9c9cad057189a3c8ec1cabb51ef29cf79d6a69b Mon Sep 17 00:00:00 2001 From: Christian Bieg Date: Wed, 29 Apr 2020 14:15:57 +0200 Subject: [PATCH 2/2] Implemented some tests for the count --- Tests/Functional/Service/IndexServiceTest.php | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Tests/Functional/Service/IndexServiceTest.php b/Tests/Functional/Service/IndexServiceTest.php index 5d277188..09257b21 100644 --- a/Tests/Functional/Service/IndexServiceTest.php +++ b/Tests/Functional/Service/IndexServiceTest.php @@ -15,6 +15,7 @@ use ONGR\App\Entity\DummyDocumentInTheEntityDirectory; use ONGR\ElasticsearchBundle\Result\DocumentIterator; use ONGR\ElasticsearchBundle\Test\AbstractElasticsearchTestCase; +use ONGR\ElasticsearchDSL\Query\FullText\SimpleQueryStringQuery; /** * Functional tests for orm manager. @@ -120,4 +121,37 @@ public function testIndexConfigOverride() $this->assertEquals(['localhost:9200'], $hosts); } + + public function testCount() + { + $index = $this->getIndex(DummyDocument::class); + + $search = $index->createSearch(); + $search->addQuery(new SimpleQueryStringQuery('foo')); + + $count = $index->count($search); + + $this->assertEquals(2, $count); + } + + public function testRawCount() + { + $index = $this->getIndex(DummyDocument::class); + + $search = $index->createSearch(); + $search->addQuery(new SimpleQueryStringQuery('bar')); + + $result = $index->count($search, [], true); + + $this->assertEquals(1, $result['count']); + } + + public function testIndexDocumentCount() + { + $index = $this->getIndex(DummyDocument::class); + + $count = $index->getIndexDocumentCount(); + + $this->assertEquals(3, $count); + } }