Skip to content

Commit 432929e

Browse files
authored
Merge pull request #2 from LulububuApps/feature/reintroduce-count
Feature/reintroduce count
2 parents 920a903 + a9c9cad commit 432929e

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

Service/IndexService.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,14 +308,35 @@ private function executeSearch(Search $search): array
308308

309309
public function getIndexDocumentCount(): int
310310
{
311-
$body = [
312-
'index' => $this->getIndexName(),
313-
'body' => [],
314-
];
311+
return $this->count();
312+
}
313+
314+
/**
315+
* Counts documents by given search.
316+
*
317+
* @param Search|null $search
318+
* @param array $params
319+
* @param bool $returnRaw If set true returns raw response gotten from client.
320+
*
321+
* @return int|array
322+
*/
323+
public function count(Search $search = null, array $params = [], $returnRaw = false)
324+
{
325+
$body = array_merge(
326+
[
327+
'index' => $this->getIndexName(),
328+
'body' => $search !== null ? $search->toArray() : [],
329+
],
330+
$params
331+
);
315332

316333
$results = $this->getClient()->count($body);
317334

318-
return $results['count'];
335+
if ($returnRaw) {
336+
return $results;
337+
} else {
338+
return $results['count'];
339+
}
319340
}
320341

321342
public function remove($id, $routing = null)

Tests/Functional/Service/IndexServiceTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use ONGR\App\Entity\DummyDocumentInTheEntityDirectory;
1616
use ONGR\ElasticsearchBundle\Result\DocumentIterator;
1717
use ONGR\ElasticsearchBundle\Test\AbstractElasticsearchTestCase;
18+
use ONGR\ElasticsearchDSL\Query\FullText\SimpleQueryStringQuery;
1819

1920
/**
2021
* Functional tests for orm manager.
@@ -120,4 +121,37 @@ public function testIndexConfigOverride()
120121

121122
$this->assertEquals(['localhost:9200'], $hosts);
122123
}
124+
125+
public function testCount()
126+
{
127+
$index = $this->getIndex(DummyDocument::class);
128+
129+
$search = $index->createSearch();
130+
$search->addQuery(new SimpleQueryStringQuery('foo'));
131+
132+
$count = $index->count($search);
133+
134+
$this->assertEquals(2, $count);
135+
}
136+
137+
public function testRawCount()
138+
{
139+
$index = $this->getIndex(DummyDocument::class);
140+
141+
$search = $index->createSearch();
142+
$search->addQuery(new SimpleQueryStringQuery('bar'));
143+
144+
$result = $index->count($search, [], true);
145+
146+
$this->assertEquals(1, $result['count']);
147+
}
148+
149+
public function testIndexDocumentCount()
150+
{
151+
$index = $this->getIndex(DummyDocument::class);
152+
153+
$count = $index->getIndexDocumentCount();
154+
155+
$this->assertEquals(3, $count);
156+
}
123157
}

0 commit comments

Comments
 (0)