summaryrefslogtreecommitdiffstats
path: root/container-search/src
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@gmail.com>2022-01-20 12:36:34 +0100
committerJon Bratseth <bratseth@gmail.com>2022-01-20 12:36:34 +0100
commitc13776e481b5ad866c2cbec033dae91ef7a61d42 (patch)
treebc2b30c844d8a9797ee913f886af0de996560cab /container-search/src
parent593c35f1aed64636249fc4ed9e9dc6a1e30fa6ac (diff)
Cleanup
Diffstat (limited to 'container-search/src')
-rw-r--r--container-search/src/main/java/com/yahoo/prelude/query/EquivItem.java15
-rw-r--r--container-search/src/main/java/com/yahoo/prelude/query/TermType.java26
-rw-r--r--container-search/src/main/java/com/yahoo/prelude/semantics/engine/Evaluation.java59
-rw-r--r--container-search/src/test/java/com/yahoo/prelude/semantics/test/MatchOnlyIfNotOnlyTermTestCase.java4
-rw-r--r--container-search/src/test/java/com/yahoo/prelude/semantics/test/OrPhraseTestCase.java2
-rw-r--r--container-search/src/test/java/com/yahoo/prelude/semantics/test/SemanticSearcherTestCase.java4
6 files changed, 55 insertions, 55 deletions
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 97cbfac0b2d..500c68cf4bd 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
@@ -83,11 +83,16 @@ public class EquivItem extends CompositeTaggableItem {
super.adding(item);
Validator.ensure("Could not add an item of type " + item.getItemType() +
": Equiv can only have word, wordAlternatives, int, exact, or phrase as children",
- item.getItemType() == ItemType.WORD ||
- item.getItemType() == ItemType.WORD_ALTERNATIVES ||
- item.getItemType() == ItemType.INT ||
- item.getItemType() == ItemType.EXACT ||
- item.getItemType() == ItemType.PHRASE);
+ acceptsChildrenOfType(item.getItemType()));
+ }
+
+ /** Returns true if this accepts child items of the given type */
+ public static boolean acceptsChildrenOfType(ItemType itemType) {
+ return itemType == ItemType.WORD ||
+ itemType == ItemType.WORD_ALTERNATIVES ||
+ itemType == ItemType.INT ||
+ itemType == ItemType.EXACT ||
+ itemType == ItemType.PHRASE;
}
}
diff --git a/container-search/src/main/java/com/yahoo/prelude/query/TermType.java b/container-search/src/main/java/com/yahoo/prelude/query/TermType.java
index 309befd80f5..fbbc746b130 100644
--- a/container-search/src/main/java/com/yahoo/prelude/query/TermType.java
+++ b/container-search/src/main/java/com/yahoo/prelude/query/TermType.java
@@ -3,35 +3,41 @@ package com.yahoo.prelude.query;
/**
- * A term type enumeration
+ * A term type enumeration.
*
* @author bratseth
* @author Steinar Knutsen
*/
public class TermType {
- public static final TermType RANK = new TermType("rank", RankItem.class, null, "$");
+ public static final TermType RANK = new TermType("rank", Item.ItemType.RANK, RankItem.class, null, "$");
- public static final TermType AND = new TermType("and", AndItem.class, null, "+");
+ public static final TermType AND = new TermType("and", Item.ItemType.AND, AndItem.class, null, "+");
- public static final TermType OR = new TermType("or", OrItem.class, null, "?");
+ public static final TermType OR = new TermType("or", Item.ItemType.OR, OrItem.class, null, "?");
- public static final TermType NOT = new TermType("not", NotItem.class, null, "-");
+ public static final TermType NOT = new TermType("not", Item.ItemType.NOT, NotItem.class, null, "-");
- public static final TermType PHRASE = new TermType("phrase", PhraseItem.class, null, "\"");
+ public static final TermType PHRASE = new TermType("phrase", Item.ItemType.PHRASE, PhraseItem.class, null, "\"");
- public static final TermType EQUIV = new TermType("equiv", EquivItem.class, null, "");
+ public static final TermType EQUIV = new TermType("equiv", Item.ItemType.EQUIV, EquivItem.class, null, "");
- public static final TermType DEFAULT = new TermType("", CompositeItem.class, AndItem.class, "");
+ public static final TermType DEFAULT = new TermType("", Item.ItemType.AND, CompositeItem.class, AndItem.class, "");
public final String name;
+ private final Item.ItemType itemType;
private final String sign;
private final Class<? extends CompositeItem> instanceClass;
private final Class<? extends CompositeItem> itemClass;
- private TermType(String name, Class<? extends CompositeItem> itemClass, Class<? extends CompositeItem> instanceClass, String sign) {
+ private TermType(String name,
+ Item.ItemType itemType,
+ Class<? extends CompositeItem> itemClass,
+ Class<? extends CompositeItem> instanceClass,
+ String sign) {
this.name = name;
+ this.itemType = itemType;
this.itemClass = itemClass;
if (instanceClass == null) {
this.instanceClass = itemClass;
@@ -73,6 +79,8 @@ public class TermType {
return sign;
}
+ public Item.ItemType toItemType() { return itemType; }
+
@Override
public boolean equals(Object o) {
if ( ! (o instanceof TermType)) return false;
diff --git a/container-search/src/main/java/com/yahoo/prelude/semantics/engine/Evaluation.java b/container-search/src/main/java/com/yahoo/prelude/semantics/engine/Evaluation.java
index 7bfcc08f21b..d5ed89b0724 100644
--- a/container-search/src/main/java/com/yahoo/prelude/semantics/engine/Evaluation.java
+++ b/container-search/src/main/java/com/yahoo/prelude/semantics/engine/Evaluation.java
@@ -245,10 +245,10 @@ public class Evaluation {
/**
* Inserts an item to the query being evaluated in a way consistent with the query type
*
- * @param items the item to insert
- * @param parent the parent of this item, or null to set the root
- * @param index the index at which to insert this into the parent
- * @param desiredParentType the desired type of the composite which contains item when this returns
+ * @param items the items to insert
+ * @param parent the parent of these items, or null to set the root
+ * @param index the index at which to insert these into the parent
+ * @param desiredParentType the desired type of the composite which contains items when this returns
*/
public void insertItems(List<Item> items, CompositeItem parent, int index, TermType desiredParentType) {
if (isEmpty(parent)) {
@@ -282,17 +282,11 @@ public class Evaluation {
items.forEach(item -> newParent.addItem(item));
parentsParent.setItem(parentsParent.getItemIndex(parent), newParent);
}
- else if (items.size() ==1 && desiredParentType.hasItemClass(items.get(0).getClass())) { // This will never happen
- for (Item item : items)
- addItem(parent, index, item, desiredParentType);
- }
- else if (incompatible(desiredParentType, parent)) {
- for (Item item : items)
- insertIncompatibleItem(item, parent, query, desiredParentType);
+ else if (items.size() == 1 && desiredParentType.hasItemClass(items.get(0).getClass())) {
+ addItem(parent, index, items.get(0), desiredParentType);
}
else {
- for (Item item : items)
- insertIncompatibleItemAsParent(item, parent, query, desiredParentType);
+ insertWithDesiredParentType(items, parent, desiredParentType);
}
}
@@ -303,12 +297,6 @@ public class Evaluation {
return false;
}
- /** Returns true if the desired type cannot have childCandidate as a child */
- private boolean incompatible(TermType desiredParentType, CompositeItem parent) {
- return desiredParentType == TermType.EQUIV
- && (parent.getItemType() != Item.ItemType.EQUIV && parent.getItemType() != Item.ItemType.PHRASE);
- }
-
private void addItem(CompositeItem parent, int index, Item item, TermType desiredParentType) {
if (parent instanceof NotItem) {
if (index == 0 && parent.getItem(0) == null) { // Case 1: The current positive is null and we are adding a positive
@@ -352,31 +340,30 @@ public class Evaluation {
return true;
}
- private void insertIncompatibleItem(Item item, CompositeItem parent, Query query, TermType desiredParentType) {
- CompositeItem newParent = newParent(desiredParentType);
-
- newParent.addItem(item);
- parent.addItem(newParent);
- }
-
- private CompositeItem newParent(TermType desiredParentType) {
- return desiredParentType == TermType.DEFAULT ? new AndItem() : (CompositeItem)desiredParentType.createItemClass();
- }
-
- private void insertIncompatibleItemAsParent(Item item, CompositeItem parent, Query query, TermType desiredParentType) {
+ private void insertWithDesiredParentType(List<Item> items, CompositeItem parent, TermType desiredParentType) {
CompositeItem parentsParent = parent.getParent();
CompositeItem newParent = newParent(desiredParentType);
+
if (! (parentsParent instanceof QueryTree) && parentsParent.getItemType() == newParent.getItemType()) { // Collapse
newParent = parentsParent;
}
- // Add items to new parent
- newParent.addItem(parent);
- newParent.addItem(item);
+ for (Item item : items)
+ newParent.addItem(item);
- if (newParent != parentsParent) // Insert new parent as root or child of old parent's parent
- parentsParent.setItem(parentsParent.getItemIndex(parent), newParent);
+ if (desiredParentType == TermType.EQUIV || desiredParentType == TermType.PHRASE) { // insert new parent below the current
+ parent.addItem(newParent);
+ }
+ else { // insert new parent above the current
+ newParent.addItem(parent);
+ if (newParent != parentsParent) // Insert new parent as root or child of old parent's parent
+ parentsParent.setItem(parentsParent.getItemIndex(parent), newParent);
+ }
+ }
+
+ private CompositeItem newParent(TermType desiredParentType) {
+ return desiredParentType == TermType.DEFAULT ? new AndItem() : (CompositeItem)desiredParentType.createItemClass();
}
private Item combineItems(Item first, Item second, TermType termType) {
diff --git a/container-search/src/test/java/com/yahoo/prelude/semantics/test/MatchOnlyIfNotOnlyTermTestCase.java b/container-search/src/test/java/com/yahoo/prelude/semantics/test/MatchOnlyIfNotOnlyTermTestCase.java
index ca78cae70ea..c398f0ed99e 100644
--- a/container-search/src/test/java/com/yahoo/prelude/semantics/test/MatchOnlyIfNotOnlyTermTestCase.java
+++ b/container-search/src/test/java/com/yahoo/prelude/semantics/test/MatchOnlyIfNotOnlyTermTestCase.java
@@ -17,8 +17,8 @@ public class MatchOnlyIfNotOnlyTermTestCase extends RuleBaseAbstractTestCase {
@Test
public void testMatch() {
- assertSemantics("RANK (AND justin timberlake) showname:\"saturday night live\"!1000", "justin timberlake snl");
- assertSemantics("RANK (AND justin timberlake) showname:\"saturday night live\"!1000", "justin timberlake saturday night live");
+ assertSemantics("RANK showname:\"saturday night live\"!1000 (AND justin timberlake)", "justin timberlake snl");
+ assertSemantics("RANK showname:\"saturday night live\"!1000 (AND justin timberlake)", "justin timberlake saturday night live");
}
@Test
diff --git a/container-search/src/test/java/com/yahoo/prelude/semantics/test/OrPhraseTestCase.java b/container-search/src/test/java/com/yahoo/prelude/semantics/test/OrPhraseTestCase.java
index 3051dd77190..1d6eea8d7de 100644
--- a/container-search/src/test/java/com/yahoo/prelude/semantics/test/OrPhraseTestCase.java
+++ b/container-search/src/test/java/com/yahoo/prelude/semantics/test/OrPhraseTestCase.java
@@ -14,7 +14,7 @@ public class OrPhraseTestCase extends RuleBaseAbstractTestCase {
@Test
public void testReplacing1() {
- assertSemantics("OR (AND new york) title:\"software engineer\"","software engineer new york");
+ assertSemantics("OR title:\"software engineer\" (AND new york)","software engineer new york");
assertSemantics("title:\"software engineer\"","software engineer"); // Skip or when there is nothing else
}
diff --git a/container-search/src/test/java/com/yahoo/prelude/semantics/test/SemanticSearcherTestCase.java b/container-search/src/test/java/com/yahoo/prelude/semantics/test/SemanticSearcherTestCase.java
index 76b2d3991c1..bee65db4347 100644
--- a/container-search/src/test/java/com/yahoo/prelude/semantics/test/SemanticSearcherTestCase.java
+++ b/container-search/src/test/java/com/yahoo/prelude/semantics/test/SemanticSearcherTestCase.java
@@ -128,12 +128,12 @@ public class SemanticSearcherTestCase extends RuleBaseAbstractTestCase {
@Test
public void testTypeChange() {
- assertSemantics("RANK doors default:typechange","typechange doors");
+ assertSemantics("RANK default:typechange doors","typechange doors");
}
@Test
public void testTypeChangeWithSingularToPluralButNonReplaceWillNotSingularify() {
- assertSemantics("RANK door default:typechange","typechange door");
+ assertSemantics("RANK default:typechange door","typechange door");
}
@Test