19
19
import org .elasticsearch .xpack .ql .expression .Expressions ;
20
20
import org .elasticsearch .xpack .ql .expression .FieldAttribute ;
21
21
import org .elasticsearch .xpack .ql .expression .gen .pipeline .ConstantInput ;
22
+ import org .elasticsearch .xpack .ql .querydsl .container .Sort ;
22
23
import org .elasticsearch .xpack .ql .querydsl .query .Query ;
23
24
import org .elasticsearch .xpack .ql .type .DataTypes ;
24
25
25
26
import java .io .IOException ;
27
+ import java .util .LinkedHashMap ;
26
28
import java .util .List ;
29
+ import java .util .Map ;
27
30
import java .util .Objects ;
28
31
29
32
import static java .util .Collections .emptyList ;
33
+ import static java .util .Collections .emptyMap ;
30
34
import static org .elasticsearch .xpack .ql .util .CollectionUtils .combine ;
31
35
32
36
public class QueryContainer {
@@ -37,17 +41,19 @@ public class QueryContainer {
37
41
// list of fields available in the output
38
42
private final List <Tuple <FieldExtraction , String >> fields ;
39
43
44
+ private final Map <String , Sort > sort ;
40
45
private final boolean trackHits ;
41
46
private final boolean includeFrozen ;
42
47
43
48
public QueryContainer () {
44
- this (null , emptyList (), AttributeMap .emptyAttributeMap (), false , false );
49
+ this (null , emptyList (), AttributeMap .emptyAttributeMap (), emptyMap (), false , false );
45
50
}
46
51
47
- private QueryContainer (Query query , List <Tuple <FieldExtraction , String >> fields , AttributeMap <Expression > attributes , boolean trackHits ,
48
- boolean includeFrozen ) {
52
+ private QueryContainer (Query query , List <Tuple <FieldExtraction , String >> fields , AttributeMap <Expression > attributes ,
53
+ Map < String , Sort > sort , boolean trackHits , boolean includeFrozen ) {
49
54
this .query = query ;
50
55
this .fields = fields ;
56
+ this .sort = sort ;
51
57
this .attributes = attributes ;
52
58
this .trackHits = trackHits ;
53
59
this .includeFrozen = includeFrozen ;
@@ -65,12 +71,16 @@ public List<Tuple<FieldExtraction, String>> fields() {
65
71
return fields ;
66
72
}
67
73
74
+ public Map <String , Sort > sort () {
75
+ return sort ;
76
+ }
77
+
68
78
public boolean shouldTrackHits () {
69
79
return trackHits ;
70
80
}
71
81
72
82
public QueryContainer with (Query q ) {
73
- return new QueryContainer (q , fields , attributes , trackHits , includeFrozen );
83
+ return new QueryContainer (q , fields , attributes , sort , trackHits , includeFrozen );
74
84
}
75
85
76
86
public QueryContainer addColumn (Attribute attr ) {
@@ -98,6 +108,12 @@ private Tuple<QueryContainer, FieldExtraction> asFieldExtraction(Attribute attr)
98
108
throw new EqlIllegalArgumentException ("Unknown output attribute {}" , attr );
99
109
}
100
110
111
+ public QueryContainer addSort (String expressionId , Sort sortable ) {
112
+ Map <String , Sort > newSort = new LinkedHashMap <>(this .sort );
113
+ newSort .put (expressionId , sortable );
114
+ return new QueryContainer (query , fields , attributes , newSort , trackHits , includeFrozen );
115
+ }
116
+
101
117
//
102
118
// reference methods
103
119
//
@@ -139,7 +155,7 @@ private FieldExtraction topHitFieldRef(FieldAttribute fieldAttr) {
139
155
}
140
156
141
157
public QueryContainer addColumn (FieldExtraction ref , String id ) {
142
- return new QueryContainer (query , combine (fields , new Tuple <>(ref , id )), attributes , trackHits , includeFrozen );
158
+ return new QueryContainer (query , combine (fields , new Tuple <>(ref , id )), attributes , sort , trackHits , includeFrozen );
143
159
}
144
160
145
161
@ Override
0 commit comments