diff options
author | Jon Bratseth <bratseth@gmail.com> | 2022-01-20 12:36:34 +0100 |
---|---|---|
committer | Jon Bratseth <bratseth@gmail.com> | 2022-01-20 12:36:34 +0100 |
commit | c13776e481b5ad866c2cbec033dae91ef7a61d42 (patch) | |
tree | bc2b30c844d8a9797ee913f886af0de996560cab /container-search/src/main/java/com/yahoo/prelude | |
parent | 593c35f1aed64636249fc4ed9e9dc6a1e30fa6ac (diff) |
Cleanup
Diffstat (limited to 'container-search/src/main/java/com/yahoo/prelude')
3 files changed, 50 insertions, 50 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) { |