Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Count API support #446

Merged
merged 1 commit into from
Oct 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Resources/doc/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,19 @@ It will construct a query:
For more query and filter examples take a look at the [Elasticsearch DSL library docs](https://github.com/ongr-io/ElasticsearchDSL/blob/master/docs/index.md). We covered all examples that we found in [Elasticsearch Query DSL documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html) how to cover in objective oriented way.

> The results parsing is the same like in the find functions.

## Results count

Elasticsearch bundle provides support for [Count API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-count.html). If you need only count results, this is faster way to approach this. Here's an example how to count cars by red color:

```php

$repo = $this->get('es.manager.default.cars');
$search = $repo->createSearch();

$termQuery = new TermQuery('color', 'red');
$search->addQuery($termQuery);

$count = $repo->count($search);

```
31 changes: 31 additions & 0 deletions Service/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,37 @@ public function execute(Search $search, $resultsType = self::RESULTS_OBJECT)
return $this->parseResult($results, $resultsType, $search->getScroll());
}

/**
* Counts documents by given search.
*
* @param Search $search
* @param array $params
* @param bool $returnRaw If set true returns raw response gotten from client.
*
* @return int|array
*/
public function count(Search $search, array $params = [], $returnRaw = false)
{
$body = array_merge(
[
'index' => $this->getManager()->getIndexName(),
'type' => $this->getTypes(),
'body' => $search->toArray(),
],
$params
);

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

if ($returnRaw) {
return $results;
} else {
return $results['count'];
}
}

/**
* Delete by query.
*
Expand Down
43 changes: 43 additions & 0 deletions Tests/Functional/Service/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,4 +551,47 @@ public function testPartialUpdateWithDocumentResponse()

$this->assertNotContains('id', $result['get']['fields']);
}

/**
* Tests results counting via search query.
*/
public function testCountApi()
{
$manager = $this->getManager();
$repository = $manager->getRepository('AcmeBarBundle:Product');

$matchAll = new MatchAllQuery();
$search = $repository->createSearch();
$search->addQuery($matchAll);

$count = $repository->count($search);

$this->assertEquals(4, $count);
}

/**
* Tests results counting raw response from client.
*/
public function testCountApiRawResponse()
{
$manager = $this->getManager();
$repository = $manager->getRepository('AcmeBarBundle:Product');

$matchAll = new MatchAllQuery();
$search = $repository->createSearch();
$search->addQuery($matchAll);

$count = $repository->count($search, [], true);

$this->assertTrue(is_array($count));
$this->assertEquals(4, $count['count']);

$shards = [
'total' => 5,
'successful' => 5,
'failed' => 0,
];

$this->assertEquals($shards, $count['_shards']);
}
}