Skip to content

Commit 6be6aaf

Browse files
Merge pull request #249922 from mattmsft/mattmsft-patch-1
Add strategy for avoiding high skip query
2 parents 54f4d6e + 8fb46a4 commit 6be6aaf

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

articles/search/search-pagination-page-layout.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Count won't be affected by routine maintenance or other workloads on the search
6868

6969
By default, the search engine returns up to the first 50 matches. The top 50 are determined by search score, assuming the query is full text search or semantic search. Otherwise, the top 50 are an arbitrary order for exact match queries (where uniform "@searchScore=1.0" indicates arbitrary ranking).
7070

71-
To control the paging of all documents returned in a result set, add `$top` and `$skip` parameters to the query request. The following list explains the logic.
71+
To control the paging of all documents returned in a result set, add `$top` and `$skip` parameters to the GET query request or `top` and `skip` to the POST query request. The following list explains the logic.
7272

7373
+ Return the first set of 15 matching documents plus a count of total matches: `GET /indexes/<INDEX-NAME>/docs?search=<QUERY STRING>&$top=15&$skip=0&$count=true`
7474

@@ -101,6 +101,40 @@ On the service, assume a fifth document is added to the index in between query c
101101

102102
Notice that document 2 is fetched twice. This is because the new document 5 has a greater value for rating, so it sorts before document 2 and lands on the first page. While this behavior might be unexpected, it's typical of how a search engine behaves.
103103

104+
### Paging through large numbers of results
105+
106+
Using `$top` and `$skip` allows a search query to page through 100,000 results. A value greater than 100,000 may not be used for `$skip`. It's possible to work around this limitation if a field has the ["filterable"](./search-filters.md) and ["sortable"] attributes.
107+
108+
1. Issue a query to return a full page of sorted results.
109+
```http
110+
POST /indexes/good-books/docs/search?api-version=2020-06-30
111+
{
112+
"search": "divine secrets",
113+
"top": 50,
114+
"orderby": "id asc"
115+
}
116+
```
117+
2. Choose the last result returned by the search query. An example result with only an "id" value is shown here.
118+
```json
119+
{
120+
"id": "50"
121+
}
122+
```
123+
3. Use that "id" value in a range query to fetch the next page of results. This "id" field should have unique values, otherwise pagination may include duplicate results.
124+
```http
125+
POST /indexes/good-books/docs/search?api-version=2020-06-30
126+
{
127+
"search": "divine secrets",
128+
"top": 50,
129+
"orderby": "id asc",
130+
"filter": "id ge 50"
131+
}
132+
```
133+
4. Pagination ends when the query returns 0 results.
134+
135+
> [!NOTE]
136+
> The "filterable" and "sortable" attributes can only be enabled when a field is first added to an index, they cannot be enabled on an existing field.
137+
104138
## Ordering results
105139

106140
In a full text search query, results can be ranked by:

0 commit comments

Comments
 (0)