Skip to content
This repository was archived by the owner on Sep 28, 2022. It is now read-only.
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
53 changes: 53 additions & 0 deletions doc/3/controllers/document/count/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
code: true
type: page
title: count
description: Counts documents in a collection.
---

# count

Counts documents in a collection.

A query can be provided to alter the count result, otherwise returns the total number of documents in the collection.

Kuzzle uses the [ElasticSearch Query DSL](https://www.elastic.co/guide/en/elasticsearch/reference/7.4/query-dsl.html) syntax.

---

## Arguments

```java
public CompletableFuture<Integer> count(
final String index,
final String collection)
throws NotConnectedException, InternalException

```

```java
public CompletableFuture<Integer> count(
final String index,
final String collection,
final ConcurrentHaspMap<String, Object> searchQuery)
throws NotConnectedException, InternalException

```

---

| Argument | Type | Description |
| ------------------ | -------------------------------------------- | --------------- |
| `index` | <pre>String</pre> | Index name |
| `collection` | <pre>String</pre> | Collection name |
| `searchQuery` | <pre>ConcurrentHashMap<String, Object></pre> | Query to match |

---

## Return

Returns an Integer.

## Usage

<<< ./snippets/count.java
8 changes: 8 additions & 0 deletions doc/3/controllers/document/count/snippets/count.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ConcurrentHashMap<String, Object> searchQuery = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> match = new ConcurrentHashMap<>();
match.put("Hello", "Clarisse");
searchQuery.put("match", match);
Integer result = kuzzle
.getDocumentController()
.count("nyc-open-data", "yellow-taxi", searchQuery)
.get();
18 changes: 18 additions & 0 deletions doc/3/controllers/document/count/snippets/count.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
name: document#count
description: Counts documents matching the given query
hooks:
before: |
curl -XDELETE kuzzle:7512/nyc-open-data
curl -XPOST kuzzle:7512/nyc-open-data/_create
curl -XPUT kuzzle:7512/nyc-open-data/yellow-taxi
for i in 1 2 3 4 5; do
curl -H "Content-type: application/json" -d '{"Hello": "Clarisse"}' kuzzle:7512/nyc-open-data/yellow-taxi/_create
done
for i in 1 2 3 4 5; do
curl -H "Content-type: application/json" -d '{}' kuzzle:7512/nyc-open-data/yellow-taxi/_create
done
curl -XPOST kuzzle:7512/nyc-open-data/yellow-taxi/_refresh
after:
template: print-result
expected: 5
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.kuzzle.sdk.API.Controllers;

import com.google.gson.internal.LazilyParsedNumber;
import io.kuzzle.sdk.CoreClasses.Maps.KuzzleMap;
import io.kuzzle.sdk.Exceptions.InternalException;
import io.kuzzle.sdk.Exceptions.NotConnectedException;
Expand Down Expand Up @@ -593,4 +594,51 @@ public CompletableFuture<ConcurrentHashMap<String, ArrayList<Object>>> mCreateOr

return this.mCreateOrReplace(index, collection, documents, null);
}

/**
* Counts documents in a collection.
*
* @param index
* @param collection
* @paran searchQuery
* @return a CompletableFuture
* @throws NotConnectedException
* @throws InternalException
*/
public CompletableFuture<Integer> count(
final String index,
final String collection,
final ConcurrentHashMap<String, Object> searchQuery) throws NotConnectedException, InternalException {

final KuzzleMap query = new KuzzleMap();
query
.put("index", index)
.put("collection", collection)
.put("controller", "document")
.put("body", new KuzzleMap().put("query", searchQuery))
.put("action", "count");

return kuzzle
.query(query)
.thenApplyAsync(
(response) -> ((LazilyParsedNumber) ((ConcurrentHashMap<String, Object>)response.result).get("count")).intValue());
}

/**
* Counts documents in a collection.
*
* @param index
* @param collection
* @return a CompletableFuture
* @throws NotConnectedException
* @throws InternalException
*/
public CompletableFuture<Integer> count(
final String index,
final String collection) throws NotConnectedException, InternalException {

final ConcurrentHashMap<String, Object> searchQuery = new ConcurrentHashMap<>();

return this.count(index, collection, searchQuery);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -814,4 +814,59 @@ public void mCreateOrReplaceDocumentShouldThrowWhenNotConnected() throws NotConn

kuzzleMock.getDocumentController().mCreateOrReplace(index, collection, documents);
}

@Test
public void countDocumentTestA() throws NotConnectedException, InternalException {

Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol));
String index = "nyc-open-data";
String collection = "yellow-taxi";

ArgumentCaptor<KuzzleMap> arg = ArgumentCaptor.forClass(KuzzleMap.class);

kuzzleMock.getDocumentController().count(index, collection);
Mockito.verify(kuzzleMock, Mockito.times(1)).query(arg.capture());

assertEquals((arg.getValue()).getString("controller"), "document");
assertEquals((arg.getValue()).getString("action"), "count");
assertEquals((arg.getValue()).getString("index"), "nyc-open-data");
assertEquals((arg.getValue()).getString("collection"), "yellow-taxi");
}

@Test
public void countDocumentTestB() throws NotConnectedException, InternalException {

Kuzzle kuzzleMock = spy(new Kuzzle(networkProtocol));
String index = "nyc-open-data";
String collection = "yellow-taxi";

ArgumentCaptor<KuzzleMap> arg = ArgumentCaptor.forClass(KuzzleMap.class);

ConcurrentHashMap<String, Object> searchQuery = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> match = new ConcurrentHashMap<>();
match.put("Hello", "Clarisse");
searchQuery.put("match", match);

kuzzleMock.getDocumentController().count(index, collection, searchQuery);
Mockito.verify(kuzzleMock, Mockito.times(1)).query(arg.capture());

assertEquals((arg.getValue()).getString("controller"), "document");
assertEquals((arg.getValue()).getString("action"), "count");
assertEquals((arg.getValue()).getString("index"), "nyc-open-data");
assertEquals((arg.getValue()).getString("collection"), "yellow-taxi");
assertEquals(((ConcurrentHashMap<String, Object>)((ConcurrentHashMap<String, Object>)(((KuzzleMap)(arg.getValue()).get("body"))).get("query")).get("match")).get("Hello"), "Clarisse");

}

@Test(expected = NotConnectedException.class)
public void countDocumentShouldThrowWhenNotConnected() throws NotConnectedException, InternalException {
AbstractProtocol fakeNetworkProtocol = Mockito.mock(WebSocket.class);
Mockito.when(fakeNetworkProtocol.getState()).thenAnswer((Answer<ProtocolState>) invocation -> ProtocolState.CLOSE);

Kuzzle kuzzleMock = spy(new Kuzzle(fakeNetworkProtocol));
String index = "nyc-open-data";
String collection = "yellow-taxi";

kuzzleMock.getDocumentController().count(index, collection);
}
}