Skip to content
Closed
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
27 changes: 27 additions & 0 deletions src/sdk-reference/js/6/search-result/forEachHit/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
layout: sdk.html.hbs
title: forEachHit
description: Executes an action on every retrieved documents
---

# SearchResult

Iterates through all pages of results and executes an action for every document.

## Arguments

```js
forEachHit(action);
```

<br/>

| Arguments | Type | Description |
|--------------|---------|-------------|
| ``action`` | <pre>Function</pre> | Action to execute |

**Note:**
* the callback function take a hit as only argument
* if the callback function returns a promise, they will be executed in parallel and the method will wait for their resolution

[snippet=foreachhit]
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
try {
const results = await kuzzle.document.search(
'nyc-open-data',
'yellow-taxi',
{},
{ from: 0, size: 1 });

const documents = [];

results.forEachHit(hit => documents.push(hit));

console.log(`Successfully retrieved ${documents.length} documents`);
} catch (error) {
console.error(error.message);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: searchresult#foreachhit
description: Executes an action of every results
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 '{"capacity": 4}' 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 '{"capacity": 7}' kuzzle:7512/nyc-open-data/yellow-taxi/_create
done

curl -XPOST kuzzle:7512/nyc-open-data/_refresh
after:
template: default
expected: Successfully retrieved 10 documents
sdk: js
version: 6