private void removeNonreferencedMatches(RuleEvaluation e) {
        int itemCount = e.getEvaluation().getQuerySize();

        // Remove items backwards to ease index handling
        for (int i = e.getNonreferencedMatchCount() - 1; i >= 0; i--) {
            // Ensure we don't produce an empty query
            if (getProduction().getTermCount() == 0 && itemCount == 1)
                break;
            itemCount--;

            Match match = e.getNonreferencedMatch(i);
            if (match.getItem().getParent() != null)
                match.getItem().getParent().removeItem(match.getPosition());
        }
    }
Remove items until there's only one item left
container-search/src/main/java/com/yahoo/prelude/semantics/rule/ReplacingProductionRule.java