Briefly, this error occurs when Elasticsearch fails to clear the scroll context, which is used for scrolling through large sets of results. This could be due to an invalid scroll ID or a server-side issue. To resolve this, ensure that the scroll ID is correct and valid. If the issue persists, check the server logs for any underlying issues. You may also need to increase the server resources if the server is overloaded. Additionally, ensure that the Elasticsearch version is up-to-date as this could be a bug in an older version.
This guide will help you check for common problems that cause the log ” failed to build clear scroll entity ” to appear. To understand the issues related to this log, read the explanation below about the following Elasticsearch concepts: scroll, reindex.
Overview
In Elasticsearch, the concept of scroll comes into play when you have a large set of search results. Large search results are exhaustive for both the Elasticsearch cluster and the requesting client in terms of memory and processing. The scroll API enables you to take a snapshot of a large number of results from a single search request.
Examples
To perform a scroll search, you need to add the scroll parameter to a search query and specify how long Elasticsearch should keep the search context viable.
GET mydocs-2019/_search?scroll=40s { "size": 5000, "query": { "match_all": {} }, "sort": [ { "_doc": { "order": "asc" } } ] }
This query will return a maximum of 5000 hits. If the scroll is idle for more than 40 seconds, it will be deleted. The response will return the first page of the results and a scroll ID. You can use the scroll ID to get additional documents from the scroll. You’ll be able to keep retrieving the documents until you have all of them.
Notes
- Changes made to documents after the scroll will not show up in your results.
- When you are done with the scroll, you can delete it manually using the scroll ID.
DELETE _search/scroll/<scroll_id>
Overview
Reindex is the concept of copying existing data from a source index to a destination index which can be inside the same or a different cluster. Elasticsearch has a dedicated endpoint _reindex for this purpose. A reindexing is mostly required for updating mapping or settings.
Examples
Reindex data from a source index to destination index in the same cluster:
POST /_reindex?pretty { "source": { "index": "news" }, "dest": { "index": "news_v2" } }
Notes
- Reindex API does not copy settings and mappings from the source index to the destination index. You need to create the destination index with the desired settings and mappings before you begin the reindexing process.
- The API exposes an extensive list of configuration options to fetch data from the source index, such as query-based indexing and selecting multiple indices as the source index.
- In some scenarios reindex API is not useful, where reindexing requires complex data processing and data modification based on application logic. In this case, you can write your custom script using Elasticsearch scroll API to fetch the data from source index and bulk API to index data into destination index.
Log Context
Log “failed to build clear scroll entity” class name is RemoteRequestBuilders.java. We extracted the following from Elasticsearch source code for those seeking an in-depth context :
} try (XContentBuilder entity = JsonXContent.contentBuilder()) { entity.startObject().array("scroll_id"; scroll).endObject(); request.setJsonEntity(Strings.toString(entity)); } catch (IOException e) { throw new ElasticsearchException("failed to build clear scroll entity"; e); } return request; } }