Page through results
When a response includes nextToken, pass it in the next request with the same query, searchType, and filter.
-
A page can hold fewer items than
maxResults, or none, and still include anextToken, because AWS Context removes results the caller can’t see after ranking. Stop only when the response has nonextToken. -
A
SEMANTICorAUTOsearch returns at most 5 pages. AKEYWORDsearch returns results up to rank 250. -
Results aren’t a snapshot. If the Agent context changes while you page, an entity can repeat or be skipped.
For the nextToken and maxResults fields, see SearchAgentContext in the
AWS Context API Reference.
To page through all results
The following example uses the AWS SDK for Python (Boto3) to page through every result of a search. It passes nextToken until a response has none.
import boto3 client = boto3.client("context", region_name="us-east-1") agent_context_id = "ac-1234567890abcdef" token = None while True: kwargs = {"agentContextId": agent_context_id, "query": "late shipments", "searchType": "SEMANTIC"} if token: kwargs["nextToken"] = token page = client.search_agent_context(**kwargs) for item in page["items"]: print(item["nodeId"]) token = page.get("nextToken") if not token: break
The AWS CLI follows nextToken for you and prints every page. To page manually, use --max-items and --starting-token.
aws context search-agent-context \ --agent-context-id ac-1234567890abcdef \ --query "late shipments" \ --search-type SEMANTIC