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 ;
3940import java .io .UncheckedIOException ;
4647 */
4748public abstract class QueryList {
4849 protected final SearchExecutionContext searchExecutionContext ;
50+ protected final AliasFilter aliasFilter ;
4951 protected final MappedFieldType field ;
5052 protected final Block block ;
5153 @ Nullable
@@ -54,10 +56,12 @@ public abstract class QueryList {
5456 protected QueryList (
5557 MappedFieldType field ,
5658 SearchExecutionContext searchExecutionContext ,
59+ AliasFilter aliasFilter ,
5760 Block block ,
5861 OnlySingleValueParams onlySingleValueParams
5962 ) {
6063 this .searchExecutionContext = searchExecutionContext ;
64+ this .aliasFilter = aliasFilter ;
6165 this .field = field ;
6266 this .block = block ;
6367 this .onlySingleValueParams = onlySingleValueParams ;
@@ -74,7 +78,7 @@ int getPositionCount() {
7478 * Returns a copy of this query list that only returns queries for single-valued positions.
7579 * That is, it returns `null` queries for either multivalued or null positions.
7680 * <p>
77- * Whenever a multi-value position is encountered, whether in the input block or in the queried index, a warning is emitted.
81+ * Whenever a multi-value position is encountered, whether in the input block or in the queried index, a warning is emitted.
7882 * </p>
7983 */
8084 public abstract QueryList onlySingleValues (Warnings warnings , String multiValueWarningMessage );
@@ -93,6 +97,17 @@ final Query getQuery(int position) {
9397
9498 Query query = doGetQuery (position , firstValueIndex , valueCount );
9599
100+ if (aliasFilter != null && aliasFilter != AliasFilter .EMPTY ) {
101+ BooleanQuery .Builder builder = new BooleanQuery .Builder ();
102+ builder .add (query , BooleanClause .Occur .FILTER );
103+ try {
104+ builder .add (aliasFilter .getQueryBuilder ().toQuery (searchExecutionContext ), BooleanClause .Occur .FILTER );
105+ query = builder .build ();
106+ } catch (IOException e ) {
107+ throw new UncheckedIOException ("Error while building query for alias filter" , e );
108+ }
109+ }
110+
96111 if (onlySingleValueParams != null ) {
97112 query = wrapSingleValueQuery (query );
98113 }
@@ -138,7 +153,12 @@ private Query wrapSingleValueQuery(Query query) {
138153 * using only the {@link ElementType} of the {@link Block} to determine the
139154 * query.
140155 */
141- public static QueryList rawTermQueryList (MappedFieldType field , SearchExecutionContext searchExecutionContext , Block block ) {
156+ public static QueryList rawTermQueryList (
157+ MappedFieldType field ,
158+ SearchExecutionContext searchExecutionContext ,
159+ AliasFilter aliasFilter ,
160+ Block block
161+ ) {
142162 IntFunction <Object > blockToJavaObject = switch (block .elementType ()) {
143163 case BOOLEAN -> {
144164 BooleanBlock booleanBlock = (BooleanBlock ) block ;
@@ -170,17 +190,22 @@ public static QueryList rawTermQueryList(MappedFieldType field, SearchExecutionC
170190 case AGGREGATE_METRIC_DOUBLE -> throw new IllegalArgumentException ("can't read values from [aggregate metric double] block" );
171191 case UNKNOWN -> throw new IllegalArgumentException ("can't read values from [" + block + "]" );
172192 };
173- return new TermQueryList (field , searchExecutionContext , block , null , blockToJavaObject );
193+ return new TermQueryList (field , searchExecutionContext , aliasFilter , block , null , blockToJavaObject );
174194 }
175195
176196 /**
177197 * Returns a list of term queries for the given field and the input block of
178198 * {@code ip} field values.
179199 */
180- public static QueryList ipTermQueryList (MappedFieldType field , SearchExecutionContext searchExecutionContext , BytesRefBlock block ) {
200+ public static QueryList ipTermQueryList (
201+ MappedFieldType field ,
202+ SearchExecutionContext searchExecutionContext ,
203+ AliasFilter aliasFilter ,
204+ BytesRefBlock block
205+ ) {
181206 BytesRef scratch = new BytesRef ();
182207 byte [] ipBytes = new byte [InetAddressPoint .BYTES ];
183- return new TermQueryList (field , searchExecutionContext , block , null , offset -> {
208+ return new TermQueryList (field , searchExecutionContext , aliasFilter , block , null , offset -> {
184209 final var bytes = block .getBytesRef (offset , scratch );
185210 if (ipBytes .length != bytes .length ) {
186211 // Lucene only support 16-byte IP addresses, even IPv4 is encoded in 16 bytes
@@ -195,10 +220,16 @@ public static QueryList ipTermQueryList(MappedFieldType field, SearchExecutionCo
195220 * Returns a list of term queries for the given field and the input block of
196221 * {@code date} field values.
197222 */
198- public static QueryList dateTermQueryList (MappedFieldType field , SearchExecutionContext searchExecutionContext , LongBlock block ) {
223+ public static QueryList dateTermQueryList (
224+ MappedFieldType field ,
225+ SearchExecutionContext searchExecutionContext ,
226+ AliasFilter aliasFilter ,
227+ LongBlock block
228+ ) {
199229 return new TermQueryList (
200230 field ,
201231 searchExecutionContext ,
232+ aliasFilter ,
202233 block ,
203234 null ,
204235 field instanceof RangeFieldMapper .RangeFieldType rangeFieldType
@@ -210,8 +241,13 @@ public static QueryList dateTermQueryList(MappedFieldType field, SearchExecution
210241 /**
211242 * Returns a list of geo_shape queries for the given field and the input block.
212243 */
213- public static QueryList geoShapeQueryList (MappedFieldType field , SearchExecutionContext searchExecutionContext , Block block ) {
214- return new GeoShapeQueryList (field , searchExecutionContext , block , null );
244+ public static QueryList geoShapeQueryList (
245+ MappedFieldType field ,
246+ SearchExecutionContext searchExecutionContext ,
247+ AliasFilter aliasFilter ,
248+ Block block
249+ ) {
250+ return new GeoShapeQueryList (field , searchExecutionContext , aliasFilter , block , null );
215251 }
216252
217253 private static class TermQueryList extends QueryList {
@@ -220,11 +256,12 @@ private static class TermQueryList extends QueryList {
220256 private TermQueryList (
221257 MappedFieldType field ,
222258 SearchExecutionContext searchExecutionContext ,
259+ AliasFilter aliasFilter ,
223260 Block block ,
224261 OnlySingleValueParams onlySingleValueParams ,
225262 IntFunction <Object > blockValueReader
226263 ) {
227- super (field , searchExecutionContext , block , onlySingleValueParams );
264+ super (field , searchExecutionContext , aliasFilter , block , onlySingleValueParams );
228265 this .blockValueReader = blockValueReader ;
229266 }
230267
@@ -233,6 +270,7 @@ public TermQueryList onlySingleValues(Warnings warnings, String multiValueWarnin
233270 return new TermQueryList (
234271 field ,
235272 searchExecutionContext ,
273+ aliasFilter ,
236274 block ,
237275 new OnlySingleValueParams (warnings , multiValueWarningMessage ),
238276 blockValueReader
@@ -264,10 +302,11 @@ private static class GeoShapeQueryList extends QueryList {
264302 private GeoShapeQueryList (
265303 MappedFieldType field ,
266304 SearchExecutionContext searchExecutionContext ,
305+ AliasFilter aliasFilter ,
267306 Block block ,
268307 OnlySingleValueParams onlySingleValueParams
269308 ) {
270- super (field , searchExecutionContext , block , onlySingleValueParams );
309+ super (field , searchExecutionContext , aliasFilter , block , onlySingleValueParams );
271310
272311 this .blockValueReader = blockToGeometry (block );
273312 this .shapeQuery = shapeQuery ();
@@ -278,6 +317,7 @@ public GeoShapeQueryList onlySingleValues(Warnings warnings, String multiValueWa
278317 return new GeoShapeQueryList (
279318 field ,
280319 searchExecutionContext ,
320+ aliasFilter ,
281321 block ,
282322 new OnlySingleValueParams (warnings , multiValueWarningMessage )
283323 );
0 commit comments