private Item addNotFilter(Item root, Item item) {
        if (item == null) {
            return root;
        }

        if (root instanceof NotItem) {
            ((NotItem) root).addNegativeItem(item);
            return root;
        }

        if (root instanceof RankItem rootAsRank) {
            Item firstChild = rootAsRank.getItem(0);

            if (firstChild instanceof NotItem) {
                ((NotItem) firstChild).addNegativeItem(item);
                return root;
            } else {
                NotItem not = new NotItem();

                not.addPositiveItem(rootAsRank.removeItem(0));
                not.addNegativeItem(item);
                if (rootAsRank.getItemCount() == 0) {
                    return not;
                } else {
                    rootAsRank.addItem(0, not);
                    return root;
                }
            }
        }

        NotItem not = new NotItem();
        not.addPositiveItem(root);
        not.addNegativeItem(item);
        return not;
    }
Returns the top level item resulting from combining the given top
level item and the new item. This implements most of the weird transformation
rules of the parser.
container-search/src/main/java/com/yahoo/prelude/query/parser/AnyParser.java