diff options
Diffstat (limited to 'container-search/src/main/java/com/yahoo/prelude')
7 files changed, 38 insertions, 54 deletions
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 d3fbeb020f8..f48dc9a8630 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 @@ -42,13 +42,9 @@ public abstract class CompositeItem extends Item { } public void ensureNotInSubtree(CompositeItem item) { - for (Iterator<Item> i = item.getItemIterator(); i.hasNext();) { - Item possibleCycle = i.next(); - - if (this == possibleCycle) { + for (Item i = this; i != null; i = i.getParent()) { + if (i == item) { throw new IllegalArgumentException("Cannot add " + item + " to " + this + " as it would create a cycle"); - } else if (possibleCycle instanceof CompositeItem) { - ensureNotInSubtree((CompositeItem) possibleCycle); } } } @@ -205,7 +201,7 @@ public abstract class CompositeItem extends Item { } /** Composite items should be parenthized when not on the top level */ - protected boolean shouldParenthize() { + protected boolean shouldParenthesize() { return getParent()!= null && ! (getParent() instanceof QueryTree); } diff --git a/container-search/src/main/java/com/yahoo/prelude/query/EquivItem.java b/container-search/src/main/java/com/yahoo/prelude/query/EquivItem.java index 500c68cf4bd..43258db8963 100644 --- a/container-search/src/main/java/com/yahoo/prelude/query/EquivItem.java +++ b/container-search/src/main/java/com/yahoo/prelude/query/EquivItem.java @@ -20,7 +20,7 @@ public class EquivItem extends CompositeTaggableItem { public EquivItem() {} /** - * Creates an EQUIV with the given item as child. The new EQUIV will take connectivity, + * Creates an EQUIV with the given item as child. The new EQUIV will take connectivity, * significance and weight from the given item. * * @param item will be modified and added as a child @@ -48,7 +48,7 @@ public class EquivItem extends CompositeTaggableItem { // steal other item's weight: setWeight(item.getWeight()); - // we have now stolen all of the other item's unique id needs: + // we have now stolen all the other item's unique id needs: item.setHasUniqueID(false); } 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 92b321adb70..06fe07d3895 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 @@ -11,7 +11,6 @@ import com.yahoo.text.Utf8; import java.nio.ByteBuffer; import java.util.Objects; -import java.util.Optional; /** @@ -20,7 +19,7 @@ import java.util.Optional; * Items are in general mutable and not thread safe. * They can be deeply cloned by calling clone(). * Their identity is defined by their content - * (i.e the field value of two items decide if they are equal). + * (i.e. the field value of two items decide if they are equal). * * @author bratseth * @author havardpe @@ -60,7 +59,8 @@ public abstract class Item implements Cloneable { NEAREST_NEIGHBOR(26), GEO_LOCATION_TERM(27), TRUE(28), - FALSE(29); + FALSE(29), + MULTI_TERM(30); public final int code; @@ -233,36 +233,34 @@ public abstract class Item implements Cloneable { public abstract int encode(ByteBuffer buffer); protected void encodeThis(ByteBuffer buffer) { - int FEAT_SHIFT = 5; - int CODE_MASK = 0x1f; - int FEAT_MASK = 0xe0; - int FEAT_WEIGHT = 0x01; - int FEAT_UNIQUEID = 0x02; - int FEAT_FLAGS = 0x04; + byte CODE_MASK = 0b00011111; + byte FEAT_WEIGHT = 0b00100000; + byte FEAT_UNIQUEID = 0b01000000; + byte FEAT_FLAGS = -0b10000000; - int features = 0; + byte type = (byte) (getCode() & CODE_MASK); + if (type != getCode()) + throw new IllegalStateException("must increase number of bytes in serialization format for queries"); if (weight != DEFAULT_WEIGHT) { - features |= FEAT_WEIGHT; + type |= FEAT_WEIGHT; } if (hasUniqueID()) { - features |= FEAT_UNIQUEID; + type |= FEAT_UNIQUEID; } byte flags = getFlagsFeature(); if (flags != 0) { - features |= FEAT_FLAGS; + type |= FEAT_FLAGS; } - byte type = (byte)(((getCode() & CODE_MASK) - | ((features << FEAT_SHIFT) & FEAT_MASK)) & 0xff); buffer.put(type); - if ((features & FEAT_WEIGHT) != 0) { + if ((type & FEAT_WEIGHT) != 0) { IntegerCompressor.putCompressedNumber(weight, buffer); } - if ((features & FEAT_UNIQUEID) != 0) { + if ((type & FEAT_UNIQUEID) != 0) { IntegerCompressor.putCompressedPositiveNumber(uniqueID, buffer); } - if (flags != 0) { + if ((type & FEAT_FLAGS) != 0) { buffer.put(flags); } } @@ -297,7 +295,7 @@ public abstract class Item implements Cloneable { /** Utility method for turning a string into utf-8 bytes */ - protected static final byte[] getBytes(String string) { + protected static byte[] getBytes(String string) { return Utf8.toBytes(string); } public static void putString(String s, ByteBuffer buffer) { @@ -322,7 +320,7 @@ public abstract class Item implements Cloneable { public String toString() { StringBuilder buffer = new StringBuilder(); - if (shouldParenthize()) { + if (shouldParenthesize()) { buffer.append("("); } if (isFilter()) { @@ -330,7 +328,7 @@ public abstract class Item implements Cloneable { } appendHeadingString(buffer); appendBodyString(buffer); - if (shouldParenthize()) { + if (shouldParenthesize()) { buffer.append(")"); } @@ -343,10 +341,10 @@ public abstract class Item implements Cloneable { } /** - * Returns whether this item should be parethized when printed. + * Returns whether this item should be parenthesized when printed. * Default is false - no parentheses */ - protected boolean shouldParenthize() { + protected boolean shouldParenthesize() { return false; } @@ -376,7 +374,7 @@ public abstract class Item implements Cloneable { // note: connectedItem and connectedBacklink references are corrected in CompositeItem.clone() return clone; } catch (CloneNotSupportedException e) { - throw new RuntimeException("Someone made Item unclonable"); + throw new RuntimeException("Someone made Item uncloneable"); } } diff --git a/container-search/src/main/java/com/yahoo/prelude/query/PhraseItem.java b/container-search/src/main/java/com/yahoo/prelude/query/PhraseItem.java index fae282868f8..379dfd6bb30 100644 --- a/container-search/src/main/java/com/yahoo/prelude/query/PhraseItem.java +++ b/container-search/src/main/java/com/yahoo/prelude/query/PhraseItem.java @@ -202,7 +202,7 @@ public class PhraseItem extends CompositeIndexedItem { /** Returns false, no parenthezes for phrases */ @Override - protected boolean shouldParenthize() { + protected boolean shouldParenthesize() { return false; } diff --git a/container-search/src/main/java/com/yahoo/prelude/query/PhraseSegmentItem.java b/container-search/src/main/java/com/yahoo/prelude/query/PhraseSegmentItem.java index 16e22f6d482..2a97970ac4a 100644 --- a/container-search/src/main/java/com/yahoo/prelude/query/PhraseSegmentItem.java +++ b/container-search/src/main/java/com/yahoo/prelude/query/PhraseSegmentItem.java @@ -148,7 +148,7 @@ public class PhraseSegmentItem extends IndexedSegmentItem { /** Returns false, no parenthezes for phrases */ @Override - protected boolean shouldParenthize() { + protected boolean shouldParenthesize() { return false; } diff --git a/container-search/src/main/java/com/yahoo/prelude/query/TaggableItem.java b/container-search/src/main/java/com/yahoo/prelude/query/TaggableItem.java index f23f2088f1d..eb23570ec90 100644 --- a/container-search/src/main/java/com/yahoo/prelude/query/TaggableItem.java +++ b/container-search/src/main/java/com/yahoo/prelude/query/TaggableItem.java @@ -18,10 +18,10 @@ public interface TaggableItem { * This is used to influence ranking features taking proximity into account: nativeRank and a subset of the * fieldMatch features. * <p> - * By default consecutive query terms are 'somewhat' connected, meaning ranking features will be better in documents + * By default consecutive query terms are 'somewhat' connected, meaning ranking features will score higher in documents * where the terms are found close to each other. This effect can be increased or decreased by manipulating the * connectivity value. Typical use is to increase the connectivity between terms in the query that we believe are - * semantically connected. E.g in the query 'new york hotel', it is a good idea to increase the connectivity between + * semantically connected. E.g., in the query 'new york hotel', it is a good idea to increase the connectivity between * "new" and "york" to ensure that a document containing "List of hotels in New York" is ranked above one containing * "List of new hotels in York". * @@ -36,8 +36,8 @@ public interface TaggableItem { /** * Used for setting explicit term significance (in the tf/idf sense) to a single term or phrase, * relative to the rest of the query. - * This influences ranking features which take term significance into account and overrides the default - * partial corpus based term significance computation happening in the backend. + * This influences ranking features which take term significance into account, and overrides the default + * partial corpus based term significance computation in the backend. */ void setSignificance(double significance); boolean hasExplicitSignificance(); diff --git a/container-search/src/main/java/com/yahoo/prelude/query/WeightedSetItem.java b/container-search/src/main/java/com/yahoo/prelude/query/WeightedSetItem.java index a988753d699..a5903f24cbd 100644 --- a/container-search/src/main/java/com/yahoo/prelude/query/WeightedSetItem.java +++ b/container-search/src/main/java/com/yahoo/prelude/query/WeightedSetItem.java @@ -10,6 +10,8 @@ import java.util.Iterator; import java.util.Map; import java.util.Objects; +import static java.util.Objects.requireNonNullElse; + /** * A term which contains a weighted set. * @@ -30,20 +32,12 @@ public class WeightedSetItem extends SimpleTaggableItem { /** Creates an empty weighted set; note you must provide an index name up front */ public WeightedSetItem(String indexName) { - if (indexName == null) { - this.indexName = ""; - } else { - this.indexName = indexName; - } + this.indexName = requireNonNullElse(indexName, ""); set = new CopyOnWriteHashMap<>(1000); } public WeightedSetItem(String indexName, Map<Object, Integer> map) { - if (indexName == null) { - this.indexName = ""; - } else { - this.indexName = indexName; - } + this.indexName = requireNonNullElse(indexName, ""); set = new CopyOnWriteHashMap<>(map); } @@ -96,11 +90,7 @@ public class WeightedSetItem extends SimpleTaggableItem { @Override public void setIndexName(String index) { - if (index == null) { - this.indexName = ""; - } else { - this.indexName = index; - } + this.indexName = requireNonNullElse(index, ""); } public String getIndexName() { |