1919import org .elasticsearch .index .query .QueryBuilder ;
2020import org .elasticsearch .index .query .QueryBuilders ;
2121import org .elasticsearch .search .SearchHit ;
22- import org .elasticsearch .search .SearchHits ;
2322import org .elasticsearch .search .fetch .StoredFieldsContext ;
2423import org .elasticsearch .search .sort .SortOrder ;
2524import org .elasticsearch .xpack .core .ClientHelper ;
@@ -107,14 +106,14 @@ public void cancel() {
107106 isCancelled = true ;
108107 }
109108
110- public Optional <List < Row > > next () throws IOException {
109+ public Optional <SearchHit [] > next () throws IOException {
111110 if (hasNext () == false ) {
112111 throw new NoSuchElementException ();
113112 }
114113
115- Optional <List < Row > > hits = Optional .ofNullable (nextSearch ());
116- if (hits .isPresent () && hits .get ().isEmpty () == false ) {
117- lastSortKey = hits . get () .get (hits .get ().size () - 1 ). getSortKey () ;
114+ Optional <SearchHit [] > hits = Optional .ofNullable (nextSearch ());
115+ if (hits .isPresent () && hits .get ().length > 0 ) {
116+ lastSortKey = ( long ) hits .get ()[ hits .get ().length - 1 ]. getSortValues ()[ 0 ] ;
118117 } else {
119118 hasNext = false ;
120119 }
@@ -126,7 +125,7 @@ public Optional<List<Row>> next() throws IOException {
126125 * Does no sorting of the results.
127126 * @param listener To alert with the extracted rows
128127 */
129- public void preview (ActionListener <List <Row >> listener ) {
128+ public void preview (ActionListener <List <String [] >> listener ) {
130129
131130 SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder (client )
132131 // This ensures the search throws if there are failures and the scroll context gets cleared automatically
@@ -155,22 +154,24 @@ public void preview(ActionListener<List<Row>> listener) {
155154 return ;
156155 }
157156
158- List <Row > rows = new ArrayList <>(searchResponse .getHits ().getHits ().length );
157+ List <String [] > rows = new ArrayList <>(searchResponse .getHits ().getHits ().length );
159158 for (SearchHit hit : searchResponse .getHits ().getHits ()) {
160- var unpooled = hit .asUnpooled ();
161- String [] extractedValues = extractValues (unpooled );
162- rows .add (extractedValues == null ? new Row (null , unpooled , true ) : new Row (extractedValues , unpooled , false ));
159+ String [] extractedValues = extractValues (hit );
160+ rows .add (extractedValues );
163161 }
164162 delegate .onResponse (rows );
165163 })
166164 );
167165 }
168166
169- protected List <Row > nextSearch () throws IOException {
167+ protected SearchHit [] nextSearch () throws IOException {
168+ if (isCancelled ) {
169+ return null ;
170+ }
170171 return tryRequestWithSearchResponse (() -> executeSearchRequest (buildSearchRequest ()));
171172 }
172173
173- private List < Row > tryRequestWithSearchResponse (Supplier <SearchResponse > request ) throws IOException {
174+ private SearchHit [] tryRequestWithSearchResponse (Supplier <SearchResponse > request ) throws IOException {
174175 try {
175176
176177 // We've set allow_partial_search_results to false which means if something
@@ -179,7 +180,7 @@ private List<Row> tryRequestWithSearchResponse(Supplier<SearchResponse> request)
179180 try {
180181 LOGGER .trace (() -> "[" + context .jobId + "] Search response was obtained" );
181182
182- List < Row > rows = processSearchResponse (searchResponse );
183+ SearchHit [] rows = processSearchResponse (searchResponse );
183184
184185 // Request was successfully executed and processed so we can restore the flag to retry if a future failure occurs
185186 hasPreviousSearchFailed = false ;
@@ -246,22 +247,12 @@ private void setFetchSource(SearchRequestBuilder searchRequestBuilder) {
246247 }
247248 }
248249
249- private List < Row > processSearchResponse (SearchResponse searchResponse ) {
250- if (searchResponse .getHits ().getHits ().length == 0 ) {
250+ private SearchHit [] processSearchResponse (SearchResponse searchResponse ) {
251+ if (isCancelled || searchResponse .getHits ().getHits ().length == 0 ) {
251252 hasNext = false ;
252253 return null ;
253254 }
254-
255- SearchHits hits = searchResponse .getHits ();
256- List <Row > rows = new ArrayList <>(hits .getHits ().length );
257- for (SearchHit hit : hits ) {
258- if (isCancelled ) {
259- hasNext = false ;
260- break ;
261- }
262- rows .add (createRow (hit ));
263- }
264- return rows ;
255+ return searchResponse .getHits ().asUnpooled ().getHits ();
265256 }
266257
267258 private String extractNonProcessedValues (SearchHit hit , String organicFeature ) {
@@ -317,14 +308,13 @@ private String[] extractProcessedValue(ProcessedField processedField, SearchHit
317308 return extractedValue ;
318309 }
319310
320- private Row createRow (SearchHit hit ) {
321- var unpooled = hit .asUnpooled ();
322- String [] extractedValues = extractValues (unpooled );
311+ public Row createRow (SearchHit hit ) {
312+ String [] extractedValues = extractValues (hit );
323313 if (extractedValues == null ) {
324- return new Row (null , unpooled , true );
314+ return new Row (null , hit , true );
325315 }
326316 boolean isTraining = trainTestSplitter .get ().isTraining (extractedValues );
327- Row row = new Row (extractedValues , unpooled , isTraining );
317+ Row row = new Row (extractedValues , hit , isTraining );
328318 LOGGER .trace (
329319 () -> format (
330320 "[%s] Extracted row: sort key = [%s], is_training = [%s], values = %s" ,
0 commit comments