Skip to content

Commit

Permalink
Merge pull request #63 from arnordhaenens/feature/count_documents
Browse files Browse the repository at this point in the history
Add support for Count API
  • Loading branch information
Douglasdc3 authored Jan 26, 2021
2 parents 60f55c7 + 17d01e5 commit 1fff34d
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,16 @@ public function aggregations(): Aggregation
return $this->aggregations;
}

/**
* Return the Dsl Query object.
*
* @return \AviationCode\Elasticsearch\Query\Dsl\Query
*/
public function query(): Query
{
return $this->query;
}

/**
* Perform a raw elastic query.
*
Expand Down Expand Up @@ -378,6 +388,16 @@ public function get()
], $this->model->getIndexName(), ['typed_keys' => true]), $this->model);
}

public function count(): int
{
return (int) ($this->getClient()->count(
[
'index' => $this->model->getIndexName(),
'body' => array_filter(['query' => $this->query->toArray()]),
]
)['count']);
}

/**
* Force a clone of the underlying query builder when cloning.
*
Expand Down
111 changes: 111 additions & 0 deletions tests/Feature/CountTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace AviationCode\Elasticsearch\Tests\Feature;

use AviationCode\Elasticsearch\Query\Dsl\Boolean\Must;
use AviationCode\Elasticsearch\Tests\Feature\TestModels\Article;
use Elasticsearch\Client;

class CountTest extends TestCase
{
/**
* @var Client|\Mockery\LegacyMockInterface|\Mockery\MockInterface
*/
private $client;

protected function setUp(): void
{
parent::setUp();
$this->app->instance('elasticsearch.client', $this->client = \Mockery::mock(Client::class));
}

/** @test */
public function it_can_count_the_documents_without_filtering()
{
$this->client
->shouldReceive('count')
->with(['index' => 'article', 'body' => []])
->andReturn(
[
'count' => 122,
'_shards' => [
'total' => 1,
'successful' => 1,
'skipped' => 0,
'failed' => 0,
],
]
);

$this->assertSame(122, $this->elastic->query(Article::class)->count());
}

/** @test */
public function it_can_count_the_documents_without_an_eloquent_model()
{
$this->client
->shouldReceive('count')
->with(['index' => 'article', 'body' => []])
->andReturn(
[
'count' => 6,
'_shards' => [
'total' => 1,
'successful' => 1,
'skipped' => 0,
'failed' => 0,
],
]
);

$this->assertSame(6, $this->elastic->query('article')->count());
}

/** @test */
public function it_can_count_the_matching_documents_with_filter()
{
$this->client
->shouldReceive('count')
->with(
[
'index' => 'article',
'body' => [
'query' => [
'bool' => [
'must' => [
[
'exists' => [
'field' => 'published_at',
],
],
],
],
],
],
]
)
->andReturn(
[
'count' => 12,
'_shards' => [
'total' => 1,
'successful' => 1,
'skipped' => 0,
'failed' => 0,
],
]
);

$this->assertSame(
12,
$this->elastic
->query('article')
->must(
function (Must $must) {
$must->exists('published_at');
}
)
->count()
);
}
}

0 comments on commit 1fff34d

Please sign in to comment.