public void insertItems(List<Item> items, CompositeItem parent, int index, TermType desiredParentType, boolean replacing) {
        if (isEmpty(parent)) {
            if (items.size() == 1 && desiredParentType.hasItemClass(items.get(0).getClass())) {
                query.getModel().getQueryTree().setRoot(items.get(0));
            }
            else {
                CompositeItem newParent = (CompositeItem) desiredParentType.createItemClass();
                items.forEach(item -> newParent.addItem(item));
                query.getModel().getQueryTree().setRoot(newParent);
            }
            return;
        }

        if (parent.getItemCount() > 0 && parent instanceof QueryTree && parent.getItem(0) instanceof CompositeItem) {
            // combine with the existing root instead
            parent = (CompositeItem)parent.getItem(0);
            if (index == 1) { // that means adding it after the existing root
                index = parent.getItemCount();
            }
        }

        if (( desiredParentType == TermType.DEFAULT || desiredParentType.hasItemClass(parent.getClass()) )
             && equalIndexNameIfParentIsPhrase(items, parent)) {
            for (Item item : items)
                addItem(parent, index, item, desiredParentType);
        }
        else if (parent.items().isEmpty()) {
            CompositeItem parentsParent = parent.getParent();
            CompositeItem newParent = newParent(desiredParentType);
            items.forEach(item -> newParent.addItem(item));
            parentsParent.setItem(parentsParent.getItemIndex(parent), newParent);
        }
        else if (items.size() == 1 && desiredParentType.hasItemClass(items.get(0).getClass())) {
            addItem(parent, index, items.get(0), desiredParentType);
        }
        else {
            insertWithDesiredParentType(items, index, parent, desiredParentType, replacing);
        }
    }
Inserts an item to the query being evaluated in a way consistent with the query type

@param items the items to insert
@param parent the parent of these items, or null to set the root
@param index the index at which to insert these into the parent
@param desiredParentType the desired type of the composite which contains items when this returns
container-search/src/main/java/com/yahoo/prelude/semantics/engine/Evaluation.java