Skip to content

Commit

Permalink
Add count method to client
Browse files Browse the repository at this point in the history
  • Loading branch information
mmenozzi committed Jun 27, 2020
1 parent 63b1103 commit a733288
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,22 @@ public function search(array $query, string $indexOrIndices = null, array $optio
return $this->doRequest($this->createJsonRequest($method, $uri, ['query' => $query]));
}

public function count(string $index, array $options = [], array $query = null): Promise
{
$method = 'GET';
$uri = [$this->baseUri, $index];
$uri[] = '_count';
$uri = implode('/', $uri);
if ($options) {
$uri .= '?' . http_build_query($options);
}
$body = null;
if (null !== $query) {
$body = ['query' => $query];
}
return $this->doRequest($this->createJsonRequest($method, $uri, $body));
}

private function createJsonRequest(string $method, string $uri, array $body = null): Request
{
$request = (new Request($uri, $method))
Expand Down
32 changes: 32 additions & 0 deletions tests/Integration/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,36 @@ public function testSearch(): void
$this->assertIsArray($response);
$this->assertCount(1, $response['hits']['hits']);
}

public function testCount(): void
{
Promise\wait($this->client->createIndex(self::TEST_INDEX));
Promise\wait(
$this->client->indexDocument(self::TEST_INDEX, '', ['payload' => []], ['refresh' => 'true'])
);
Promise\wait(
$this->client->indexDocument(self::TEST_INDEX, '', ['payload' => []], ['refresh' => 'true'])
);

$response = Promise\wait($this->client->count(self::TEST_INDEX));

$this->assertIsArray($response);
$this->assertEquals(2, $response['count']);
}

public function testCountWithQuery(): void
{
Promise\wait($this->client->createIndex(self::TEST_INDEX));
Promise\wait(
$this->client->indexDocument(self::TEST_INDEX, '', ['user' => 'kimchy'], ['refresh' => 'true'])
);
Promise\wait(
$this->client->indexDocument(self::TEST_INDEX, '', ['user' => 'foo'], ['refresh' => 'true'])
);

$response = Promise\wait($this->client->count(self::TEST_INDEX, [], ['term' => ['user' => 'kimchy']]));

$this->assertIsArray($response);
$this->assertEquals(1, $response['count']);
}
}

0 comments on commit a733288

Please sign in to comment.