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); }
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; }
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); } } }
/** * 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); } }
for (ListIterator<Item> i = ((CompositeItem) root).getItemIterator(); i.hasNext();) { Item item = i.next(); if (item instanceof WordItem) replacement.addItem(item); else if (item instanceof PhraseSegmentItem) { replacement.addItem(new AndSegmentItem((PhraseSegmentItem) item)); else replacement.addItem(item); // should never run, but hey... just convert and hope it's OK :)
private boolean optimizeAnd(AndItem and, IndexFacts.Session indexFacts) { // Find consolidated ranges by collecting a list of compatible ranges List<FieldRange> fieldRanges = null; for (Iterator<Item> i = and.getItemIterator(); i.hasNext(); ) { Item item = i.next(); if ( ! (item instanceof IntItem)) continue; IntItem intItem = (IntItem)item; if (intItem.getHitLimit() != 0) continue; // each such op gets a different partial set: Cannot be optimized if (intItem.getFromLimit().equals(intItem.getToLimit())) continue; // don't optimize searches for single numbers if (indexFacts.getIndex(intItem.getIndexName()).isMultivalue()) continue; // May match different values in each range if (fieldRanges == null) fieldRanges = new ArrayList<>(); Optional<FieldRange> compatibleRange = findCompatibleRange(intItem, fieldRanges); if (compatibleRange.isPresent()) compatibleRange.get().addRange(intItem); else fieldRanges.add(new FieldRange(intItem)); i.remove(); } // Add consolidated ranges if (fieldRanges == null) return false; boolean optimized = false; for (FieldRange fieldRange : fieldRanges) { and.addItem(fieldRange.toItem()); optimized |= fieldRange.isOptimization(); } return optimized; }
/** 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); } }
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); } } }
private void convertVarArgsAnd(OperatorNode<ExpressionOperator> ast, int argIdx, AndItem outAnd, NotItem outNot) { Iterable<OperatorNode<ExpressionOperator>> args = ast.getArgument(argIdx); for (OperatorNode<ExpressionOperator> arg : args) { assertHasOperator(arg, ExpressionOperator.class); if (arg.getOperator() == ExpressionOperator.NOT) { OperatorNode<ExpressionOperator> exp = arg.getArgument(0); assertHasOperator(exp, ExpressionOperator.class); outNot.addNegativeItem(convertExpression(exp)); } else { outAnd.addItem(convertExpression(arg)); } } }
and.addItem(i.next());
AndItem and = new AndItem(); and.addItem(topLevelItem); return and;