2222import org .apache .lucene .search .Query ;
2323import org .apache .lucene .search .Sort ;
2424import org .apache .lucene .search .SortField ;
25- import org .apache .lucene .search .join .ScoreMode ;
2625import org .apache .lucene .search .join .ToChildBlockJoinQuery ;
27- import org .apache .lucene .search .join .ToParentBlockJoinQuery ;
2826import org .elasticsearch .common .ParseField ;
2927import org .elasticsearch .common .ParsingException ;
3028import org .elasticsearch .common .Strings ;
@@ -186,10 +184,21 @@ protected static Nested resolveNested(QueryShardContext context, String nestedPa
186184 }
187185
188186 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 );
190199 }
191200
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 {
193202 if (nestedSort == null || nestedSort .getPath () == null ) {
194203 return null ;
195204 }
@@ -207,23 +216,15 @@ private static Nested resolveNested(QueryShardContext context, NestedSortBuilder
207216 if (!nestedObjectMapper .nested ().isNested ()) {
208217 throw new QueryShardException (context , "[nested] nested object under path [" + nestedPath + "] is not of nested type" );
209218 }
210-
211- // get our parent query which will determines our parent documents
212- Query parentQuery ;
213219 ObjectMapper objectMapper = context .nestedScope ().getObjectMapper ();
214- if (objectMapper == null ) {
215- parentQuery = Queries .newNonNestedFilter (context .indexVersionCreated ());
216- } else {
217- parentQuery = objectMapper .nestedTypeFilter ();
218- }
219220
220221 // get our child query, potentially applying a users filter
221222 Query childQuery ;
222223 try {
223224 context .nestedScope ().nextLevel (nestedObjectMapper );
224225 if (nestedFilter != null ) {
225226 assert nestedFilter == Rewriteable .rewrite (nestedFilter , context ) : "nested filter is not rewritten" ;
226- if (nested == null ) {
227+ if (parentQuery == null ) {
227228 // this is for back-compat, original single level nested sorting never applied a nested type filter
228229 childQuery = nestedFilter .toFilter (context );
229230 } else {
@@ -237,27 +238,23 @@ private static Nested resolveNested(QueryShardContext context, NestedSortBuilder
237238 }
238239
239240 // 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 ) {
244242 if (objectMapper != null ) {
245243 childQuery = Queries .filtered (childQuery ,
246- new ToChildBlockJoinQuery (nested . getInnerQuery () , context .bitsetFilter (objectMapper .nestedTypeFilter ())));
244+ new ToChildBlockJoinQuery (parentQuery , context .bitsetFilter (objectMapper .nestedTypeFilter ())));
247245 }
248246 }
249247
250248 // 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 );
252249 if (nestedNestedSort != null ) {
253250 try {
254251 context .nestedScope ().nextLevel (nestedObjectMapper );
255- return resolveNested (context , nestedNestedSort , innerNested );
252+ return resolveNestedQuery (context , nestedNestedSort , childQuery );
256253 } finally {
257254 context .nestedScope ().previousLevel ();
258255 }
259256 } else {
260- return innerNested ;
257+ return childQuery ;
261258 }
262259 }
263260
0 commit comments