Skip to content

Commit

Permalink
[DOCS] Update search_after section with an example (#89328)
Browse files Browse the repository at this point in the history
* [DOCS] Update search_after section with an example

* Update docs/reference/search/search-your-data/paginate-search-results.asciidoc

Co-authored-by: Abdon Pijpelink <[email protected]>

* Update docs/reference/search/search-your-data/paginate-search-results.asciidoc

Co-authored-by: Abdon Pijpelink <[email protected]>

* Update docs/reference/search/search-your-data/paginate-search-results.asciidoc

Co-authored-by: Abdon Pijpelink <[email protected]>

Co-authored-by: Abdon Pijpelink <[email protected]>
  • Loading branch information
anthonymcglone2022 and abdonpijpelink authored Aug 17, 2022
1 parent f2257ca commit af8ac50
Showing 1 changed file with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,53 @@ You can use the `search_after` parameter to retrieve the next page of hits
using a set of <<sort-search-results,sort values>> from the previous page.

Using `search_after` requires multiple search requests with the same `query` and
`sort` values. If a <<near-real-time,refresh>> occurs between these requests,
`sort` values. The first step is to run an initial request. The following
example sorts the results by two fields (`date` and `tie_breaker_id`):
[source,js]
--------------------------------------------------
GET twitter/_search
{
"query": {
"match" : {
"title" : "elasticsearch"
}
},
"sort": [
{"date": "asc"},
{"tie_breaker_id": "asc"} <1>
]
}
--------------------------------------------------
// CONSOLE
// TEST[setup:twitter]
// TEST[s/"tie_breaker_id": "asc"/"tie_breaker_id": {"unmapped_type": "keyword"}/]

<1> A copy of the `_id` field with `doc_values` enabled

The search response includes an array of `sort` values for each hit. To retrieve
the next page of results, repeat the request, take the `sort` values from the
last hit, and insert those into the `search_after` array:
[source,js]
--------------------------------------------------
GET twitter/_search
{
"query": {
"match" :
"title" : "elasticsearch"
}
},
"search_after": [1463538857, "654323"],
"sort": [
{"date": "asc"},
{"tie_breaker_id": "asc"}
]
}
--------------------------------------------------
// CONSOLE
// TEST[setup:twitter]
// TEST[s/"tie_breaker_id": "asc"/"tie_breaker_id": {"unmapped_type": "keyword"}/]
Repeat this process by updating the `search_after` array every time you retrieve a
new page of results. If a <<near-real-time,refresh>> occurs between these requests,
the order of your results may change, causing inconsistent results across pages. To
prevent this, you can create a <<point-in-time-api,point in time (PIT)>> to
preserve the current index state over your searches.
Expand Down

0 comments on commit af8ac50

Please sign in to comment.