Can I scan and scroll using this API?
Yes it is. My implementation works like this.
Start your search for elasticity scrolling:
public SearchResult startScrollSearch (String type, Long size) throws IOException { String query = ConfigurationFactory.loadElasticScript("my_es_search_script.json"); Search search = new Search.Builder(query) // multiple index or types can be added. .addIndex("myIndex") .addType(type) .setParameter(Parameters.SIZE, size) .setParameter(Parameters.SCROLL, "1m") .build(); SearchResult searchResult = EsClientConn.getJestClient().execute(search); return searchResult; }
The SearchResult object will return the first (size), which, as usual, will cancel the search, but will return to the scrollId parameter, which is a link to stay in the result set, which will be stored in memory for you. Parameters.SCROLL, will determine the time during which this search will be stored in memory.
To read scrollId:
scrollId = searchResult.getJsonObject().get("_scroll_id").getAsString();
To get more detailed information from the result set, you should use something like the following:
public JestResult readMoreFromSearch(String scrollId, Long size) throws IOException { SearchScroll scroll = new SearchScroll.Builder(scrollId, "1m") .setParameter(Parameters.SIZE, size).build(); JestResult searchResult = EsClientConn.getJestClient().execute(scroll); return searchResult; }
Do not forget that every time you read the result, a new scrollId from elastic is returned.
Please tell me if you have any doubts.
source share