Search in sources :

Example 31 with AndItem

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;
}
Also used : CompositeItem(com.yahoo.prelude.query.CompositeItem) NullItem(com.yahoo.prelude.query.NullItem) SimpleIndexedItem(com.yahoo.prelude.query.SimpleIndexedItem) OrItem(com.yahoo.prelude.query.OrItem) SubstringItem(com.yahoo.prelude.query.SubstringItem) Item(com.yahoo.prelude.query.Item) AndItem(com.yahoo.prelude.query.AndItem) RankItem(com.yahoo.prelude.query.RankItem) NotItem(com.yahoo.prelude.query.NotItem) NearItem(com.yahoo.prelude.query.NearItem) EquivItem(com.yahoo.prelude.query.EquivItem) AndItem(com.yahoo.prelude.query.AndItem)

Example 32 with AndItem

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;
}
Also used : CompositeItem(com.yahoo.prelude.query.CompositeItem) NullItem(com.yahoo.prelude.query.NullItem) SimpleIndexedItem(com.yahoo.prelude.query.SimpleIndexedItem) OrItem(com.yahoo.prelude.query.OrItem) SubstringItem(com.yahoo.prelude.query.SubstringItem) Item(com.yahoo.prelude.query.Item) AndItem(com.yahoo.prelude.query.AndItem) RankItem(com.yahoo.prelude.query.RankItem) NotItem(com.yahoo.prelude.query.NotItem) NearItem(com.yahoo.prelude.query.NearItem) EquivItem(com.yahoo.prelude.query.EquivItem) NotItem(com.yahoo.prelude.query.NotItem)

Example 33 with AndItem

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;
}
Also used : AndSegmentItem(com.yahoo.prelude.query.AndSegmentItem) SegmentItem(com.yahoo.prelude.query.SegmentItem) CompositeItem(com.yahoo.prelude.query.CompositeItem) PhraseSegmentItem(com.yahoo.prelude.query.PhraseSegmentItem) PhraseItem(com.yahoo.prelude.query.PhraseItem) Item(com.yahoo.prelude.query.Item) AndItem(com.yahoo.prelude.query.AndItem) WordItem(com.yahoo.prelude.query.WordItem) CompositeItem(com.yahoo.prelude.query.CompositeItem) AndItem(com.yahoo.prelude.query.AndItem) AndSegmentItem(com.yahoo.prelude.query.AndSegmentItem) WordItem(com.yahoo.prelude.query.WordItem) ListIterator(java.util.ListIterator) PhraseSegmentItem(com.yahoo.prelude.query.PhraseSegmentItem) PhraseItem(com.yahoo.prelude.query.PhraseItem) AndSegmentItem(com.yahoo.prelude.query.AndSegmentItem) SegmentItem(com.yahoo.prelude.query.SegmentItem) PhraseSegmentItem(com.yahoo.prelude.query.PhraseSegmentItem)

Example 34 with AndItem

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;
}
Also used : FalseItem(com.yahoo.prelude.query.FalseItem) IntItem(com.yahoo.prelude.query.IntItem) CompositeItem(com.yahoo.prelude.query.CompositeItem) Item(com.yahoo.prelude.query.Item) AndItem(com.yahoo.prelude.query.AndItem) IntItem(com.yahoo.prelude.query.IntItem) ArrayList(java.util.ArrayList)

Example 35 with AndItem

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);
}
Also used : Query(com.yahoo.search.Query) AndItem(com.yahoo.prelude.query.AndItem) WordItem(com.yahoo.prelude.query.WordItem) WeightedSetItem(com.yahoo.prelude.query.WeightedSetItem) QueryPacket(com.yahoo.fs4.QueryPacket) Test(org.junit.Test)

Aggregations

AndItem (com.yahoo.prelude.query.AndItem)50 Test (org.junit.Test)39 WordItem (com.yahoo.prelude.query.WordItem)36 Query (com.yahoo.search.Query)24 Item (com.yahoo.prelude.query.Item)13 CompositeItem (com.yahoo.prelude.query.CompositeItem)12 PhraseItem (com.yahoo.prelude.query.PhraseItem)12 OrItem (com.yahoo.prelude.query.OrItem)10 Execution (com.yahoo.search.searchchain.Execution)10 IntItem (com.yahoo.prelude.query.IntItem)9 NotItem (com.yahoo.prelude.query.NotItem)7 RankItem (com.yahoo.prelude.query.RankItem)7 SubstringItem (com.yahoo.prelude.query.SubstringItem)5 PhraseMatcher (com.yahoo.prelude.querytransform.PhraseMatcher)5 PhrasingSearcher (com.yahoo.prelude.querytransform.PhrasingSearcher)5 Searcher (com.yahoo.search.Searcher)5 PhraseSegmentItem (com.yahoo.prelude.query.PhraseSegmentItem)4 WeakAndItem (com.yahoo.prelude.query.WeakAndItem)4 Result (com.yahoo.search.Result)4 IndexFacts (com.yahoo.prelude.IndexFacts)3