15
15
import org .elasticsearch .action .search .SearchScrollAction ;
16
16
import org .elasticsearch .action .search .SearchScrollRequestBuilder ;
17
17
import org .elasticsearch .client .Client ;
18
+ import org .elasticsearch .common .Nullable ;
18
19
import org .elasticsearch .common .unit .TimeValue ;
19
20
import org .elasticsearch .search .SearchHit ;
20
21
import org .elasticsearch .search .sort .SortOrder ;
@@ -68,18 +69,18 @@ public void cancel() {
68
69
isCancelled = true ;
69
70
}
70
71
71
- public Optional <List <String [] >> next () throws IOException {
72
+ public Optional <List <Row >> next () throws IOException {
72
73
if (!hasNext ()) {
73
74
throw new NoSuchElementException ();
74
75
}
75
- Optional <List <String [] >> records = scrollId == null ? Optional .ofNullable (initScroll ()) : Optional .ofNullable (continueScroll ());
76
- if (!records .isPresent ()) {
76
+ Optional <List <Row >> hits = scrollId == null ? Optional .ofNullable (initScroll ()) : Optional .ofNullable (continueScroll ());
77
+ if (!hits .isPresent ()) {
77
78
hasNext = false ;
78
79
}
79
- return records ;
80
+ return hits ;
80
81
}
81
82
82
- protected List <String [] > initScroll () throws IOException {
83
+ protected List <Row > initScroll () throws IOException {
83
84
LOGGER .debug ("[{}] Initializing scroll" , "analytics" );
84
85
SearchResponse searchResponse = executeSearchRequest (buildSearchRequest ());
85
86
LOGGER .debug ("[{}] Search response was obtained" , context .jobId );
@@ -106,7 +107,7 @@ private SearchRequestBuilder buildSearchRequest() {
106
107
return searchRequestBuilder ;
107
108
}
108
109
109
- private List <String [] > processSearchResponse (SearchResponse searchResponse ) throws IOException {
110
+ private List <Row > processSearchResponse (SearchResponse searchResponse ) throws IOException {
110
111
111
112
if (searchResponse .getFailedShards () > 0 && searchHasShardFailure == false ) {
112
113
LOGGER .debug ("[{}] Resetting scroll search after shard failure" , context .jobId );
@@ -123,29 +124,35 @@ private List<String[]> processSearchResponse(SearchResponse searchResponse) thro
123
124
}
124
125
125
126
SearchHit [] hits = searchResponse .getHits ().getHits ();
126
- List <String []> records = new ArrayList <>(hits .length );
127
+ List <Row > rows = new ArrayList <>(hits .length );
127
128
for (SearchHit hit : hits ) {
128
129
if (isCancelled ) {
129
130
hasNext = false ;
130
131
clearScroll (scrollId );
131
132
break ;
132
133
}
133
- records .add (toStringArray (hit ));
134
+ rows .add (createRow (hit ));
134
135
}
135
- return records ;
136
+ return rows ;
137
+
136
138
}
137
139
138
- private String [] toStringArray (SearchHit hit ) {
139
- String [] result = new String [context .extractedFields .getAllFields ().size ()];
140
- for (int i = 0 ; i < result .length ; ++i ) {
140
+ private Row createRow (SearchHit hit ) {
141
+ String [] extractedValues = new String [context .extractedFields .getAllFields ().size ()];
142
+ for (int i = 0 ; i < extractedValues .length ; ++i ) {
141
143
ExtractedField field = context .extractedFields .getAllFields ().get (i );
142
144
Object [] values = field .value (hit );
143
- result [i ] = (values .length == 1 ) ? Objects .toString (values [0 ]) : "" ;
145
+ if (values .length == 1 && values [0 ] instanceof Number ) {
146
+ extractedValues [i ] = Objects .toString (values [0 ]);
147
+ } else {
148
+ extractedValues = null ;
149
+ break ;
150
+ }
144
151
}
145
- return result ;
152
+ return new Row ( extractedValues ) ;
146
153
}
147
154
148
- private List <String [] > continueScroll () throws IOException {
155
+ private List <Row > continueScroll () throws IOException {
149
156
LOGGER .debug ("[{}] Continuing scroll with id [{}]" , context .jobId , scrollId );
150
157
SearchResponse searchResponse = executeSearchScrollRequest (scrollId );
151
158
LOGGER .debug ("[{}] Search response was obtained" , context .jobId );
@@ -210,4 +217,19 @@ public DataSummary(long rows, long cols) {
210
217
this .cols = cols ;
211
218
}
212
219
}
220
+
221
+ public static class Row {
222
+
223
+ @ Nullable
224
+ private String [] values ;
225
+
226
+ private Row (String [] values ) {
227
+ this .values = values ;
228
+ }
229
+
230
+ @ Nullable
231
+ public String [] getValues () {
232
+ return values ;
233
+ }
234
+ }
213
235
}
0 commit comments