/** * Creates the root of the query subtree which will contain the grams to match, * called by {@link #splitToGrams}. This hook is provided to make it easy to create a subclass which * matches grams using a different composite item, e.g an OrItem. * <p> * This default implementation return new AndItem(); * * @param query the input query, to make it possible to return a different composite item type * depending on the query content * @return the composite item to add the gram items to in {@link #splitToGrams} */ protected CompositeItem createGramRoot(Query query) { return new AndItem(); }
protected AndItem addAnd(Item item,AndItem and) { if (and==null) and=new AndItem(); and.addItem(item); return and; }
private void addHighlightItem(String key, Item value) { /*List<IndexedItem> l = highlightItems.get(key); if (l == null) { l = new ArrayList<IndexedItem>(); highlightItems.put(key, l); } l.addField(value);*/ AndItem item = highlightItems.get(key); if (item == null) { item = new AndItem(); highlightItems.put(key, item); } item.addItem(value); }
@NonNull private CompositeItem buildAnd(String key, Inspector value) { AndItem andItem = new AndItem(); addItemsFromInspector(andItem, value); return andItem; }
private CompositeItem createType(TermType termType) { if (termType==TermType.DEFAULT) { if (query.getModel().getType().equals(Query.Type.ANY)) return new OrItem(); else return new AndItem(); } else if (termType==TermType.AND) { return new AndItem(); } else if (termType==TermType.OR) { return new OrItem(); } else if (termType==TermType.RANK) { return new RankItem(); } else if (termType==TermType.NOT) { return new NotItem(); } throw new IllegalArgumentException("Programing error, this method should be updated with add in RankType"); }
/** * Convert segment items into their mutable counterpart, do not update query tree. * Non-segment items are returned directly. * * @return a mutable CompositeItem instance */ private CompositeItem convertSegmentItem(CompositeItem item) { if (!(item instanceof SegmentItem)) { return item; } CompositeItem converted = null; if (item instanceof AndSegmentItem) { converted = new AndItem(); } else if (item instanceof PhraseSegmentItem) { PhraseItem p = new PhraseItem(); PhraseSegmentItem old = (PhraseSegmentItem) item; p.setIndexName(old.getIndexName()); converted = p; } else { // TODO: Do something else than nothing for unknowns? return item; } for (Iterator<Item> i = item.getItemIterator(); i.hasNext();) { converted.addItem(i.next()); } return converted; }
private void addAndItem(QueryTree q, String term) { Item root = q.getRoot(); CompositeItem compositeRoot; if (root instanceof AndItem) { compositeRoot = (CompositeItem) root; } else { compositeRoot = new AndItem(); compositeRoot.addItem(root); q.setRoot(compositeRoot); } compositeRoot.addItem(new WordItem(term)); }
static public void andQueryItemWithRoot(QueryTree tree, Item item) { if (tree.isEmpty()) { tree.setRoot(item); } else { Item oldRoot = tree.getRoot(); if (oldRoot.getClass() == AndItem.class) { ((AndItem) oldRoot).addItem(item); } else { AndItem newRoot = new AndItem(); newRoot.addItem(oldRoot); newRoot.addItem(item); tree.setRoot(newRoot); } } }
if (asPhrase.isExplicit() || hasOverlappingTokens(asPhrase)) return root; AndItem replacement = new AndItem(); for (ListIterator<Item> i = ((CompositeItem) root).getItemIterator(); i.hasNext();) { Item item = i.next();
protected Item negativeItem() { int position = tokens.getPosition(); Item item = null; try { if (!tokens.skipMultiple(MINUS)) return null; if (tokens.currentIsNoIgnore(SPACE)) return null; item = indexableItem(); if (item == null) { item = compositeItem(); if (item != null) { if (item instanceof OrItem) { // Turn into And AndItem and = new AndItem(); for (Iterator<Item> i = ((OrItem) item).getItemIterator(); i.hasNext();) { and.addItem(i.next()); } item = and; } } } if (item!=null) item.setProtected(true); return item; } finally { if (item == null) { tokens.setPosition(position); } } }
@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; }
private Item addAndFilter(Item root, Item item) { if (item == null) { return root; } if (root instanceof AndItem) { ((AndItem) root).addItem(item); return root; } if (root instanceof RankItem) { Item firstChild = ((RankItem) root).getItem(0); if (firstChild instanceof AndItem) { ((AndItem) firstChild).addItem(item); return root; } else if (firstChild instanceof NotItem) { ((NotItem) firstChild).addPositiveItem(item); return root; } } AndItem and = new AndItem(); and.addItem(root); and.addItem(item); return and; }
AndItem and = new AndItem();
/** * 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 insertIncompatibleItem(Item item,CompositeItem parent,Query query,TermType desiredParentType) { // Create new parent CompositeItem newParent; if (desiredParentType==TermType.DEFAULT) newParent=new AndItem(); else newParent=(CompositeItem)desiredParentType.createItemClass(); // Save previous parent parent CompositeItem parentsParent=parent.getParent(); // Add items to new parent newParent.addItem(parent); newParent.addItem(item); // Insert new parent as root or child of old parents parent if (parentsParent==null) { query.getModel().getQueryTree().setRoot(newParent); } else { int parentIndex=0; if (parentsParent!=null) { parentIndex=parentsParent.getItemIndex(parent); } parentsParent.setItem(parentIndex,newParent); } }
/** 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); } }
if (or==null) or=addOr(and,or); and=new AndItem(); or.addItem(and);
expectingOperator = true; } else { AndItem and = new AndItem();
if (isTheWord("and", item)) { if (topLevelIsClosed || !(topLevelItem instanceof AndItem)) { AndItem and = new AndItem();