use of com.yahoo.prelude.query.AndItem in project vespa by vespa-engine.
the class QueryRewrite method extractAndNotRecursively.
private static CompositeItem extractAndNotRecursively(CompositeItem parent) {
for (int i = 0; i < parent.getItemCount(); i++) {
Item child = parent.getItem(i);
Item possibleNewChild = optimizeAndNot(child);
if (child != possibleNewChild) {
parent.setItem(i, possibleNewChild);
}
}
if (parent instanceof AndItem) {
return extractAndNot((AndItem) parent);
}
return parent;
}
use of com.yahoo.prelude.query.AndItem in project vespa by vespa-engine.
the class QueryRewrite method extractAndNot.
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;
}
use of com.yahoo.prelude.query.AndItem in project vespa by vespa-engine.
the class CJKSearcher method transform.
private Item transform(Item root) {
if (root instanceof PhraseItem) {
PhraseItem asPhrase = (PhraseItem) root;
if (asPhrase.isExplicit() || hasOverlappingTokens(asPhrase))
return root;
AndItem replacement = new AndItem();
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
// should never run, but hey... just convert and hope it's OK :)
replacement.addItem(item);
}
return replacement;
} else if (root instanceof PhraseSegmentItem) {
PhraseSegmentItem asSegment = (PhraseSegmentItem) root;
if (asSegment.isExplicit() || hasOverlappingTokens(asSegment))
return root;
else
return new AndSegmentItem(asSegment);
} else if (root instanceof SegmentItem) {
// avoid descending into AndSegmentItems and similar
return root;
} else if (root instanceof CompositeItem) {
for (ListIterator<Item> i = ((CompositeItem) root).getItemIterator(); i.hasNext(); ) {
Item item = i.next();
Item transformedItem = transform(item);
if (item != transformedItem) {
i.set(transformedItem);
}
}
return root;
}
return root;
}
use of com.yahoo.prelude.query.AndItem in project vespa by vespa-engine.
the class RangeQueryOptimizer method optimizeAnd.
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;
// each such op gets a different partial set: Cannot be optimized
if (intItem.getHitLimit() != 0)
continue;
// don't optimize searches for single numbers
if (intItem.getFromLimit().equals(intItem.getToLimit()))
continue;
// May match different values in each range
if (indexFacts.getIndex(intItem.getIndexName()).isMultivalue())
continue;
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;
}
use of com.yahoo.prelude.query.AndItem in project vespa by vespa-engine.
the class QueryTestCase method testEncodeQueryPacketWithLabelsConnectivityAndSignificance.
@Test
public void testEncodeQueryPacketWithLabelsConnectivityAndSignificance() {
Query query = new Query();
AndItem and = new AndItem();
WeightedSetItem taggable1 = new WeightedSetItem("field1");
taggable1.setLabel("foo");
WeightedSetItem taggable2 = new WeightedSetItem("field2");
taggable1.setLabel("bar");
and.addItem(taggable1);
and.addItem(taggable2);
WordItem word1 = new WordItem("word1", "field3");
word1.setSignificance(0.37);
WordItem word2 = new WordItem("word1", "field3");
word2.setSignificance(0.81);
word2.setConnectivity(word1, 0.15);
and.addItem(word1);
and.addItem(word2);
query.getModel().getQueryTree().setRoot(and);
query.prepare();
QueryPacket packet = QueryPacket.create(query);
byte[] encoded = packetToBytes(packet);
byte[] correctBuffer = new byte[] { 0, 0, 1, 16, 0, 0, 0, -38, 0, 0, 0, 0, 0, 16, 0, 6, 0, 10, ignored, ignored, ignored, ignored, 0, 0, 0x40, 0x03, 7, 'd', 'e', 'f', 'a', 'u', 'l', 't', 0, 0, 0, 1, 0, 0, 0, 4, 'r', 'a', 'n', 'k', 0, 0, 0, 5, 0, 0, 0, 18, 'v', 'e', 's', 'p', 'a', 46, 'l', 'a', 'b', 'e', 'l', 46, 'b', 'a', 'r', 46, 'i', 'd', 0, 0, 0, 1, '1', 0, 0, 0, 22, 'v', 'e', 's', 'p', 'a', 46, 't', 'e', 'r', 'm', 46, '4', 46, 'c', 'o', 'n', 'n', 'e', 'x', 'i', 't', 'y', 0, 0, 0, 1, '3', 0, 0, 0, 22, 'v', 'e', 's', 'p', 'a', 46, 't', 'e', 'r', 'm', 46, '4', 46, 'c', 'o', 'n', 'n', 'e', 'x', 'i', 't', 'y', 0, 0, 0, 4, '0', 46, '1', '5', 0, 0, 0, 25, 'v', 'e', 's', 'p', 'a', 46, 't', 'e', 'r', 'm', 46, '3', 46, 's', 'i', 'g', 'n', 'i', 'f', 'i', 'c', 'a', 'n', 'c', 'e', 0, 0, 0, 4, '0', 46, '3', '7', 0, 0, 0, 25, 'v', 'e', 's', 'p', 'a', 46, 't', 'e', 'r', 'm', 46, '4', 46, 's', 'i', 'g', 'n', 'i', 'f', 'i', 'c', 'a', 'n', 'c', 'e', 0, 0, 0, 4, '0', 46, '8', '1', 0, 0, 0, 5, 0, 0, 0, '4', 1, 4, 79, 1, 0, 6, 'f', 'i', 'e', 'l', 'd', '1', 79, 2, 0, 6, 'f', 'i', 'e', 'l', 'd', '2', 68, 3, 6, 'f', 'i', 'e', 'l', 'd', '3', 5, 'w', 'o', 'r', 'd', '1', 68, 4, 6, 'f', 'i', 'e', 'l', 'd', '3', 5, 'w', 'o', 'r', 'd', 49 };
assertEqualArrays(correctBuffer, encoded);
}
Aggregations