SQL: Preserve field order in response again

We accidentally stopped returning fields in the same order that the
user asked for them in but some docs tests caught it.

Original commit: elastic/x-pack-elasticsearch@586e3cf207
This commit is contained in:
Nik Everett 2017-12-22 17:16:15 -05:00
parent 1003cf3829
commit 2b11f8b3ba
2 changed files with 8 additions and 6 deletions

View File

@ -12,6 +12,8 @@ import org.elasticsearch.xpack.sql.execution.search.FieldExtraction;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
@ -21,9 +23,10 @@ import java.util.Set;
* the resulting ES document as a field.
*/
public class SqlSourceBuilder {
final Set<String> sourceFields = new HashSet<>();
final Set<String> docFields = new HashSet<>();
final Map<String, Script> scriptFields = new HashMap<>();
// The LinkedHashMaps preserve the order of the fields in the response
final Set<String> sourceFields = new LinkedHashSet<>();
final Set<String> docFields = new LinkedHashSet<>();
final Map<String, Script> scriptFields = new LinkedHashMap<>();
boolean trackScores = false;

View File

@ -15,7 +15,6 @@ import java.util.Map;
import java.util.stream.Collectors;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
public class SqlSourceBuilderTests extends ESTestCase {
@ -35,8 +34,8 @@ public class SqlSourceBuilderTests extends ESTestCase {
assertTrue(source.trackScores());
FetchSourceContext fsc = source.fetchSource();
assertThat(Arrays.asList(fsc.includes()), containsInAnyOrder("foo", "foo2"));
assertThat(source.docValueFields(), containsInAnyOrder("bar", "bar2"));
assertThat(Arrays.asList(fsc.includes()), contains("foo", "foo2"));
assertThat(source.docValueFields(), contains("bar", "bar2"));
Map<String, Script> scriptFields = source.scriptFields()
.stream()
.collect(Collectors.toMap(SearchSourceBuilder.ScriptField::fieldName, SearchSourceBuilder.ScriptField::script));