diff options
author | Jon Bratseth <bratseth@gmail.com> | 2022-01-06 17:44:59 +0100 |
---|---|---|
committer | Jon Bratseth <bratseth@gmail.com> | 2022-01-06 17:44:59 +0100 |
commit | 00ba52f4900ec8c78c5647f4d0e67499f86fd86d (patch) | |
tree | d219cf11d66febc0dc905464b33962c532ab3e5f /container-search | |
parent | e2e44dbccb30c3b9673518f3204699e137b81fcc (diff) |
Skip a TRUE positive in toString()
Diffstat (limited to 'container-search')
3 files changed, 21 insertions, 24 deletions
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 2dc1d09e129..6985ed0913c 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 @@ -7,11 +7,12 @@ import java.util.Objects; /** * A composite item where the first item is positive and the following - * items are negative items which should be excluded from the result. + * items are negative items where matches should exclude the document should from the result. + * The default positive item, if only negatives are added, is TrueItem: Meaning that all documents are matched + * except those matching the negative terms added. * * @author bratseth */ -// TODO: Handle nulls by creating nullItem or checking in encode/toString public class NotItem extends CompositeItem { @Override @@ -53,7 +54,7 @@ public class NotItem extends CompositeItem { /** * Sets the positive item (the first item) * - * @return the old positive item, or null if there was none + * @return the old positive item, or TrueItem if there was none */ public Item setPositiveItem(Item item) { Objects.requireNonNull(item, () -> "Positive item of " + this); @@ -111,26 +112,22 @@ public class NotItem extends CompositeItem { protected void appendHeadingString(StringBuilder buffer) {} /** - * Overridden to tolerate nulls and to append "+" + * Overridden to skip the positive TrueItem and (otherwise) append "+" * to the first item and "-" to the rest */ + @Override protected void appendBodyString(StringBuilder buffer) { - boolean isFirstItem = true; - - for (Iterator<Item> i = getItemIterator(); i.hasNext();) { - Item item = i.next(); - - if (isFirstItem) { - buffer.append("+"); - } else { - buffer.append(" -"); - } - if (item == null) { - buffer.append("(null)"); - } else { - buffer.append(item.toString()); - } - isFirstItem = false; + if (items().isEmpty()) return; + if (items().size() == 1) { + buffer.append(items().get(0)); + return; + } + for (int i = 0; i < items().size(); i++) { + if (i == 0 && items().get(i) instanceof TrueItem) continue; // skip positive true + + buffer.append(i == 0 ? "+" : "-").append(items().get(i)); + if ( i < items().size() - 1) + buffer.append(" "); } } diff --git a/container-search/src/test/java/com/yahoo/prelude/query/test/QueryCanonicalizerTestCase.java b/container-search/src/test/java/com/yahoo/prelude/query/test/QueryCanonicalizerTestCase.java index 17d5b4d23c3..11424cc7e4e 100644 --- a/container-search/src/test/java/com/yahoo/prelude/query/test/QueryCanonicalizerTestCase.java +++ b/container-search/src/test/java/com/yahoo/prelude/query/test/QueryCanonicalizerTestCase.java @@ -295,7 +295,7 @@ public class QueryCanonicalizerTestCase { NotItem root = new NotItem(); root.addNegativeItem(new WordItem("negative")); - assertCanonicalized("+TRUE -negative",null, root); + assertCanonicalized("-negative",null, root); } @Test @@ -306,7 +306,7 @@ public class QueryCanonicalizerTestCase { root.addItem(not); root.addItem(new WordItem("word")); not.addNegativeItem(new WordItem("negative")); - assertCanonicalized("AND (+TRUE -negative) word",null, root); + assertCanonicalized("AND (-negative) word",null, root); } @Test diff --git a/container-search/src/test/java/com/yahoo/search/yql/YqlParserTestCase.java b/container-search/src/test/java/com/yahoo/search/yql/YqlParserTestCase.java index 2a4980f6590..881101a7bda 100644 --- a/container-search/src/test/java/com/yahoo/search/yql/YqlParserTestCase.java +++ b/container-search/src/test/java/com/yahoo/search/yql/YqlParserTestCase.java @@ -195,13 +195,13 @@ public class YqlParserTestCase { @Test public void testSingleNot() { assertParse("select foo from bar where !(title contains \"saint\")", - "+TRUE -title:saint"); + "-title:saint"); } @Test public void testMultipleNot() { assertParse("select foo from bar where !(title contains \"saint\") AND !(title contains \"etienne\")", - "+TRUE -title:saint -title:etienne"); + "-title:saint -title:etienne"); } @Test |