From 1366eb01c678345c2879ea092ab8737d0f087a89 Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Thu, 6 Jan 2022 11:35:01 +0100 Subject: Default positive in NotItem to TRUE instead of null --- .../com/yahoo/prelude/query/CompositeItem.java | 25 +++++----------------- .../main/java/com/yahoo/prelude/query/NotItem.java | 23 +++++++++++--------- .../yahoo/prelude/query/QueryCanonicalizer.java | 5 ----- .../yahoo/prelude/query/parser/SimpleParser.java | 2 +- 4 files changed, 19 insertions(+), 36 deletions(-) (limited to 'container-search/src/main/java/com/yahoo/prelude/query') diff --git a/container-search/src/main/java/com/yahoo/prelude/query/CompositeItem.java b/container-search/src/main/java/com/yahoo/prelude/query/CompositeItem.java index aaa4d33c6dc..d3fbeb020f8 100644 --- a/container-search/src/main/java/com/yahoo/prelude/query/CompositeItem.java +++ b/container-search/src/main/java/com/yahoo/prelude/query/CompositeItem.java @@ -80,11 +80,6 @@ public abstract class CompositeItem extends Item { subitems.add(index, item); } - /** For NOT items, which may wish to insert nulls */ - void insertNullFirstItem() { - subitems.add(0, null); - } - /** * Returns a subitem * @@ -109,7 +104,7 @@ public abstract class CompositeItem extends Item { adding(item); Item old = subitems.set(index, item); - if (old!=item) + if (old != item) removing(old); return old; } @@ -188,9 +183,7 @@ public abstract class CompositeItem extends Item { return itemCount; } - /** - * Encodes just this item, not it's usual subitems, to the given buffer. - */ + /** Encodes just this item, not its regular subitems, to the given buffer. */ protected void encodeThis(ByteBuffer buffer) { super.encodeThis(buffer); IntegerCompressor.putCompressedPositiveNumber(encodingArity(), buffer); @@ -279,10 +272,7 @@ public abstract class CompositeItem extends Item { return code; } - /** - * Returns whether this item is of the same class and - * contains the same state as the given item - */ + /** Returns whether this item is of the same class and contains the same state as the given item. */ @Override public boolean equals(Object object) { if (!super.equals(object)) return false; @@ -303,17 +293,12 @@ public abstract class CompositeItem extends Item { @Override public int getTermCount() { int terms = 0; - for (Item item : subitems) { + for (Item item : subitems) terms += item.getTermCount(); - } return terms; } - /** - * Will return its single child if itself can safely be omitted. - * - * @return a valid Item or empty Optional if it can not be done - */ + /** Returns the single child of this, if this can be omitted without changes to recall semantics. */ public Optional extractSingleChild() { return getItemCount() == 1 ? Optional.of(getItem(0)) : Optional.empty(); } diff --git a/container-search/src/main/java/com/yahoo/prelude/query/NotItem.java b/container-search/src/main/java/com/yahoo/prelude/query/NotItem.java index 833b8635f61..2dc1d09e129 100644 --- a/container-search/src/main/java/com/yahoo/prelude/query/NotItem.java +++ b/container-search/src/main/java/com/yahoo/prelude/query/NotItem.java @@ -25,6 +25,7 @@ public class NotItem extends CompositeItem { } /** Adds an item. The first item is the positive, the rest are negative */ + @Override public void addItem(Item item) { super.addItem(item); } @@ -34,20 +35,18 @@ public class NotItem extends CompositeItem { * (position 0) if it is not already set. */ public void addNegativeItem(Item negative) { - if (getItemCount() == 0) { - insertNullFirstItem(); - } + if (getItemCount() == 0) + insertTrueFirstItem(); addItem(negative); } /** Returns the negative items of this: All child items except the first */ public List negativeItems() { return items().subList(1, getItemCount()); } - /** Returns the positive item (the first subitem), or null if no positive items has been added. */ + /** Returns the positive item (the first subitem), or TrueItem if no positive items has been added. */ public Item getPositiveItem() { - if (getItemCount() == 0) { - return null; - } + if (getItemCount() == 0) + return new TrueItem(); return getItem(0); } @@ -72,7 +71,7 @@ public class NotItem extends CompositeItem { * the positive item becomes an AndItem with the items added */ public void addPositiveItem(Item item) { - if (getPositiveItem() == null) { + if (getPositiveItem() instanceof TrueItem) { setPositiveItem(item); } else if (getPositiveItem() instanceof AndItem) { ((AndItem) getPositiveItem()).addItem(item); @@ -90,7 +89,7 @@ public class NotItem extends CompositeItem { boolean removed = super.removeItem(item); if (removed && removedIndex == 0) { - insertNullFirstItem(); + insertTrueFirstItem(); } return removed; } @@ -99,11 +98,15 @@ public class NotItem extends CompositeItem { Item removed = super.removeItem(index); if (index == 0) { // Don't make the first negative the positive - insertNullFirstItem(); + insertTrueFirstItem(); } return removed; } + private void insertTrueFirstItem() { + addItem(0, new TrueItem()); + } + /** Not items uses a empty heading instead of "NOT " */ protected void appendHeadingString(StringBuilder buffer) {} diff --git a/container-search/src/main/java/com/yahoo/prelude/query/QueryCanonicalizer.java b/container-search/src/main/java/com/yahoo/prelude/query/QueryCanonicalizer.java index 1f30833b3db..8c4c5c84a28 100644 --- a/container-search/src/main/java/com/yahoo/prelude/query/QueryCanonicalizer.java +++ b/container-search/src/main/java/com/yahoo/prelude/query/QueryCanonicalizer.java @@ -77,11 +77,6 @@ public class QueryCanonicalizer { else if (composite instanceof RankItem) { makeDuplicatesCheap((RankItem)composite); } - else if (composite instanceof NotItem) { - if (((NotItem) composite).getPositiveItem() == null) - return CanonicalizationResult.error("Can not search for only negative items"); - } - if (composite.getItemCount() == 0) parentIterator.remove(); diff --git a/container-search/src/main/java/com/yahoo/prelude/query/parser/SimpleParser.java b/container-search/src/main/java/com/yahoo/prelude/query/parser/SimpleParser.java index 087da13a937..020d93d951c 100644 --- a/container-search/src/main/java/com/yahoo/prelude/query/parser/SimpleParser.java +++ b/container-search/src/main/java/com/yahoo/prelude/query/parser/SimpleParser.java @@ -121,7 +121,7 @@ abstract class SimpleParser extends StructuredParser { return combineItems(topLevelItem, not.getPositiveItem()); } } - if (not != null && not.getPositiveItem() == null) { + if (not != null && not.getPositiveItem() instanceof TrueItem) { // Incomplete not, only negatives - if (topLevelItem != null && topLevelItem != not) { -- cgit v1.2.3