I'm trying to combine pagination using nextToken with sorting in a single graphql query, like so:
const topPosts = await API.graphql(
graphqlOperation(searchPosts, {
sort: {
field: 'totalScore',
direction: 'desc',
}
})
);
In my case, the totalScore
field is a float. Thus, because the way the resolver is generated, this will be the only field used for the ElasticSearch sort
parameter.
## ...
"sort": [{$sortField0: { "order" : $util.toJson($sortDirection) }}],
## ...
This is a problem when attempting to paginate on the returned data. In my case totalScore is a non-unique field, and thus cannot be used by itself for the search_after
parameter in the ES request. The nextToken returned will always be the totalScore of the last item queried.
To work around this, I attempted to override the generated searchPosts resolver:
"params": {
"body": {
#if( $context.args.nextToken )"search_after": ,$context.args.nextToken, #end
"size": #if( $context.args.limit ) $context.args.limit #else 100 #end,
"sort": [{$sortField0: { "order" : $util.toJson($sortDirection) }} #if( $sortField == "id" ) #else, {"_id": { "order": "desc" }} #end],
"version": false,
"query": #if( $context.args.filter )
$util.transform.toElasticsearchQueryDSL($ctx.args.filter)
I also overrode the relevant SearchInput type in my schema to accept and return nextToken
as an array. Thus, with this code, I intended for the field to be sorted by my specified field, while using the unique id as a tie-breaker. Then the returned nextToken would be an array of the last returned item for both. However, when using this array in the request with the above code, my ID was not parsed properly as a String and would throw a syntax error in the VTL. Let me know how I should best approach my scenario.
Which Category is your question related to?
AWS ElasticSearch via @searchable
Amplify CLI Version
4.20.0
What AWS Services are you utilizing?
AppSync, ES
Same problem here.
Same problem here.
IDEM. Same problem here
same
Most helpful comment
Same problem here.