You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## What does this PR do?
:warning: Depends on kuzzleio/kuzzle#1600
Fix few bugs on the `SearchResult` pagination with `sort` and `size`.
The SDK now support every kind of ES sort:
- string value
- array of string
- array of object
Errors are now thrown when:
- the sort is invalid
- the sort combination does not identify one item only
### Other changes
- the `next` method now return rejected promises instead of throwing (inconsistent behavior that can lead to unhandled rejection)
- change `_uid` by `_id` because this field does not longer exists in ES 7.x
Copy file name to clipboardExpand all lines: doc/7/core-classes/search-result/next/index.md
+11-4Lines changed: 11 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,9 +20,9 @@ next();
20
20
21
21
Resolves to a `SearchResult` object, or to `null` if no more pages are available.
22
22
23
-
## Throw
23
+
## Rejects
24
24
25
-
This method throws an exception if:
25
+
This method returns a rejected promise with an error if:
26
26
27
27
- No pagination strategy can be applied (see below)
28
28
- If invoking it would lead to more than 10 000 items being retrieved with the `from/size` strategy
@@ -56,19 +56,26 @@ You can restrict the scroll session maximum duration under the `services.storage
56
56
57
57
If the initial search contains `sort` and `size` parameters, the `next` method retrieves the next page of results following the sort order, the last item of the current page acting as a live cursor.
58
58
59
-
To avoid too many duplicates, it is advised to provide a sort combination that will always identify one item only. The recommended way is to use the field `_uid` which is certain to contain one unique value for each document.
59
+
This strategy uses Elasticsearch [search_after](https://www.elastic.co/guide/en/elasticsearch/reference/7.4/search-request-body.html#request-body-search-search-after) parameter.
60
+
61
+
::: warning
62
+
You have to provide a sort combination that will always identify one item only. The recommended way is to use the field `_id` which is certain to contain one unique value for each document.
63
+
To prevent partial retrieval of results, the SDK will reject with an error if the sort combination can identify multiple items.
64
+
:::
60
65
61
66
Because this method does not freeze the search results between two calls, there can be missing or duplicated documents between two result pages.
62
67
63
68
This method efficiently mitigates the costs of scroll searches, but returns less consistent results: it's a middle ground, ideal for real-time search requests.
64
69
70
+
<<< ./snippets/sortsize.js
71
+
65
72
### Strategy: from / size
66
73
67
74
If the initial search contains `from` and `size` parameters, the `next` method retrieves the next page of result by incrementing the `from` offset.
68
75
69
76
Because this method does not freeze the search results between two calls, there can be missing or duplicated documents between two result pages.
70
77
71
78
It's the fastest pagination method available, but also the less consistent, and it is not possible to retrieve more than 10000 items using it.
72
-
Above that limit, any call to `next`throws an Exception.
79
+
Above that limit, any call to `next`will return a rejected promise with an error.
// When sorting only on a non unique field, the search_after will not iterate
45
+
// over all documents having the same values but ES will returns the results
46
+
// directly after.
47
+
// It resulting in having less fetched documents than the total and thus the SDK
48
+
// try to fetch the next results page but it's empty
49
+
if(!hit){
50
+
returnPromise.reject(newError('Unable to retrieve all results from search: the sort combination must identify one item only. Add document "_id" to the sort.'));
51
+
}
46
52
47
53
request.body.search_after=[];
48
54
49
-
for(constsortofthis._request.body.sort){
55
+
letsorts;
56
+
if(typeofthis._request.body.sort==='string'){
57
+
sorts=[this._request.body.sort];
58
+
}
59
+
elseif(Array.isArray(this._request.body.sort)){
60
+
sorts=this._request.body.sort;
61
+
}
62
+
else{
63
+
sorts=Object.keys(this._request.body.sort);
64
+
}
65
+
66
+
if(sorts.length===0){
67
+
returnPromise.reject(newError('Unable to retrieve next results from search: sort param is empty'));
0 commit comments