docs: @Override
    public MtasSpanQuery rewrite(IndexReader reader) throws IOException {
      if (items.size() == 1) {
        MtasSpanQuery singleQuery = items.get(0).getQuery();
        if (leftMaximum != 0 || rightMaximum != 0) {
          singleQuery = new MtasExpandSpanQuery(singleQuery, leftMinimum,
              leftMaximum, rightMinimum, rightMaximum);
        }
        return singleQuery.rewrite(reader);
      } else {
        MtasSpanSequenceItem newItem;
        MtasSpanSequenceItem previousNewItem = null;
        ArrayList<MtasSpanSequenceItem> newItems = new ArrayList<>(items.size());
        int newLeftMinimum = leftMinimum;
        int newLeftMaximum = leftMaximum;
        int newRightMinimum = rightMinimum;
        int newRightMaximum = rightMaximum;
        MtasSpanQuery newIgnoreClause = ignoreQuery != null
            ? ignoreQuery.rewrite(reader) : null;
        boolean actuallyRewritten = ignoreQuery != null
            ? !newIgnoreClause.equals(ignoreQuery) : false;
        for (int i = 0; i < items.size(); i++) {
          newItem = items.get(i).rewrite(reader);
          if (newItem.getQuery() instanceof MtasSpanMatchNoneQuery) {
            if (!newItem.isOptional()) {
              return new MtasSpanMatchNoneQuery(field);
            } else {
              actuallyRewritten = true;
            }
          } else {
            actuallyRewritten |= !items.get(i).equals(newItem);
            MtasSpanSequenceItem previousMergedItem = MtasSpanSequenceItem.merge(
                previousNewItem, newItem, ignoreQuery, maximumIgnoreLength);
            if (previousMergedItem != null) {
              newItems.set((newItems.size() - 1), previousMergedItem);
              actuallyRewritten = true;
            } else {
              newItems.add(newItem);
            }
            previousNewItem = newItem;
          }
        }
        // check first and last
        if (ignoreQuery == null) {
          ArrayList<MtasSpanSequenceItem> possibleTrimmedItems = new ArrayList<>(
              newItems.size());
          MtasSpanSequenceItem firstItem = newItems.get(0);
          MtasSpanQuery firstQuery = firstItem.getQuery();
          if (firstQuery instanceof MtasSpanMatchAllQuery) {
            newLeftMaximum++;
            if (!firstItem.isOptional()) {
              newLeftMinimum++;
            }
          } else if (firstQuery instanceof MtasSpanRecurrenceQuery) {
            MtasSpanRecurrenceQuery firstRecurrenceQuery = (MtasSpanRecurrenceQuery) firstQuery;
            if (firstRecurrenceQuery.getQuery() instanceof MtasSpanMatchAllQuery
                && firstRecurrenceQuery.getIgnoreQuery() == null) {
              if (!firstItem.isOptional()) {
                newLeftMinimum += firstRecurrenceQuery.getMinimumRecurrence();
                newLeftMaximum += firstRecurrenceQuery.getMaximumRecurrence();
              } else {
                if (firstRecurrenceQuery.getMinimumRecurrence() == 1
                    || firstRecurrenceQuery
                        .getMinimumRecurrence() <= newLeftMinimum) {
                  newLeftMinimum += 0;
                  newLeftMaximum += firstRecurrenceQuery.getMaximumRecurrence();
                } else {
                  possibleTrimmedItems.add(firstItem);
                }
              }
            } else {
              possibleTrimmedItems.add(firstItem);
            }
          } else {
            possibleTrimmedItems.add(firstItem);
          }
          for (int i = 1; i < (newItems.size() - 1); i++) {
            possibleTrimmedItems.add(newItems.get(i));
          }
          if (newItems.size() > 1) {
            MtasSpanSequenceItem lastItem = newItems.get((newItems.size() - 1));
            MtasSpanQuery lastQuery = lastItem.getQuery();
            if (lastQuery instanceof MtasSpanMatchAllQuery) {
              newRightMaximum++;
              if (!lastItem.isOptional()) {
                newRightMinimum++;
              }
            } else if (lastQuery instanceof MtasSpanRecurrenceQuery) {
              MtasSpanRecurrenceQuery lastRecurrenceQuery = (MtasSpanRecurrenceQuery) lastQuery;
              if (lastRecurrenceQuery.getQuery() instanceof MtasSpanMatchAllQuery
                  && lastRecurrenceQuery.getIgnoreQuery() == null) {
                if (!lastItem.isOptional()) {
                  newRightMinimum += lastRecurrenceQuery.getMinimumRecurrence();
                  newRightMaximum += lastRecurrenceQuery.getMaximumRecurrence();
                } else if (lastRecurrenceQuery.getMinimumRecurrence() == 1
                    || lastRecurrenceQuery
                        .getMinimumRecurrence() <= newRightMinimum) {
                  newRightMinimum += 0;
                  newRightMaximum += lastRecurrenceQuery.getMaximumRecurrence();
                } else {
                  possibleTrimmedItems.add(lastItem);
                }
              } else {
                possibleTrimmedItems.add(lastItem);
              }
            } else {
              possibleTrimmedItems.add(lastItem);
            }
          }
          if (possibleTrimmedItems.size() < newItems.size()) {
            actuallyRewritten = true;
            newItems = possibleTrimmedItems;
          }
        }
        if (!actuallyRewritten) {
          if (leftMaximum != 0 || rightMaximum != 0) {
            newLeftMinimum = leftMinimum;
            newLeftMaximum = leftMaximum;
            newRightMinimum = rightMinimum;
            newRightMaximum = rightMaximum;
            leftMinimum = 0;
            leftMaximum = 0;
            rightMinimum = 0;
            rightMaximum = 0;
            MtasSpanQuery finalQuery = new MtasExpandSpanQuery(this,
                newLeftMinimum, newLeftMaximum, newRightMinimum, newRightMaximum);
            return finalQuery.rewrite(reader);
          } else {
            return super.rewrite(reader);
          }
        } else {
          if (!newItems.isEmpty()) {
            return new MtasSpanSequenceQuery(newItems, newLeftMinimum,
                newLeftMaximum, newRightMinimum, newRightMaximum, newIgnoreClause,
                maximumIgnoreLength).rewrite(reader);
          } else {
            return new MtasSpanMatchNoneQuery(field);
          }
        }
      }
    }