((NotItem) root).addNegativeItem(item); return root; ((NotItem) firstChild).addNegativeItem(item); return root; } else { NotItem not = new NotItem(); not.addPositiveItem(rootAsRank.removeItem(0)); not.addNegativeItem(item); if (rootAsRank.getItemCount() == 0) { return not; NotItem not = new NotItem(); not.addPositiveItem(root); not.addNegativeItem(item); return not;
/** * Adds a negative item. Like addItem but skips the first position * (position 0) if it is not already set. */ public void addNegativeItem(Item negative) { if (getItemCount() == 0) { insertNullFirstItem(); } addItem(negative); }
private static CompositeItem extractAndNot(AndItem parent) { NotItem theOnlyNot = null; for (int i = 0; i < parent.getItemCount(); i++) { Item child = parent.getItem(i); if (child instanceof NotItem) { NotItem thisNot = (NotItem) child; parent.setItem(i, thisNot.getPositiveItem()); if (theOnlyNot == null) { theOnlyNot = thisNot; theOnlyNot.setPositiveItem(parent); } else { for (int j=1; j < thisNot.getItemCount(); j++) { theOnlyNot.addNegativeItem(thisNot.getItem(j)); } } } } return (theOnlyNot != null) ? theOnlyNot : parent; }
/** * Returns the positive item (the first subitem), * or null if no positive items has been added */ public Item getPositiveItem() { if (getItemCount() == 0) { return null; } return getItem(0); }
protected NotItem addNot(Item item,NotItem not) { if (not==null) not=new NotItem(); not.addNegativeItem(item); return not; }
if (item != null) { if (not == null) { not = new NotItem(); not.addPositiveItem(item); topLevelItem = combineItems(topLevelItem, not); } else { not.addPositiveItem(item); if (item != null) { if (not == null && item != null) { not = new NotItem(); not.addNegativeItem(item); topLevelItem = combineItems(topLevelItem, not); } else if (item != null) { not.addNegativeItem(item); if (not != null && not.getItemCount() == 1) { return not.removeItem(0); // The positive } else if (topLevelItem instanceof RankItem) { removeNot((RankItem) topLevelItem); return combineItems(topLevelItem, not.getPositiveItem()); if (not != null && not.getPositiveItem() == null) { not.addPositiveItem(getItemAsPositiveItem(topLevelItem, not)); return not; } else { // Only negatives - ignore them
/** * Sets the positive item (the first item) * * @return the old positive item, or null if there was no items */ public Item setPositiveItem(Item item) { Validator.ensureNotNull("Positive item of " + this, item); if (getItemCount() == 0) { addItem(item); return null; } else { return setItem(0, item); } }
@NonNull private CompositeItem buildAnd(OperatorNode<ExpressionOperator> ast) { AndItem andItem = new AndItem(); NotItem notItem = new NotItem(); convertVarArgsAnd(ast, 0, andItem, notItem); Preconditions .checkArgument(andItem.getItemCount() > 0, "Vespa does not support AND with no logically positive branches."); if (notItem.getItemCount() == 0) { return andItem; } if (andItem.getItemCount() == 1) { notItem.setPositiveItem(andItem.getItem(0)); } else { notItem.setPositiveItem(andItem); } return notItem; }
public Result search(Query query, Execution execution) { // Get tensor and read items from user profile Object userIdProperty = query.properties().get("user_id"); if (userIdProperty != null) { Hit userProfile = retrieveUserProfile(userIdProperty.toString(), execution); if (userProfile != null) { addUserProfileTensorToQuery(query, userProfile); NotItem notItem = new NotItem(); notItem.addItem(new IntItem(1, "has_user_item_cf")); for (String item : getReadItems(userProfile.getField("has_read_items"))){ notItem.addItem(new WordItem(item, "post_id")); } QueryTreeUtil.andQueryItemWithRoot(query, notItem); } } // Restrict to search in blog_posts query.getModel().setRestrict("blog_post"); // Rank blog posts using tensor rank profile if(query.properties().get("ranking") == null) { query.properties().set(new CompoundName("ranking"), "tensor"); } return execution.search(query); }
private static Recall optimizeNotItemByRestrict(NotItem item, String restrictParam) { // first item is the positive one if (optimizeByRestrict(item.getItem(0), restrictParam) == Recall.RECALLS_NOTHING) { return Recall.RECALLS_NOTHING; } // all the remaining items are negative ones for (int i = item.getItemCount(); --i >= 1; ) { Item child = item.getItem(i); switch (optimizeByRestrict(child, restrictParam)) { case RECALLS_EVERYTHING: return Recall.RECALLS_NOTHING; case RECALLS_NOTHING: item.removeItem(i); break; } } return Recall.UNKNOWN_RECALL; }
NotItem notItem=(NotItem)first; if (termType==TermType.NOT) { notItem.addNegativeItem(second); Item newPositive=combineItems(notItem.getPositiveItem(),second,termType); notItem.setPositiveItem(newPositive);
} else if (isTheWord("andnot", item)) { if (topLevelIsClosed || !(topLevelItem instanceof NotItem)) { NotItem not = new NotItem(); not.addPositiveItem(topLevelItem); return not;
@NonNull private CompositeItem buildNotAnd(String key, Inspector value) { NotItem notItem = new NotItem(); addItemsFromInspector(notItem, value); return notItem; }
public boolean removeItem(Item item) { int removedIndex = getItemIndex(item); boolean removed = super.removeItem(item); if (removed && removedIndex == 0) { insertNullFirstItem(); } return removed; }
/** * Convenience method for adding a positive item. * If a positive item is already present * the positive item becomes an AndItem with the items added */ public void addPositiveItem(Item item) { if (getPositiveItem() == null) { setPositiveItem(item); } else if (getPositiveItem() instanceof AndItem) { ((AndItem) getPositiveItem()).addItem(item); } else { AndItem positives = new AndItem(); positives.addItem(getPositiveItem()); positives.addItem(item); setPositiveItem(positives); } }
private void addItems(NotItem notItem, List<Object> children) { for (Object child : children) { TypeCheck.ensureInstanceOf(child, Item.class); notItem.addItem((Item) child); } }
/** Returns the number of actual *positive* terms in this */ @Override public int getTermCount() { Item positive = getPositiveItem(); return positive == null ? 0 : positive.getTermCount(); }
private void addNegativeItems(NotItem notItem, List<Object> children) { for (Object child: butFirst(children)) { TypeCheck.ensureInstanceOf(child, Item.class); notItem.addNegativeItem((Item) child); } }
/** Modifies this query to become the current query AND the given item */ // TODO: Make sure this is complete, unit test and make it public private void and(Item item) { if (isEmpty()) { setRoot(item); } else if (getRoot() instanceof NotItem && item instanceof NotItem) { throw new IllegalArgumentException("Can't AND two NOTs"); // TODO: Complete } else if (getRoot() instanceof NotItem){ NotItem notItem = (NotItem)getRoot(); notItem.addPositiveItem(item); } else if (item instanceof NotItem){ NotItem notItem = (NotItem)item; notItem.addPositiveItem(getRoot()); setRoot(notItem); } else { AndItem andItem = new AndItem(); andItem.addItem(getRoot()); andItem.addItem(item); setRoot(andItem); } }
public Item removeItem(int index) { Item removed = super.removeItem(index); if (index == 0) { // Don't make the first negative the positive insertNullFirstItem(); } return removed; }