code: @Override
     public Query rewrite(IndexReader reader) throws IOException
     {
        Query cQuery = contextQuery.rewrite(reader);
        // only try to compact if no position is specified
        if (position == LocationStepQueryNode.NONE)
        {
           if (cQuery instanceof DescendantSelfAxisQuery)
           {
              DescendantSelfAxisQuery dsaq = (DescendantSelfAxisQuery)cQuery;
              if (dsaq.subQueryMatchesAll())
              {
                 Query sub;
                 if (nameTest == null)
                 {
                    sub = new MatchAllDocsQuery(indexConfig);
                 }
                 else
                 {
                    sub = new NameQuery(nameTest, version, nsMappings);
                 }
                 return new DescendantSelfAxisQuery(dsaq.getContextQuery(), sub, dsaq.getMinLevels() + 1, indexConfig)
                    .rewrite(reader);
              }
           }
        }
  
        // if we get here we could not compact the query
        if (cQuery == contextQuery) // NOSONAR
        {
           return this;
        }
        else
        {
           return new ChildAxisQuery(itemMgr, cQuery, nameTest, position, version, nsMappings, indexConfig);
        }
     }
docs: {@inheritDoc}