Skip to content

Commit a733288

Browse files
committed
Add count method to client
1 parent 63b1103 commit a733288

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/Client.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,22 @@ public function search(array $query, string $indexOrIndices = null, array $optio
180180
return $this->doRequest($this->createJsonRequest($method, $uri, ['query' => $query]));
181181
}
182182

183+
public function count(string $index, array $options = [], array $query = null): Promise
184+
{
185+
$method = 'GET';
186+
$uri = [$this->baseUri, $index];
187+
$uri[] = '_count';
188+
$uri = implode('/', $uri);
189+
if ($options) {
190+
$uri .= '?' . http_build_query($options);
191+
}
192+
$body = null;
193+
if (null !== $query) {
194+
$body = ['query' => $query];
195+
}
196+
return $this->doRequest($this->createJsonRequest($method, $uri, $body));
197+
}
198+
183199
private function createJsonRequest(string $method, string $uri, array $body = null): Request
184200
{
185201
$request = (new Request($uri, $method))

tests/Integration/ClientTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,36 @@ public function testSearch(): void
240240
$this->assertIsArray($response);
241241
$this->assertCount(1, $response['hits']['hits']);
242242
}
243+
244+
public function testCount(): void
245+
{
246+
Promise\wait($this->client->createIndex(self::TEST_INDEX));
247+
Promise\wait(
248+
$this->client->indexDocument(self::TEST_INDEX, '', ['payload' => []], ['refresh' => 'true'])
249+
);
250+
Promise\wait(
251+
$this->client->indexDocument(self::TEST_INDEX, '', ['payload' => []], ['refresh' => 'true'])
252+
);
253+
254+
$response = Promise\wait($this->client->count(self::TEST_INDEX));
255+
256+
$this->assertIsArray($response);
257+
$this->assertEquals(2, $response['count']);
258+
}
259+
260+
public function testCountWithQuery(): void
261+
{
262+
Promise\wait($this->client->createIndex(self::TEST_INDEX));
263+
Promise\wait(
264+
$this->client->indexDocument(self::TEST_INDEX, '', ['user' => 'kimchy'], ['refresh' => 'true'])
265+
);
266+
Promise\wait(
267+
$this->client->indexDocument(self::TEST_INDEX, '', ['user' => 'foo'], ['refresh' => 'true'])
268+
);
269+
270+
$response = Promise\wait($this->client->count(self::TEST_INDEX, [], ['term' => ['user' => 'kimchy']]));
271+
272+
$this->assertIsArray($response);
273+
$this->assertEquals(1, $response['count']);
274+
}
243275
}

0 commit comments

Comments
 (0)