From b2a36ec903172a7914ec8efd3bf70d5717196444 Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Thu, 4 Nov 2021 22:21:36 +0100 Subject: Disclose less --- .../main/java/com/yahoo/prelude/query/Item.java | 40 +++++++++++----------- .../yahoo/prelude/query/test/WandItemTestCase.java | 31 ++++++++++++----- 2 files changed, 43 insertions(+), 28 deletions(-) diff --git a/container-search/src/main/java/com/yahoo/prelude/query/Item.java b/container-search/src/main/java/com/yahoo/prelude/query/Item.java index 0ba107e6e8e..679e733a651 100644 --- a/container-search/src/main/java/com/yahoo/prelude/query/Item.java +++ b/container-search/src/main/java/com/yahoo/prelude/query/Item.java @@ -31,7 +31,7 @@ public abstract class Item implements Cloneable { * The definitions in Item.ItemType must match the ones in * searchlib/src/vespa/searchlib/parsequery/parse.h */ - public static enum ItemType { + public enum ItemType { OR(0), AND(1), NOT(2), @@ -122,10 +122,10 @@ public abstract class Item implements Cloneable { // Move this to an object which can take care of being a weighted bidirectional reference more elegantly and safely. protected Item connectedItem; protected Item connectedBacklink; - protected double connectivity; + protected double connectivity = 0; /** Explicit term significance */ - protected double significance; + protected double significance = 0; protected boolean explicitSignificance = false; /** Whether this item is eligible for change by query rewriters (false) or should be kept as-is (true) */ @@ -197,9 +197,7 @@ public abstract class Item implements Cloneable { annotations.put(key, value); } - /** - * Returns an annotation on this item, or null if the annotation is not set - */ + /** Returns an annotation on this item, or null if the annotation is not set */ public Object getAnnotation(String annotation) { if (annotations == null) { return null; @@ -273,7 +271,7 @@ public abstract class Item implements Cloneable { * Returns an integer that contains all feature flags for this item. This must be kept in sync with the flags * defined in searchlib/parsequery/parse.h. * - * @return The feature flags. + * @return the feature flags */ private byte getFlagsFeature() { byte FLAGS_NORANK = 0x01; @@ -313,17 +311,15 @@ public abstract class Item implements Cloneable { public abstract int getTermCount(); /** - *

Returns the canonical query language string of this item.

- * - *

The canonical language represent an item by the string + * Returns the canonical query language string of this item. + * The canonical language represent an item by the string *

      * ([itemName] [body])
      * 
* where the body may recursively be other items. * *

- * TODO: Change the output query language into a canonical form of the input - * query language + * TODO (Vespa 8?): Output YQL */ @Override public String toString() { @@ -350,7 +346,7 @@ public abstract class Item implements Cloneable { } /** - * Returns whether or not this item should be parethized when printed. + * Returns whether this item should be parethized when printed. * Default is false - no parentheses */ protected boolean shouldParenthize() { @@ -440,7 +436,7 @@ public abstract class Item implements Cloneable { } /** - * Sets whether or not this term item should affect ranking. + * Sets whether this term item should affect ranking. * If set to false this term is not exposed to the ranking framework in the search backend. */ public void setRanked(boolean isRanked) { @@ -453,7 +449,7 @@ public abstract class Item implements Cloneable { } /** - * Sets whether or not position data should be used when ranking this term item. + * Sets whether position data should be used when ranking this term item. * If set to false the search backend uses fast bit vector data structures when matching on this term * and only a few simple ranking features will be available when ranking this term. * Note that setting this to false also saves a lot of CPU during matching as bit vector data structures are used. @@ -462,20 +458,24 @@ public abstract class Item implements Cloneable { this.usePositionData = usePositionData; } - /** Returns whether or not position data should be used when ranking this item */ + /** Returns whether position data should be used when ranking this item */ public boolean usePositionData() { return usePositionData; } public void disclose(Discloser discloser) { - discloser.addProperty("connectivity", connectivity); - discloser.addProperty("connectedItem", connectedItem); // reference + if (connectivity != 0) + discloser.addProperty("connectivity", connectivity); + if (connectedItem != null) + discloser.addProperty("connectedItem", connectedItem); // reference discloser.addProperty("creator", creator); - discloser.addProperty("explicitSignificance", explicitSignificance); discloser.addProperty("isRanked", isRanked); discloser.addProperty("usePositionData", usePositionData); - discloser.addProperty("significance", significance); + + if (explicitSignificance) + discloser.addProperty("significance", significance); + discloser.addProperty("weight", weight); if (label != null) diff --git a/container-search/src/test/java/com/yahoo/prelude/query/test/WandItemTestCase.java b/container-search/src/test/java/com/yahoo/prelude/query/test/WandItemTestCase.java index 49621c3ef8a..2c06dd0034e 100644 --- a/container-search/src/test/java/com/yahoo/prelude/query/test/WandItemTestCase.java +++ b/container-search/src/test/java/com/yahoo/prelude/query/test/WandItemTestCase.java @@ -5,6 +5,7 @@ import com.yahoo.prelude.query.Item; import com.yahoo.prelude.query.PureWeightedString; import com.yahoo.prelude.query.WandItem; import com.yahoo.prelude.query.textualrepresentation.Discloser; +import com.yahoo.prelude.query.textualrepresentation.TextualQueryRepresentation; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -20,14 +21,6 @@ public class WandItemTestCase { private static final double DELTA = 0.0000001; - private static WandItem createSimpleItem() { - WandItem item = new WandItem("myfield", 10); - item.addToken("foo", 30); - item.setScoreThreshold(20); - item.setThresholdBoostFactor(2.0); - return item; - } - @Test public void requireThatWandItemCanBeConstructed() { WandItem item = new WandItem("myfield", 10); @@ -82,4 +75,26 @@ public class WandItemTestCase { assertEquals("myfield", discloser.props.get("index")); } + @Test + public void testTextualRepresentation() { + WandItem item = new WandItem("myfield", 10); + item.addToken("term1", 10); + item.setScoreThreshold(20); + item.setThresholdBoostFactor(2.0); + assertEquals("WAND[creator=ORIG index=\"myfield\" isRanked=true scoreThreshold=20.0 targetNumHits=10 thresholdBoostFactor=2.0 usePositionData=true weight=100]{\n" + + " PURE_WEIGHTED_STRING[weight=10]{\n" + + " \"term1\"\n" + + " }\n" + + "}\n", + new TextualQueryRepresentation(item).toString()); + } + + private static WandItem createSimpleItem() { + WandItem item = new WandItem("myfield", 10); + item.addToken("foo", 30); + item.setScoreThreshold(20); + item.setThresholdBoostFactor(2.0); + return item; + } + } -- cgit v1.2.3 From 945c970b449a2010cbd630cd7e63485525f67eed Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Thu, 4 Nov 2021 22:30:26 +0100 Subject: Disclose less --- .../src/main/java/com/yahoo/prelude/query/Item.java | 20 ++++++++++---------- .../yahoo/prelude/query/test/WandItemTestCase.java | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/container-search/src/main/java/com/yahoo/prelude/query/Item.java b/container-search/src/main/java/com/yahoo/prelude/query/Item.java index 679e733a651..670983f0cd7 100644 --- a/container-search/src/main/java/com/yahoo/prelude/query/Item.java +++ b/container-search/src/main/java/com/yahoo/prelude/query/Item.java @@ -102,10 +102,10 @@ public abstract class Item implements Cloneable { /** The annotations made on this item */ private CopyOnWriteHashMap annotations; - /** Whether or not this item should affect ranking. */ + /** Whether this item should affect ranking. */ private boolean isRanked = true; - /** Whether or not position data should be used when ranking this item */ + /** Whether position data should be used when ranking this item */ private boolean usePositionData = true; /** Whether the item should encode a unique ID */ @@ -468,16 +468,16 @@ public abstract class Item implements Cloneable { discloser.addProperty("connectivity", connectivity); if (connectedItem != null) discloser.addProperty("connectedItem", connectedItem); // reference - - discloser.addProperty("creator", creator); - discloser.addProperty("isRanked", isRanked); - discloser.addProperty("usePositionData", usePositionData); - + if (creator != ItemCreator.ORIG) + discloser.addProperty("creator", creator); + if ( ! isRanked) + discloser.addProperty("isRanked", isRanked); + if ( ! usePositionData) + discloser.addProperty("usePositionData", usePositionData); if (explicitSignificance) discloser.addProperty("significance", significance); - - discloser.addProperty("weight", weight); - + if (weight != 100) + discloser.addProperty("weight", weight); if (label != null) discloser.addProperty("label", label); if (hasUniqueID) diff --git a/container-search/src/test/java/com/yahoo/prelude/query/test/WandItemTestCase.java b/container-search/src/test/java/com/yahoo/prelude/query/test/WandItemTestCase.java index 2c06dd0034e..6a13ea130bc 100644 --- a/container-search/src/test/java/com/yahoo/prelude/query/test/WandItemTestCase.java +++ b/container-search/src/test/java/com/yahoo/prelude/query/test/WandItemTestCase.java @@ -81,7 +81,7 @@ public class WandItemTestCase { item.addToken("term1", 10); item.setScoreThreshold(20); item.setThresholdBoostFactor(2.0); - assertEquals("WAND[creator=ORIG index=\"myfield\" isRanked=true scoreThreshold=20.0 targetNumHits=10 thresholdBoostFactor=2.0 usePositionData=true weight=100]{\n" + + assertEquals("WAND[index=\"myfield\" scoreThreshold=20.0 targetNumHits=10 thresholdBoostFactor=2.0]{\n" + " PURE_WEIGHTED_STRING[weight=10]{\n" + " \"term1\"\n" + " }\n" + -- cgit v1.2.3