summaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/queryeval/weak_and/weak_and_test.cpp
blob: 689f9f085d00e2e9c4b14635d9a6266cb7479b9d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/searchlib/queryeval/fake_search.h>
#include <vespa/searchlib/queryeval/wand/weak_and_search.h>
#include <vespa/searchlib/queryeval/simpleresult.h>
#include <vespa/searchlib/queryeval/simplesearch.h>
#include <vespa/searchlib/queryeval/test/eagerchild.h>
#include <vespa/searchlib/queryeval/test/leafspec.h>
#include <vespa/searchlib/queryeval/test/wandspec.h>
#define ENABLE_GTEST_MIGRATION
#include <vespa/searchlib/test/weightedchildrenverifiers.h>
#include <vespa/vespalib/gtest/gtest.h>

using namespace search::fef;
using namespace search::queryeval;
using namespace search::queryeval::test;

using History = SearchHistory;

namespace {

struct MyWandSpec : public WandSpec
{
    uint32_t n;

    MyWandSpec(uint32_t n_) : WandSpec(), n(n_) {}
    SearchIterator *create() {
        return new TrackedSearch("WAND", getHistory(), WeakAndSearch::create(getTerms(), n, true));
    }
};

struct SimpleWandFixture {
    MyWandSpec   spec;
    SimpleResult hits;
    SimpleWandFixture() : spec(2), hits() {
        spec.leaf(LeafSpec("foo").doc(1).doc(2).doc(3).doc(4).doc(5).doc(6));
        spec.leaf(LeafSpec("bar").doc(1).doc(3).doc(5));
        SearchIterator::UP search(spec.create());
        hits.search(*search);
    }
    ~SimpleWandFixture();
};

SimpleWandFixture::~SimpleWandFixture() = default;

struct AdvancedWandFixture {
    MyWandSpec   spec;
    SimpleResult hits;
    AdvancedWandFixture() : spec(100), hits() {
        spec.leaf(LeafSpec("1").doc(1).doc(11).doc(111));
        spec.leaf(LeafSpec("2").doc(2).doc(12).doc(112));
        spec.leaf(LeafSpec("3").doc(3).doc(13).doc(113));
        spec.leaf(LeafSpec("4").doc(4).doc(14).doc(114));
        spec.leaf(LeafSpec("5").doc(5).doc(15).doc(115));
        SearchIterator::UP search(spec.create());
        hits.search(*search);
    }
    ~AdvancedWandFixture();
};

AdvancedWandFixture::~AdvancedWandFixture() = default;

struct WeightOrder {
    bool operator()(const wand::Term &t1, const wand::Term &t2) const {
        return (t1.weight < t2.weight);
    }
};

} // namespace <unnamed>

TEST(WeakAndTest, require_that_wand_prunes_bad_hits_after_enough_good_ones_are_obtained)
{
    SimpleWandFixture f;
    EXPECT_EQ(SimpleResult().addHit(1).addHit(2).addHit(3).addHit(5), f.hits);
}

TEST(WeakAndTest, require_that_wand_uses_subsearches_as_expected)
{
    SimpleWandFixture f;
    EXPECT_EQ(History()
              .seek("WAND", 1).seek("bar", 1).step("bar", 1).step("WAND", 1)
              .unpack("WAND", 1).seek("foo", 1).step("foo", 1).unpack("bar", 1).unpack("foo", 1)
              .seek("WAND", 2).seek("bar", 2).step("bar", 3).seek("foo", 2).step("foo", 2).step("WAND", 2)
              .unpack("WAND", 2).unpack("foo", 2)
              .seek("WAND", 3).step("WAND", 3)
              .unpack("WAND", 3).seek("foo", 3).step("foo", 3).unpack("bar", 3).unpack("foo", 3)
              .seek("WAND", 4).seek("bar", 4).step("bar", 5).seek("foo", 5).step("foo", 5).step("WAND", 5)
              .unpack("WAND", 5).unpack("bar", 5).unpack("foo", 5)
              .seek("WAND", 6).seek("bar", 6).step("bar", search::endDocId).step("WAND", search::endDocId),
              f.spec.getHistory());
}

TEST(WeakAndTest, require_that_documents_are_considered_in_the_right_order)
{
    AdvancedWandFixture f;
    EXPECT_EQ(SimpleResult()
              .addHit(1).addHit(2).addHit(3).addHit(4).addHit(5)
              .addHit(11).addHit(12).addHit(13).addHit(14).addHit(15)
              .addHit(111).addHit(112).addHit(113).addHit(114).addHit(115), f.hits);
}

TEST(WeakAndTest, require_that_initial_docid_for_subsearches_are_taken_into_account)
{
    History history;
    wand::Terms terms;
    terms.push_back(wand::Term(new TrackedSearch("foo", history, new EagerChild(search::endDocId)), 100, 1));
    terms.push_back(wand::Term(new TrackedSearch("bar", history, new EagerChild(10)), 100, 2));
    SearchIterator::UP search(new TrackedSearch("WAND", history, WeakAndSearch::create(terms, 2, true)));
    SimpleResult hits;
    hits.search(*search);
    EXPECT_EQ(SimpleResult().addHit(10), hits);
    EXPECT_EQ(History().seek("WAND", 1).step("WAND", 10).unpack("WAND", 10).unpack("bar", 10)
              .seek("WAND", 11).seek("bar", 11).step("bar", search::endDocId).step("WAND", search::endDocId),
              history);
}

class IteratorChildrenVerifier : public search::test::IteratorChildrenVerifier {
private:
    SearchIterator::UP create(bool strict) const override {
        wand::Terms terms;
        for (size_t i = 0; i < _num_children; ++i) {
            terms.emplace_back(createIterator(_split_lists[i], strict).release(),
                               100, _split_lists[i].size());
        }
        return SearchIterator::UP(WeakAndSearch::create(terms, -1, strict));
    }
};

TEST(WeakAndTest, verify_search_iterator_conformance)
{
    IteratorChildrenVerifier verifier;
    verifier.verify();
}

GTEST_MAIN_RUN_ALL_TESTS()