aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/tests/proton/matching/same_element_builder/same_element_builder_test.cpp
blob: c2aa2f255a843d24f157d3d640f85666e29b68bc (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/test_kit.h>

#include <vespa/searchcore/proton/matching/fakesearchcontext.h>
#include <vespa/searchcore/proton/matching/querynodes.h>
#include <vespa/searchcore/proton/matching/same_element_builder.h>
#include <vespa/searchcore/proton/matching/viewresolver.h>
#include <vespa/searchlib/fef/test/indexenvironment.h>
#include <vespa/searchlib/query/tree/location.h>
#include <vespa/searchlib/query/tree/range.h>
#include <vespa/searchlib/query/weight.h>
#include <vespa/searchlib/queryeval/fake_requestcontext.h>
#include <vespa/searchlib/queryeval/leaf_blueprints.h>
#include <vespa/searchlib/queryeval/same_element_blueprint.h>

using proton::matching::FakeSearchContext;
using proton::matching::ProtonLocationTerm;
using proton::matching::ProtonNumberTerm;
using proton::matching::ProtonPrefixTerm;
using proton::matching::ProtonRangeTerm;
using proton::matching::ProtonRegExpTerm;
using proton::matching::ProtonStringTerm;
using proton::matching::ProtonSubstringTerm;
using proton::matching::ProtonSuffixTerm;
using proton::matching::SameElementBuilder;
using proton::matching::ViewResolver;
using search::fef::FieldInfo;
using search::fef::FieldType;
using search::fef::test::IndexEnvironment;
using search::query::Location;
using search::query::Range;
using search::query::Weight;
using search::queryeval::Blueprint;
using search::queryeval::EmptyBlueprint;
using search::queryeval::FakeBlueprint;
using search::queryeval::FakeRequestContext;
using search::queryeval::FieldSpec;
using search::queryeval::IntermediateBlueprint;
using search::queryeval::SameElementBlueprint;

using CollectionType = FieldInfo::CollectionType;

struct FakeTerms {
    ViewResolver        resolver;
    IndexEnvironment    idx_env;
    ProtonStringTerm    idx_string_term;
    ProtonStringTerm    attr_string_term;
    ProtonStringTerm    both_string_term;

    ProtonNumberTerm    idx_number_term;
    ProtonLocationTerm  idx_location_term;
    ProtonPrefixTerm    idx_prefix_term;
    ProtonRangeTerm     attr_range_term;
    ProtonSubstringTerm attr_substring_term;
    ProtonSuffixTerm    attr_suffix_term;
    ProtonRegExpTerm    attr_regexp_term;

    FakeTerms()
        : resolver(),
          idx_env(),
          idx_string_term("term", "idx", 1, Weight(1)),
          attr_string_term("term", "attr", 2, Weight(1)),
          both_string_term("term", "both", 3, Weight(1)),
          idx_number_term("term", "idx", 4, Weight(1)),
          idx_location_term(Location(), "idx", 5, Weight(1)),
          idx_prefix_term("term", "idx", 6, Weight(1)),
          attr_range_term(Range(), "attr", 7, Weight(1)),
          attr_substring_term("term", "attr", 8, Weight(1)),
          attr_suffix_term("term", "attr", 9, Weight(1)),
          attr_regexp_term("term", "attr", 10, Weight(1))
    {
        resolver.add("both", "idx");
        resolver.add("both", "attr");
        idx_env.getFields().emplace_back(FieldType::INDEX, CollectionType::ARRAY, "idx", 1);
        idx_env.getFields().emplace_back(FieldType::ATTRIBUTE, CollectionType::ARRAY, "attr", 2);
        idx_string_term.resolve(resolver, idx_env);
        attr_string_term.resolve(resolver, idx_env);
        both_string_term.resolve(resolver, idx_env);
        idx_number_term.resolve(resolver, idx_env);
        idx_location_term.resolve(resolver, idx_env);
        idx_prefix_term.resolve(resolver, idx_env);
        attr_range_term.resolve(resolver, idx_env);
        attr_substring_term.resolve(resolver, idx_env);
        attr_suffix_term.resolve(resolver, idx_env);
        attr_regexp_term.resolve(resolver, idx_env);
    }
};

struct BuilderFixture {
    FakeRequestContext req_ctx;
    FakeSearchContext  ctx;
    FieldSpec field;
    SameElementBuilder builder;
    BuilderFixture() : req_ctx(), ctx(), field("foo", 3, 5), builder(req_ctx, ctx, field, false) {
        ctx.attr().tag("attr");
        ctx.addIdx(0).idx(0).getFake().tag("idx");
    }
};

const FakeBlueprint *as_fake(const Blueprint *bp) {
    const IntermediateBlueprint *parent = dynamic_cast<const IntermediateBlueprint*>(bp);
    if ((parent != nullptr) && (parent->childCnt() == 1)) {
        return as_fake(&parent->getChild(0));
    }
    return dynamic_cast<const FakeBlueprint*>(bp);    
}

void verify_blueprint(Blueprint *bp, std::initializer_list<const char *> tags) {
    SameElementBlueprint *se = dynamic_cast<SameElementBlueprint*>(bp);
    ASSERT_TRUE(se != nullptr);
    ASSERT_EQUAL(1u, se->getState().numFields());
    const auto &field = se->getState().field(0);
    EXPECT_EQUAL(3u, field.getFieldId());
    EXPECT_EQUAL(5u, field.getHandle());
    EXPECT_FALSE(field.isFilter());
    ASSERT_EQUAL(se->terms().size(), tags.size());
    size_t idx = 0;
    for (const char *tag: tags) {
        const FakeBlueprint *fake = as_fake(se->terms()[idx++].get());
        ASSERT_TRUE(fake != nullptr);
        EXPECT_EQUAL(fake->tag(), tag);
    }
}

TEST_FF("require that same element blueprint can be built", BuilderFixture(), FakeTerms()) {
    f1.builder.add_child(f2.idx_string_term);
    f1.builder.add_child(f2.attr_string_term);
    Blueprint::UP result = f1.builder.build();
    TEST_DO(verify_blueprint(result.get(), {"idx", "attr"}));
}

TEST_FF("require that terms searching multiple fields are ignored", BuilderFixture(), FakeTerms()) {
    f1.builder.add_child(f2.idx_string_term);
    f1.builder.add_child(f2.attr_string_term);
    f1.builder.add_child(f2.both_string_term); // ignored
    Blueprint::UP result = f1.builder.build();
    TEST_DO(verify_blueprint(result.get(), {"idx", "attr"}));
}

TEST_FF("require that all relevant term types can be used", BuilderFixture(), FakeTerms()) {
    f1.builder.add_child(f2.idx_string_term);
    f1.builder.add_child(f2.idx_number_term);
    f1.builder.add_child(f2.idx_location_term);
    f1.builder.add_child(f2.idx_prefix_term);
    f1.builder.add_child(f2.attr_range_term);
    f1.builder.add_child(f2.attr_substring_term);
    f1.builder.add_child(f2.attr_suffix_term);
    f1.builder.add_child(f2.attr_regexp_term);
    Blueprint::UP result = f1.builder.build();
    TEST_DO(verify_blueprint(result.get(), {"idx", "idx", "idx", "idx", "attr", "attr", "attr", "attr"}));
}

TEST_F("require that building same element with no children gives EmptyBlueprint", BuilderFixture()) {
    Blueprint::UP result = f1.builder.build();
    EXPECT_TRUE(dynamic_cast<EmptyBlueprint*>(result.get()) != nullptr);
}

TEST_MAIN() { TEST_RUN_ALL(); }