private boolean optimizeAnd(AndItem and, IndexFacts.Session indexFacts) {
        // Find consolidated ranges by collecting a list of compatible ranges
        List<FieldRange> fieldRanges = null;
        for (Iterator<Item> i = and.getItemIterator(); i.hasNext(); ) {
            Item item = i.next();
            if ( ! (item instanceof IntItem intItem)) continue;
            if (intItem.getHitLimit() != 0) continue; // each such op gets a different partial set: Cannot be optimized
            if (intItem.getFromLimit().equals(intItem.getToLimit())) continue; // don't optimize searches for single numbers
            if (indexFacts.getIndex(intItem.getIndexName()).isMultivalue()) continue; // May match different values in each range

            if (fieldRanges == null) fieldRanges = new ArrayList<>();
            Optional<FieldRange> compatibleRange = findCompatibleRange(intItem, fieldRanges);
            if (compatibleRange.isPresent())
                compatibleRange.get().addRange(intItem);
            else
                fieldRanges.add(new FieldRange(intItem));
            i.remove();
        }

        // Add consolidated ranges
        if (fieldRanges == null) return false;

        boolean optimized = false;
        for (FieldRange fieldRange : fieldRanges) {
            and.addItem(fieldRange.toItem());
            optimized |= fieldRange.isOptimization();
        }
        return optimized;
    }
Recursively performs the range optimization on this query tree and returns whether at least one optimization was done
container-search/src/main/java/com/yahoo/search/querytransform/RangeQueryOptimizer.java