22
22
import org .apache .lucene .search .Query ;
23
23
import org .apache .lucene .search .Sort ;
24
24
import org .apache .lucene .search .SortField ;
25
- import org .apache .lucene .search .join .ScoreMode ;
26
25
import org .apache .lucene .search .join .ToChildBlockJoinQuery ;
27
- import org .apache .lucene .search .join .ToParentBlockJoinQuery ;
28
26
import org .elasticsearch .common .ParseField ;
29
27
import org .elasticsearch .common .ParsingException ;
30
28
import org .elasticsearch .common .Strings ;
@@ -186,10 +184,21 @@ protected static Nested resolveNested(QueryShardContext context, String nestedPa
186
184
}
187
185
188
186
protected static Nested resolveNested (QueryShardContext context , NestedSortBuilder nestedSort ) throws IOException {
189
- return resolveNested (context , nestedSort , null );
187
+ final Query childQuery = resolveNestedQuery (context , nestedSort , null );
188
+ if (childQuery == null ) {
189
+ return null ;
190
+ }
191
+ final ObjectMapper objectMapper = context .nestedScope ().getObjectMapper ();
192
+ final Query parentQuery ;
193
+ if (objectMapper == null ) {
194
+ parentQuery = Queries .newNonNestedFilter (context .indexVersionCreated ());
195
+ } else {
196
+ parentQuery = objectMapper .nestedTypeFilter ();
197
+ }
198
+ return new Nested (context .bitsetFilter (parentQuery ), childQuery );
190
199
}
191
200
192
- private static Nested resolveNested (QueryShardContext context , NestedSortBuilder nestedSort , Nested nested ) throws IOException {
201
+ private static Query resolveNestedQuery (QueryShardContext context , NestedSortBuilder nestedSort , Query parentQuery ) throws IOException {
193
202
if (nestedSort == null || nestedSort .getPath () == null ) {
194
203
return null ;
195
204
}
@@ -207,23 +216,15 @@ private static Nested resolveNested(QueryShardContext context, NestedSortBuilder
207
216
if (!nestedObjectMapper .nested ().isNested ()) {
208
217
throw new QueryShardException (context , "[nested] nested object under path [" + nestedPath + "] is not of nested type" );
209
218
}
210
-
211
- // get our parent query which will determines our parent documents
212
- Query parentQuery ;
213
219
ObjectMapper objectMapper = context .nestedScope ().getObjectMapper ();
214
- if (objectMapper == null ) {
215
- parentQuery = Queries .newNonNestedFilter (context .indexVersionCreated ());
216
- } else {
217
- parentQuery = objectMapper .nestedTypeFilter ();
218
- }
219
220
220
221
// get our child query, potentially applying a users filter
221
222
Query childQuery ;
222
223
try {
223
224
context .nestedScope ().nextLevel (nestedObjectMapper );
224
225
if (nestedFilter != null ) {
225
226
assert nestedFilter == Rewriteable .rewrite (nestedFilter , context ) : "nested filter is not rewritten" ;
226
- if (nested == null ) {
227
+ if (parentQuery == null ) {
227
228
// this is for back-compat, original single level nested sorting never applied a nested type filter
228
229
childQuery = nestedFilter .toFilter (context );
229
230
} else {
@@ -237,27 +238,23 @@ private static Nested resolveNested(QueryShardContext context, NestedSortBuilder
237
238
}
238
239
239
240
// apply filters from the previous nested level
240
- if (nested != null ) {
241
- parentQuery = Queries .filtered (parentQuery ,
242
- new ToParentBlockJoinQuery (nested .getInnerQuery (), nested .getRootFilter (), ScoreMode .None ));
243
-
241
+ if (parentQuery != null ) {
244
242
if (objectMapper != null ) {
245
243
childQuery = Queries .filtered (childQuery ,
246
- new ToChildBlockJoinQuery (nested . getInnerQuery () , context .bitsetFilter (objectMapper .nestedTypeFilter ())));
244
+ new ToChildBlockJoinQuery (parentQuery , context .bitsetFilter (objectMapper .nestedTypeFilter ())));
247
245
}
248
246
}
249
247
250
248
// wrap up our parent and child and either process the next level of nesting or return
251
- final Nested innerNested = new Nested (context .bitsetFilter (parentQuery ), childQuery );
252
249
if (nestedNestedSort != null ) {
253
250
try {
254
251
context .nestedScope ().nextLevel (nestedObjectMapper );
255
- return resolveNested (context , nestedNestedSort , innerNested );
252
+ return resolveNestedQuery (context , nestedNestedSort , childQuery );
256
253
} finally {
257
254
context .nestedScope ().previousLevel ();
258
255
}
259
256
} else {
260
- return innerNested ;
257
+ return childQuery ;
261
258
}
262
259
}
263
260
0 commit comments