docs: @Override
    public MtasSpanQuery rewrite(IndexReader reader) throws IOException {
      if (clauses.size() > 1) {
        // rewrite, count MtasSpanMatchAllQuery and check for
        // MtasSpanMatchNoneQuery
        MtasSpanQuery[] newClauses = new MtasSpanQuery[clauses.size()];
        MtasSpanQuery[] oldClauses = clauses
            .toArray(new MtasSpanQuery[clauses.size()]);
        int singlePositionQueries = 0;
        int matchAllSinglePositionQueries = 0;
        int matchNoneQueries = 0;
        boolean actuallyRewritten = false;
        for (int i = 0; i < oldClauses.length; i++) {
          newClauses[i] = oldClauses[i].rewrite(reader);
          actuallyRewritten |= !oldClauses[i].equals(newClauses[i]);
          if (newClauses[i] instanceof MtasSpanMatchNoneQuery) {
            matchNoneQueries++;
          } else if (newClauses[i].isSinglePositionQuery()) {
            singlePositionQueries++;
            if (newClauses[i] instanceof MtasSpanMatchAllQuery) {
              matchAllSinglePositionQueries++;
            }
          }
        }
        // filter clauses
        if (matchNoneQueries > 0 || matchAllSinglePositionQueries > 0) {
          // compute new number of clauses
          int newNumber = newClauses.length - matchNoneQueries;
          if (matchAllSinglePositionQueries > 0) {
            newNumber -= singlePositionQueries - 1;
          }
          MtasSpanQuery[] newFilteredClauses = new MtasSpanQuery[newNumber];
          int j = 0;
          for (int i = 0; i < newClauses.length; i++) {
            if (!(newClauses[i] instanceof MtasSpanMatchNoneQuery)) {
              if (!newClauses[i].isSinglePositionQuery()
                  || matchAllSinglePositionQueries == 0) {
                newFilteredClauses[j] = newClauses[i];
                j++;
              } else if (singlePositionQueries > 0) {
                newFilteredClauses[j] = newClauses[i];
                j++;
                singlePositionQueries = 0; // only match this condition once
              }
            }
          }
          newClauses = newFilteredClauses;
        }
        if (newClauses.length == 0) {
          return (new MtasSpanMatchNoneQuery(this.getField())).rewrite(reader);
        } else if (newClauses.length == 1) {
          return newClauses[0].rewrite(reader);
        } else if (actuallyRewritten || newClauses.length != clauses.size()) {
          return new MtasSpanOrQuery(newClauses).rewrite(reader);
        } else {
          return super.rewrite(reader);
        }
      } else if (clauses.size() == 1) {
        return clauses.iterator().next().rewrite(reader);
      } else {
        return (new MtasSpanMatchNoneQuery(this.getField())).rewrite(reader);
      }
    }