use of com.yahoo.prelude.query.CompositeItem in project vespa by vespa-engine.
the class RewriterFeaturesTestCase method testConvertStringToQTree.
@Test
public final void testConvertStringToQTree() {
Execution placeholder = new Execution(Context.createContextStub());
SpecialTokenRegistry tokenRegistry = new SpecialTokenRegistry(new SpecialtokensConfig(new SpecialtokensConfig.Builder().tokenlist(new Tokenlist.Builder().name("default").tokens(new Tokens.Builder().token(ASCII_ELLIPSIS)))));
placeholder.context().setTokenRegistry(tokenRegistry);
Query query = new Query();
query.getModel().setExecution(placeholder);
Item parsed = RewriterFeatures.convertStringToQTree(query, "a b c " + ASCII_ELLIPSIS);
assertSame(AndItem.class, parsed.getClass());
assertEquals(4, ((CompositeItem) parsed).getItemCount());
assertEquals(ASCII_ELLIPSIS, ((CompositeItem) parsed).getItem(3).toString());
}
use of com.yahoo.prelude.query.CompositeItem 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.CompositeItem in project vespa by vespa-engine.
the class QueryRewrite method collapseSingleComposites.
private static Item collapseSingleComposites(Item item) {
if (!(item instanceof CompositeItem)) {
return item;
}
CompositeItem parent = (CompositeItem) item;
int numChildren = parent.getItemCount();
for (int i = 0; i < numChildren; ++i) {
Item oldChild = parent.getItem(i);
Item newChild = collapseSingleComposites(oldChild);
if (oldChild != newChild) {
parent.setItem(i, newChild);
}
}
return numChildren == 1 ? parent.getItem(0) : item;
}
use of com.yahoo.prelude.query.CompositeItem 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.CompositeItem 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;
}
Aggregations