3434import org .elasticsearch .index .mapper .MappedFieldType ;
3535import org .elasticsearch .index .mapper .RangeFieldMapper ;
3636import org .elasticsearch .index .query .SearchExecutionContext ;
37+ import org .elasticsearch .search .internal .AliasFilter ;
3738
3839import java .io .IOException ;
40+ import java .io .UncheckedIOException ;
3941import java .util .ArrayList ;
4042import java .util .List ;
4143import java .util .function .IntFunction ;
4547 */
4648public abstract class QueryList {
4749 protected final SearchExecutionContext searchExecutionContext ;
50+ protected final AliasFilter aliasFilter ;
4851 protected final MappedFieldType field ;
4952 protected final Block block ;
5053 protected final boolean onlySingleValues ;
5154
52- protected QueryList (MappedFieldType field , SearchExecutionContext searchExecutionContext , Block block , boolean onlySingleValues ) {
55+ protected QueryList (
56+ MappedFieldType field ,
57+ SearchExecutionContext searchExecutionContext ,
58+ AliasFilter aliasFilter ,
59+ Block block ,
60+ boolean onlySingleValues
61+ ) {
5362 this .searchExecutionContext = searchExecutionContext ;
63+ this .aliasFilter = aliasFilter ;
5464 this .field = field ;
5565 this .block = block ;
5666 this .onlySingleValues = onlySingleValues ;
@@ -78,6 +88,17 @@ final Query getQuery(int position) {
7888
7989 Query query = doGetQuery (position , firstValueIndex , valueCount );
8090
91+ if (aliasFilter != null && aliasFilter != AliasFilter .EMPTY ) {
92+ BooleanQuery .Builder builder = new BooleanQuery .Builder ();
93+ builder .add (query , BooleanClause .Occur .FILTER );
94+ try {
95+ builder .add (aliasFilter .getQueryBuilder ().toQuery (searchExecutionContext ), BooleanClause .Occur .FILTER );
96+ query = builder .build ();
97+ } catch (IOException e ) {
98+ throw new UncheckedIOException ("Error while building query for alias filter" , e );
99+ }
100+ }
101+
81102 if (onlySingleValues ) {
82103 query = wrapSingleValueQuery (query );
83104 }
@@ -121,7 +142,12 @@ private Query wrapSingleValueQuery(Query query) {
121142 * using only the {@link ElementType} of the {@link Block} to determine the
122143 * query.
123144 */
124- public static QueryList rawTermQueryList (MappedFieldType field , SearchExecutionContext searchExecutionContext , Block block ) {
145+ public static QueryList rawTermQueryList (
146+ MappedFieldType field ,
147+ SearchExecutionContext searchExecutionContext ,
148+ AliasFilter aliasFilter ,
149+ Block block
150+ ) {
125151 IntFunction <Object > blockToJavaObject = switch (block .elementType ()) {
126152 case BOOLEAN -> {
127153 BooleanBlock booleanBlock = (BooleanBlock ) block ;
@@ -153,17 +179,22 @@ public static QueryList rawTermQueryList(MappedFieldType field, SearchExecutionC
153179 case AGGREGATE_METRIC_DOUBLE -> throw new IllegalArgumentException ("can't read values from [aggregate metric double] block" );
154180 case UNKNOWN -> throw new IllegalArgumentException ("can't read values from [" + block + "]" );
155181 };
156- return new TermQueryList (field , searchExecutionContext , block , false , blockToJavaObject );
182+ return new TermQueryList (field , searchExecutionContext , aliasFilter , block , false , blockToJavaObject );
157183 }
158184
159185 /**
160186 * Returns a list of term queries for the given field and the input block of
161187 * {@code ip} field values.
162188 */
163- public static QueryList ipTermQueryList (MappedFieldType field , SearchExecutionContext searchExecutionContext , BytesRefBlock block ) {
189+ public static QueryList ipTermQueryList (
190+ MappedFieldType field ,
191+ SearchExecutionContext searchExecutionContext ,
192+ AliasFilter aliasFilter ,
193+ BytesRefBlock block
194+ ) {
164195 BytesRef scratch = new BytesRef ();
165196 byte [] ipBytes = new byte [InetAddressPoint .BYTES ];
166- return new TermQueryList (field , searchExecutionContext , block , false , offset -> {
197+ return new TermQueryList (field , searchExecutionContext , aliasFilter , block , false , offset -> {
167198 final var bytes = block .getBytesRef (offset , scratch );
168199 if (ipBytes .length != bytes .length ) {
169200 // Lucene only support 16-byte IP addresses, even IPv4 is encoded in 16 bytes
@@ -178,10 +209,16 @@ public static QueryList ipTermQueryList(MappedFieldType field, SearchExecutionCo
178209 * Returns a list of term queries for the given field and the input block of
179210 * {@code date} field values.
180211 */
181- public static QueryList dateTermQueryList (MappedFieldType field , SearchExecutionContext searchExecutionContext , LongBlock block ) {
212+ public static QueryList dateTermQueryList (
213+ MappedFieldType field ,
214+ SearchExecutionContext searchExecutionContext ,
215+ AliasFilter aliasFilter ,
216+ LongBlock block
217+ ) {
182218 return new TermQueryList (
183219 field ,
184220 searchExecutionContext ,
221+ aliasFilter ,
185222 block ,
186223 false ,
187224 field instanceof RangeFieldMapper .RangeFieldType rangeFieldType
@@ -193,8 +230,14 @@ public static QueryList dateTermQueryList(MappedFieldType field, SearchExecution
193230 /**
194231 * Returns a list of geo_shape queries for the given field and the input block.
195232 */
196- public static QueryList geoShapeQueryList (MappedFieldType field , SearchExecutionContext searchExecutionContext , Block block ) {
197- return new GeoShapeQueryList (field , searchExecutionContext , block , false );
233+
234+ public static QueryList geoShapeQueryList (
235+ MappedFieldType field ,
236+ SearchExecutionContext searchExecutionContext ,
237+ AliasFilter aliasFilter ,
238+ Block block
239+ ) {
240+ return new GeoShapeQueryList (field , searchExecutionContext , aliasFilter , block , false );
198241 }
199242
200243 private static class TermQueryList extends QueryList {
@@ -203,17 +246,18 @@ private static class TermQueryList extends QueryList {
203246 private TermQueryList (
204247 MappedFieldType field ,
205248 SearchExecutionContext searchExecutionContext ,
249+ AliasFilter aliasFilter ,
206250 Block block ,
207251 boolean onlySingleValues ,
208252 IntFunction <Object > blockValueReader
209253 ) {
210- super (field , searchExecutionContext , block , onlySingleValues );
254+ super (field , searchExecutionContext , aliasFilter , block , onlySingleValues );
211255 this .blockValueReader = blockValueReader ;
212256 }
213257
214258 @ Override
215259 public TermQueryList onlySingleValues () {
216- return new TermQueryList (field , searchExecutionContext , block , true , blockValueReader );
260+ return new TermQueryList (field , searchExecutionContext , aliasFilter , block , true , blockValueReader );
217261 }
218262
219263 @ Override
@@ -241,18 +285,19 @@ private static class GeoShapeQueryList extends QueryList {
241285 private GeoShapeQueryList (
242286 MappedFieldType field ,
243287 SearchExecutionContext searchExecutionContext ,
288+ AliasFilter aliasFilter ,
244289 Block block ,
245290 boolean onlySingleValues
246291 ) {
247- super (field , searchExecutionContext , block , onlySingleValues );
292+ super (field , searchExecutionContext , aliasFilter , block , onlySingleValues );
248293
249294 this .blockValueReader = blockToGeometry (block );
250295 this .shapeQuery = shapeQuery ();
251296 }
252297
253298 @ Override
254299 public GeoShapeQueryList onlySingleValues () {
255- return new GeoShapeQueryList (field , searchExecutionContext , block , true );
300+ return new GeoShapeQueryList (field , searchExecutionContext , aliasFilter , block , true );
256301 }
257302
258303 @ Override
0 commit comments