diff options
5 files changed, 42 insertions, 41 deletions
diff --git a/container-search/src/main/java/com/yahoo/prelude/query/SameElementItem.java b/container-search/src/main/java/com/yahoo/prelude/query/SameElementItem.java index ac4e8b98b03..58bbcd7315c 100644 --- a/container-search/src/main/java/com/yahoo/prelude/query/SameElementItem.java +++ b/container-search/src/main/java/com/yahoo/prelude/query/SameElementItem.java @@ -50,6 +50,7 @@ public class SameElementItem extends NonReducibleCompositeItem { super.adding(item); //TODO See if we can require only SimpleIndexedItem instead of TermItem Validator.ensureInstanceOf("Child item", item, TermItem.class); + Validator.ensureNotInstanceOf("Child item", item, WordAlternativesItem.class); TermItem asTerm = (TermItem) item; Validator.ensureNonEmpty("Struct fieldname", asTerm.getIndexName()); Validator.ensureNonEmpty("Query term", asTerm.getIndexedString()); diff --git a/container-search/src/main/java/com/yahoo/prelude/query/WordAlternativesItem.java b/container-search/src/main/java/com/yahoo/prelude/query/WordAlternativesItem.java index 2c017410109..d2df2aa6c89 100644 --- a/container-search/src/main/java/com/yahoo/prelude/query/WordAlternativesItem.java +++ b/container-search/src/main/java/com/yahoo/prelude/query/WordAlternativesItem.java @@ -19,7 +19,6 @@ import com.yahoo.compress.IntegerCompressor; public class WordAlternativesItem extends TermItem { private List<Alternative> alternatives; - private int maxIndex; public static final class Alternative { @@ -48,7 +47,6 @@ public class WordAlternativesItem extends TermItem { public void setAlternatives(Collection<Alternative> terms) { this.alternatives = uniqueAlternatives(terms); - setMaxIndex(); } private static ImmutableList<Alternative> uniqueAlternatives(Collection<Alternative> terms) { @@ -67,27 +65,6 @@ public class WordAlternativesItem extends TermItem { return ImmutableList.copyOf(uniqueTerms); } - private void setMaxIndex() { - int maxIndex = 0; - int currentIndex = 0; - double maxScore = 0.0d; - boolean first = true; - for (Alternative val : this.alternatives) { - if (first) { - first = false; - maxIndex = 0; - maxScore = val.exactness; - } else { - if (val.exactness > maxScore) { - maxScore = val.exactness; - maxIndex = currentIndex; - } - } - ++currentIndex; - } - this.maxIndex = maxIndex; - } - @Override public String stringValue() { StringBuilder builder = new StringBuilder(); diff --git a/container-search/src/main/java/com/yahoo/search/query/parser/ParserEnvironment.java b/container-search/src/main/java/com/yahoo/search/query/parser/ParserEnvironment.java index 9e53f9d8ea9..94b9bf6ce65 100644 --- a/container-search/src/main/java/com/yahoo/search/query/parser/ParserEnvironment.java +++ b/container-search/src/main/java/com/yahoo/search/query/parser/ParserEnvironment.java @@ -4,7 +4,6 @@ package com.yahoo.search.query.parser; import com.yahoo.language.Linguistics; import com.yahoo.language.simple.SimpleLinguistics; import com.yahoo.prelude.IndexFacts; -import com.yahoo.prelude.query.parser.SpecialTokenRegistry; import com.yahoo.prelude.query.parser.SpecialTokens; import com.yahoo.search.Searcher; import com.yahoo.search.searchchain.Execution; diff --git a/container-search/src/test/java/com/yahoo/prelude/query/test/SameElementItemTestCase.java b/container-search/src/test/java/com/yahoo/prelude/query/test/SameElementItemTestCase.java index 1382c106ae3..bb3a775ccf2 100644 --- a/container-search/src/test/java/com/yahoo/prelude/query/test/SameElementItemTestCase.java +++ b/container-search/src/test/java/com/yahoo/prelude/query/test/SameElementItemTestCase.java @@ -5,10 +5,14 @@ import com.yahoo.prelude.query.AndItem; import com.yahoo.prelude.query.IntItem; import com.yahoo.prelude.query.Item; import com.yahoo.prelude.query.SameElementItem; +import com.yahoo.prelude.query.Substring; import com.yahoo.prelude.query.TermItem; +import com.yahoo.prelude.query.WordAlternativesItem; import com.yahoo.prelude.query.WordItem; import org.junit.Test; +import java.util.Collections; +import java.util.List; import java.util.Optional; import static org.junit.Assert.assertEquals; @@ -79,7 +83,7 @@ public class SameElementItemTestCase { } @Test - public void requireAllChildrenAreTermItems() { + public void requireNoChildrenAreWordAlternatives() { try { SameElementItem s = new SameElementItem("structa"); s.addItem(new AndItem()); @@ -91,6 +95,19 @@ public class SameElementItemTestCase { } } + @Test + public void requireAllChildrenAreTermItems() { + try { + SameElementItem s = new SameElementItem("structa"); + s.addItem(new WordAlternativesItem("test", true, new Substring("origin"), List.of(new WordAlternativesItem.Alternative("a", 0.3)))); + fail("Expected exception"); + } + catch (IllegalArgumentException e) { // Success + assertEquals("Child item WORD_ALTERNATIVES test:[ a(0.3) ] should NOT be an instance of class com.yahoo.prelude.query.WordAlternativesItem but is class com.yahoo.prelude.query.WordAlternativesItem", + e.getMessage()); + } + } + private void verifyExtractSingle(TermItem term) { String subFieldName = term.getIndexName(); SameElementItem s = new SameElementItem("structa"); diff --git a/vespajlib/src/main/java/com/yahoo/protect/Validator.java b/vespajlib/src/main/java/com/yahoo/protect/Validator.java index 49fe7716ba2..ee4a93c2f01 100644 --- a/vespajlib/src/main/java/com/yahoo/protect/Validator.java +++ b/vespajlib/src/main/java/com/yahoo/protect/Validator.java @@ -68,14 +68,10 @@ public abstract class Validator { * Throws an IllegalArgumentException if the first argument is not strictly * smaller than the second argument * - * @param smallDescription - * description of the smallest argument - * @param small - * the smallest argument - * @param largeDescription - * description of the largest argument - * @param large - * the largest argument + * @param smallDescription description of the smallest argument + * @param small the smallest argument + * @param largeDescription description of the largest argument + * @param large the largest argument */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static void ensureSmaller(String smallDescription, Comparable small, String largeDescription, Comparable large) { @@ -115,14 +111,10 @@ public abstract class Validator { /** * Ensures that an item is of a particular class * - * @param description - * a description of the item to be checked - * @param item - * the item to check the type of - * @param type - * the type the given item should be instanceof - * @throws IllegalArgumentException - * if the given item is not of the correct type + * @param description a description of the item to be checked + * @param item the item to check the type of + * @param type the type the given item should be instanceof + * @throws IllegalArgumentException if the given item is not of the correct type */ public static void ensureInstanceOf(String description, Object item, Class<?> type) { if ( ! type.isAssignableFrom(item.getClass())) { @@ -131,4 +123,19 @@ public abstract class Validator { } } + /** + * Ensures that an item is not of a particular class + * + * @param description a description of the item to be checked + * @param item the item to check the type of + * @param type the type the given item should NOT be instanceof + * @throws IllegalArgumentException if the given item is of the wrong type + */ + public static void ensureNotInstanceOf(String description, Object item, Class<?> type) { + if ( type.isAssignableFrom(item.getClass())) { + throw new IllegalArgumentException(description + " " + item + " should NOT be an instance of " + type + + " but is " + item.getClass()); + } + } + } |