aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/queryeval/filter_search/filter_search_test.cpp
blob: d40248336cb4a248b885ca1fd34e8afe9345e56d (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/searchlib/queryeval/simpleresult.h>
#include <vespa/searchlib/queryeval/emptysearch.h>
#include <vespa/searchlib/queryeval/full_search.h>
#include <vespa/searchlib/queryeval/blueprint.h>
#include <vespa/searchlib/queryeval/intermediate_blueprints.h>
#include <vespa/searchlib/queryeval/leaf_blueprints.h>
#include <vespa/searchlib/queryeval/isourceselector.h>
#include <vespa/searchlib/queryeval/simple_phrase_blueprint.h>
#include <vespa/searchlib/queryeval/equiv_blueprint.h>
#include <vespa/searchlib/queryeval/weighted_set_term_blueprint.h>
#include <vespa/searchlib/queryeval/dot_product_blueprint.h>
#include <vespa/searchlib/queryeval/same_element_blueprint.h>
#include <vespa/searchlib/queryeval/wand/parallel_weak_and_blueprint.h>
#include <vespa/searchlib/fef/matchdatalayout.h>
#include <vespa/vespalib/util/trinary.h>
#include <vespa/vespalib/util/require.h>
#include <vespa/vespalib/gtest/gtest.h>
#include <functional>
#include <typeindex>

namespace search::fef { class TermFieldMatchDataArray; }
namespace search::fef { class MatchData; }

using namespace search::queryeval;
using search::fef::MatchData;
using search::fef::MatchDataLayout;
using search::fef::TermFieldMatchDataArray;
using vespalib::Trinary;

using Constraint = Blueprint::FilterConstraint;
constexpr auto lower_bound = Constraint::LOWER_BOUND;
constexpr auto upper_bound = Constraint::UPPER_BOUND;

const uint32_t docid_limit = 100;

template <typename T>
concept FilterFactory = requires(const T &a, T &ma, InFlow in_flow, uint32_t my_docid_limit, bool strict, Constraint constraint) {
    ma.basic_plan(in_flow, my_docid_limit);
    { a.createFilterSearch(constraint) } -> std::same_as<std::unique_ptr<SearchIterator>>;
};

template <typename T>
concept ChildCollector = requires(T a, std::unique_ptr<Blueprint> bp) {
    a.addChild(std::move(bp));
};

// proxy class used to make various decorators for leaf blueprints
// may be used directly to add the use of a field to a leaf blueprint
struct LeafProxy : SimpleLeafBlueprint {
    std::unique_ptr<Blueprint> child;
    void init() {
        setParent(child->getParent());
        child->setParent(this);
    }
    LeafProxy(std::unique_ptr<Blueprint> child_in)
      : SimpleLeafBlueprint(), child(std::move(child_in)) { init(); }
    LeafProxy(FieldSpecBase field, std::unique_ptr<Blueprint> child_in)
      : SimpleLeafBlueprint(field), child(std::move(child_in)) { init(); }
    void each_node_post_order(const std::function<void(Blueprint&)> &f) override {
        child->each_node_post_order(f);
        f(*this);
    }
    FlowStats calculate_flow_stats(uint32_t my_docid_limit) const override {
        return child->calculate_flow_stats(my_docid_limit);
    }
    void sort(InFlow in_flow) override {
        strict(in_flow.strict());
        child->sort(in_flow);
    }
    SearchIteratorUP createLeafSearch(const TermFieldMatchDataArray &) const override { abort(); }
    SearchIteratorUP createFilterSearch(Constraint constraint) const override {
        return child->createFilterSearch(constraint);
    }
};

// check strictness and filter constraints when creating a filter search
struct CheckParamsProxy : LeafProxy {
    static bool current_strict;           // <- changed by test
    static Constraint current_constraint; // <- changed by test
    bool expect_forced_strict = false;
    bool expect_inherit_strict;
    bool expect_same_constraint;
    CheckParamsProxy(std::unique_ptr<Blueprint> child_in, bool expect_inherit_strict_in, bool expect_same_constraint_in)
      : LeafProxy(std::move(child_in)),
        expect_inherit_strict(expect_inherit_strict_in), expect_same_constraint(expect_same_constraint_in) {}
    CheckParamsProxy(std::unique_ptr<Blueprint> child_in)
      : LeafProxy(std::move(child_in)), expect_forced_strict(true), expect_inherit_strict(false), expect_same_constraint(true) {}
    SearchIteratorUP createFilterSearch(Constraint constraint) const override {
        if (expect_forced_strict) {
            EXPECT_EQ(strict(), true);
        } else {
            EXPECT_EQ(strict(), (current_strict && expect_inherit_strict));
        }
        EXPECT_EQ((constraint == current_constraint), expect_same_constraint);
        return child->createFilterSearch(constraint);
    }
};
bool CheckParamsProxy::current_strict = false;
Constraint CheckParamsProxy::current_constraint = lower_bound;

// check dropped blueprints (due to short-circuit)
struct CheckDroppedProxy : LeafProxy {
    mutable bool used;
    CheckDroppedProxy(std::unique_ptr<Blueprint> child_in)
      : LeafProxy(std::move(child_in)), used(false) {}
    SearchIteratorUP createFilterSearch(Constraint constraint) const override {
        used = true;
        return child->createFilterSearch(constraint);
    }
    ~CheckDroppedProxy() override {
        EXPECT_EQ(used, false);
    }
};

// need one of these to be able to create a SourceBlender
struct NullSelector : ISourceSelector {
    NullSelector() : ISourceSelector(7) {}
    void setSource(uint32_t, Source) override { abort(); }
    uint32_t getDocIdLimit() const override { abort(); }
    void compactLidSpace(uint32_t) override { abort(); }
    std::unique_ptr<sourceselector::Iterator> createIterator() const override { abort(); }
};

// make a simple result containing the given documents
SimpleResult make_result(const std::vector<uint32_t> &docs) {
    SimpleResult result;
    for (uint32_t doc: docs) {
        result.addHit(doc);
    }
    return result;
}

// make a simple result containing all the documents
SimpleResult make_full_result() {
    SimpleResult result;
    for (uint32_t docid = 1; docid < docid_limit; ++docid) {
        result.addHit(docid);
    }
    return result;
}

// make a simple result containing none of the documents
SimpleResult make_empty_result() {
    return SimpleResult();
}

// create a leaf blueprint that matches no documents
std::unique_ptr<Blueprint> empty() {
    return std::make_unique<EmptyBlueprint>();
}

// create a leaf blueprint that matches all documents
std::unique_ptr<Blueprint> full() {
    return std::make_unique<AlwaysTrueBlueprint>();
}

// create a leaf blueprint with the specified hits
std::unique_ptr<Blueprint> hits(const std::vector<uint32_t> &docs) {
    return std::make_unique<SimpleBlueprint>(make_result(docs));
}

// check filter creation parameters
std::unique_ptr<Blueprint> check(std::unique_ptr<Blueprint> child, bool expect_inherit_strict, bool expect_same_constraint) {
    return std::make_unique<CheckParamsProxy>(std::move(child), expect_inherit_strict, expect_same_constraint);
}

std::unique_ptr<Blueprint> check_forced(std::unique_ptr<Blueprint> child) {
    return std::make_unique<CheckParamsProxy>(std::move(child));
}

// check that create filter is not called
std::unique_ptr<Blueprint> dropped(std::unique_ptr<Blueprint> child) {
    return std::make_unique<CheckDroppedProxy>(std::move(child));
}

// Describes blueprint children with a list of simple factories that
// can later be used to create them.
struct Children {
    using Factory = std::function<Blueprint::UP()>;
    std::vector<Factory> list;
    Children() : list() {}
    size_t size() const { return list.size(); }
    Children &hits(const std::vector<uint32_t> &docs) {
        list.push_back([docs](){ return ::hits(docs); });
        return *this;
    }
    Children &full() {
        list.push_back([](){ return ::full(); });
        return *this;
    }
    Children &empty() {
        list.push_back([](){ return ::empty(); });
        return *this;
    }
    Children &check(bool expect_inherit_strict, bool expect_same_constraint) {
        Factory old_factory = list.back();
        list.back() = [=](){ return ::check(old_factory(), expect_inherit_strict, expect_same_constraint); };
        return *this;
    }
    Children &check_forced() {
        Factory old_factory = list.back();
        list.back() = [=](){ return ::check_forced(old_factory()); };
        return *this;
    }
    Children &dropped() {
        Factory old_factory = list.back();
        list.back() = [=](){ return ::dropped(old_factory()); };
        return *this;
    }
    template <ChildCollector Builder>
    void apply(Builder &builder) const {
        for (const Factory &make_child: list) {
            builder.addChild(make_child());
        }
    }
};

struct NoFlow {
    NoFlow(InFlow) noexcept {}
    void add(double) noexcept {};
    bool strict() noexcept { return false; }
    double flow() noexcept { return 0.0; }
};

// Combine children blueprints using a shared filter creation
// algorithm. Satisfies the FilterFactory concept.
template <typename Flow>
struct Combine {
    using factory_fun = std::function<std::unique_ptr<SearchIterator>(const Blueprint::Children &, bool, Constraint)>;
    factory_fun fun;
    bool strict;
    Blueprint::Children list;
    Combine(factory_fun fun_in, const Children &child_list)
      : fun(fun_in), strict(false), list()
    {
        child_list.apply(*this);
    }
    ~Combine();
    void addChild(std::unique_ptr<Blueprint> child) {
        list.push_back(std::move(child));
    }
    void basic_plan(InFlow in_flow, uint32_t my_docid_limit) {
        strict = in_flow.strict();
        Flow flow(in_flow);
        for (auto &child: list) {
            child->basic_plan(InFlow(flow.strict(), flow.flow()), my_docid_limit);
            flow.add(child->estimate());
        }
    }
    auto createFilterSearch(Constraint constraint) const {
        return fun(list, strict, constraint);
    }
};
template <typename Flow>
Combine<Flow>::~Combine() = default;

// enable Make-ing source blender
struct SourceBlenderAdapter {
    NullSelector selector;
    SourceBlenderBlueprint blueprint;
    SourceBlenderAdapter() : selector(), blueprint(selector) {}
    void addChild(std::unique_ptr<Blueprint> child) {
        blueprint.addChild(std::move(child));
    }
    void basic_plan(InFlow in_flow, uint32_t my_docid_limit) {
        blueprint.basic_plan(in_flow, my_docid_limit);
    }
    auto createFilterSearch(Constraint constraint) const {
        return blueprint.createFilterSearch(constraint);
    }
};

// enable Make-ing simple phrase
struct SimplePhraseAdapter {
    FieldSpec field;
    SimplePhraseBlueprint blueprint;
    SimplePhraseAdapter() : field("foo", 3, 7), blueprint(field, false) {}
    void addChild(std::unique_ptr<Blueprint> child) {
        auto child_field = blueprint.getNextChildField(field);
        auto term = std::make_unique<LeafProxy>(child_field, std::move(child));
        blueprint.addTerm(std::move(term));
    }
    void basic_plan(InFlow in_flow, uint32_t my_docid_limit) {
        blueprint.basic_plan(in_flow, my_docid_limit);
    }
    auto createFilterSearch(Constraint constraint) const {
        return blueprint.createFilterSearch(constraint);
    }
};

//enable Make-ing equiv
struct EquivAdapter {
    FieldSpecBaseList fields;
    EquivBlueprint blueprint;
    EquivAdapter() : fields(), blueprint(fields, MatchDataLayout()) {}
    void addChild(std::unique_ptr<Blueprint> child) {
        blueprint.addTerm(std::move(child), 1.0);
    }
    void basic_plan(InFlow in_flow, uint32_t my_docid_limit) {
        blueprint.basic_plan(in_flow, my_docid_limit);
    }
    auto createFilterSearch(Constraint constraint) const {
        return blueprint.createFilterSearch(constraint);
    }
};

// enable Make-ing weighted set
struct WeightedSetTermAdapter {
    FieldSpec field;
    WeightedSetTermBlueprint blueprint;
    WeightedSetTermAdapter();
    ~WeightedSetTermAdapter();
    void addChild(std::unique_ptr<Blueprint> child) {
        Blueprint::HitEstimate estimate = blueprint.getState().estimate();
        blueprint.addTerm(std::move(child), 100, estimate);
        blueprint.complete(estimate);
    }
    void basic_plan(InFlow in_flow, uint32_t my_docid_limit) {
        blueprint.basic_plan(in_flow, my_docid_limit);
    }
    auto createFilterSearch(Constraint constraint) const {
        return blueprint.createFilterSearch(constraint);
    }
};

WeightedSetTermAdapter::WeightedSetTermAdapter() : field("foo", 3, 7), blueprint(field) {}
WeightedSetTermAdapter::~WeightedSetTermAdapter() = default;

// enable Make-ing dot product
struct DotProductAdapter {
    FieldSpec field;
    DotProductBlueprint blueprint;
    DotProductAdapter();
    ~DotProductAdapter();
    void addChild(std::unique_ptr<Blueprint> child) {
        auto child_field = blueprint.getNextChildField(field);
        auto term = std::make_unique<LeafProxy>(child_field, std::move(child));
        Blueprint::HitEstimate estimate = blueprint.getState().estimate();
        blueprint.addTerm(std::move(term), 100, estimate);
        blueprint.complete(estimate);
    }
    void basic_plan(InFlow in_flow, uint32_t my_docid_limit) {
        blueprint.basic_plan(in_flow, my_docid_limit);
    }
    auto createFilterSearch(Constraint constraint) const {
        return blueprint.createFilterSearch(constraint);
    }
};

DotProductAdapter::DotProductAdapter() : field("foo", 3, 7), blueprint(field) {}
DotProductAdapter::~DotProductAdapter() = default;

// enable Make-ing parallel weak and
struct ParallelWeakAndAdapter {
    FieldSpec field;
    ParallelWeakAndBlueprint blueprint;
    ParallelWeakAndAdapter() : field("foo", 3, 7), blueprint(field, 100, 0.0, 1.0) {}
    void addChild(std::unique_ptr<Blueprint> child) {
        auto child_field = blueprint.getNextChildField(field);
        auto term = std::make_unique<LeafProxy>(child_field, std::move(child));
        Blueprint::HitEstimate estimate = blueprint.getState().estimate();
        blueprint.addTerm(std::move(term), 100, estimate);
        blueprint.complete(estimate);
    }
    void basic_plan(InFlow in_flow, uint32_t my_docid_limit) {
        blueprint.basic_plan(in_flow, my_docid_limit);
    }
    auto createFilterSearch(Constraint constraint) const {
        return blueprint.createFilterSearch(constraint);
    }
};

// enable Make-ing same element
struct SameElementAdapter {
    FieldSpec field;
    SameElementBlueprint blueprint;
    SameElementAdapter();
    ~SameElementAdapter();
    void addChild(std::unique_ptr<Blueprint> child) {
        auto child_field = blueprint.getNextChildField("foo", 3);
        auto term = std::make_unique<LeafProxy>(child_field, std::move(child));
        blueprint.addTerm(std::move(term));
    }
    void basic_plan(InFlow in_flow, uint32_t my_docid_limit) {
        blueprint.basic_plan(in_flow, my_docid_limit);
    }
    auto createFilterSearch(Constraint constraint) const {
        return blueprint.createFilterSearch(constraint);
    }
};

SameElementAdapter::SameElementAdapter() : field("foo", 5, 11), blueprint(field, false) {}
SameElementAdapter::~SameElementAdapter() = default;

// Make a specific intermediate-ish blueprint that you can add
// children to. Satisfies the FilterFactory concept.
template <FilterFactory T>
requires ChildCollector<T>
struct Make {
    T blueprint;
    template <typename ... Args>
    Make(const Children &child_list, Args && ... args) : blueprint(std::forward<Args>(args)...) {
        child_list.apply(blueprint);
    }
    void basic_plan(InFlow in_flow, uint32_t my_docid_limit) {
        blueprint.basic_plan(in_flow, my_docid_limit);
    }
    auto createFilterSearch(Constraint constraint) const {
        return blueprint.createFilterSearch(constraint);
    }
};

// what kind of results are we expecting from a filter search?
struct Expect {
    Trinary matches_any = Trinary::Undefined;
    SimpleResult docs;
    size_t children = 0;
    Expect(const std::vector<uint32_t> &docs_in) : docs(make_result(docs_in)) {}
    Expect(Trinary matches_any_in) : matches_any(matches_any_in) {
        REQUIRE(matches_any != Trinary::Undefined);
        if (matches_any == Trinary::True) {
            docs = make_full_result();
        } else {
            docs = make_empty_result();
        }
    }
    Expect &child_count(size_t n) { children = n;  return *this; }
    static Expect empty() { return Expect(Trinary::False); }
    static Expect full() { return Expect(Trinary::True); }
    static Expect hits(const std::vector<uint32_t> &docs) { return Expect(docs); }
};

template <FilterFactory Blueprint>
void verify(Blueprint &&blueprint, bool strict, Constraint constraint, const Expect &expect) {
    CheckParamsProxy::current_strict = strict;
    CheckParamsProxy::current_constraint = constraint;
    blueprint.basic_plan(strict, docid_limit);
    auto filter = blueprint.createFilterSearch(constraint);
    if (expect.children >  0) {
        ASSERT_EQ(filter->isMultiSearch(), true);
        EXPECT_EQ(static_cast<MultiSearch*>(filter.get())->getChildren().size(), expect.children);
    }
    EXPECT_EQ(filter->matches_any(), expect.matches_any);
    switch (filter->matches_any()) {
    case Trinary::True:
        EXPECT_EQ(std::type_index(typeid(*filter)), std::type_index(typeid(FullSearch)));
        break;
    case Trinary::False:
        EXPECT_EQ(std::type_index(typeid(*filter)), std::type_index(typeid(EmptySearch)));
    default:
        break;
    }
    SimpleResult actual;
    if (strict) {
        actual.searchStrict(*filter, docid_limit);
    } else {
        actual.search(*filter, docid_limit);
    }
    EXPECT_EQ(actual, expect.docs);
}

template <FilterFactory Blueprint>
void verify(Blueprint &&blueprint, bool strict, const Expect &expect) {
    for (auto constraint: {lower_bound, upper_bound}) {
        verify(blueprint, strict, constraint, expect);
    }
}

template <FilterFactory Blueprint>
void verify(Blueprint &&blueprint, const Expect &upper, const Expect &lower) {
    for (auto constraint: {lower_bound, upper_bound}) {
        const Expect &expect = (constraint == upper_bound) ? upper : lower;
        for (bool strict: {false, true}) {
            verify(blueprint, strict, constraint, expect);
        }
    }
}

template <FilterFactory Blueprint>
void verify(Blueprint &&blueprint, const Expect &upper_and_lower) {
    verify(blueprint, upper_and_lower, upper_and_lower);
}

TEST(FilterSearchTest, empty_leaf) {
    verify(*empty(), Expect::empty());
}

TEST(FilterSearchTest, full_leaf) {
    verify(*full(), Expect::full());
}

TEST(FilterSearchTest, custom_leaf) {
    verify(*hits({5,10,20}), Expect::hits({5,10,20}));
}

TEST(FilterSearchTest, default_filter) {
    auto adapter = [](const auto &ignore_children, bool ignore_strict, Constraint constraint) {
                       (void) ignore_children;
                       (void) ignore_strict;
                       return Blueprint::create_default_filter(constraint);
                   };
    verify(Combine<NoFlow>(adapter, Children()), Expect::full(), Expect::empty());
}

TEST(FilterSearchTest, simple_or) {
    auto child_list = Children()
        .hits({5, 10}).check(true, true)
        .hits({7}).check(true, true)
        .hits({3, 11}).check(true, true);
    auto expected = Expect::hits({3, 5, 7, 10, 11});
    verify(Combine<OrFlow>(Blueprint::create_or_filter, child_list), expected);
    verify(Make<OrBlueprint>(child_list), expected);
    verify(Make<EquivAdapter>(child_list), expected);
    verify(Combine<OrFlow>(Blueprint::create_atmost_or_filter, child_list), expected, Expect::empty());
    verify(Make<WeakAndBlueprint>(child_list, 100), expected, Expect::empty());
    verify(Make<SourceBlenderAdapter>(child_list), expected, Expect::empty());
    verify(Make<ParallelWeakAndAdapter>(child_list), expected, Expect::empty());
}

TEST(FilterSearchTest, forced_or) {
    auto child_list = Children()
        .hits({5, 10}).check_forced()
        .hits({7}).check_forced()
        .hits({3, 11}).check_forced();
    auto expected = Expect::hits({3, 5, 7, 10, 11});
    verify(Make<WeightedSetTermAdapter>(child_list), expected);
    verify(Make<DotProductAdapter>(child_list), expected);
}

TEST(FilterSearchTest, simple_and) {
    auto child_list = Children()
        .hits({2, 4, 6, 7}).check(true, true)
        .hits({1, 4, 6, 7, 10}).check(false, true)
        .hits({1, 2, 3, 4, 5, 6}).check(false, true);
    auto expected = Expect::hits({4, 6});
    verify(Combine<AndFlow>(Blueprint::create_and_filter, child_list), expected);
    verify(Make<AndBlueprint>(child_list), expected);
    verify(Combine<AndFlow>(Blueprint::create_atmost_and_filter, child_list), expected, Expect::empty());
    verify(Make<NearBlueprint>(child_list, 3), expected, Expect::empty());
    verify(Make<ONearBlueprint>(child_list, 3), expected, Expect::empty());
    verify(Make<SameElementAdapter>(child_list), expected, Expect::empty());
}

TEST(FilterSearchTest, eager_and) {
    auto child_list = Children()
        .hits({2, 4, 6, 7}).check(true, true)
        .hits({1, 4, 6, 7, 10}).check(true, true)
        .hits({1, 2, 3, 4, 5, 6}).check(true, true);
    auto expected = Expect::hits({4, 6});
    verify(Make<SimplePhraseAdapter>(child_list), expected, Expect::empty());
}

TEST(FilterSearchTest, simple_andnot) {
    auto child_list = Children()
        .hits({1, 2, 3, 4, 5, 6}).check(true, true)
        .hits({2, 4, 6}).check(false, false)
        .hits({4, 6, 7}).check(false, false);
    auto expected = Expect::hits({1, 3, 5});
    verify(Combine<AndNotFlow>(Blueprint::create_andnot_filter, child_list), expected);
    verify(Make<AndNotBlueprint>(child_list), expected);
}

TEST(FilterSearchTest, rank_filter) {
    auto child_list1 = Children().hits({1,2,3}).empty().full();
    auto child_list2 = Children().empty().hits({1,2,3}).full();
    auto child_list3 = Children().full().hits({1,2,3}).empty();
    auto adapter = [](const auto &children, bool ignore_strict, Constraint constraint) {
                       (void) ignore_strict;
                       return Blueprint::create_first_child_filter(children, constraint);
                   };
    verify(Combine<RankFlow>(adapter, child_list1), Expect::hits({1,2,3}));
    verify(Combine<RankFlow>(adapter, child_list2), Expect::empty());
    verify(Combine<RankFlow>(adapter, child_list3), Expect::full());
    verify(Make<RankBlueprint>(child_list1), Expect::hits({1,2,3}));
    verify(Make<RankBlueprint>(child_list2), Expect::empty());
    verify(Make<RankBlueprint>(child_list3), Expect::full());
}

TEST(FilterSearchTest, or_short_circuit) {
    auto child_list = Children()
        .hits({5, 10}).check(true, true)
        .full().check(true, true)
        .hits({3, 11}).check(true, true).dropped();
    verify(Combine<OrFlow>(Blueprint::create_or_filter, child_list),
           Expect::full());
}

TEST(FilterSearchTest, or_pruning) {
    auto child_list = Children()
        .empty().check(true, true)
        .empty().check(true, true)
        .empty().check(true, true);
    verify(Combine<OrFlow>(Blueprint::create_or_filter, child_list),
           Expect::empty());
}

TEST(FilterSearchTest, or_partial_pruning) {
    auto child_list = Children()
        .hits({5, 10}).check(true, true)
        .empty().check(true, true)
        .hits({3, 11}).check(true, true);
    verify(Combine<OrFlow>(Blueprint::create_or_filter, child_list),
           Expect::hits({3, 5, 10, 11}).child_count(2));
}

TEST(FilterSearchTest, and_short_circuit) {
    auto child_list = Children()
        .hits({1, 2, 3}).check(true, true)
        .empty().check(false, true)
        .hits({2, 3, 4}).check(false, true).dropped();
    verify(Combine<AndFlow>(Blueprint::create_and_filter, child_list),
           Expect::empty());
}

TEST(FilterSearchTest, and_pruning) {
    auto child_list = Children()
        .full().check(true, true)
        .full().check(false, true)
        .full().check(false, true);
    verify(Combine<AndFlow>(Blueprint::create_and_filter, child_list),
           Expect::full());
}

TEST(FilterSearchTest, and_partial_pruning) {
    auto child_list = Children()
        .hits({1, 2, 3}).check(true, true)
        .full().check(false, true)
        .hits({2, 3, 4}).check(false, true);
    verify(Combine<AndFlow>(Blueprint::create_and_filter, child_list),
           Expect::hits({2, 3}).child_count(2));
}

TEST(FilterSearchTest, andnot_positive_short_circuit) {
    auto child_list = Children()
        .empty().check(true, true)
        .hits({1, 2, 3}).check(false, false).dropped();
    verify(Combine<AndNotFlow>(Blueprint::create_andnot_filter, child_list),
           Expect::empty());
}

TEST(FilterSearchTest, andnot_negative_short_circuit) {
    auto child_list = Children()
        .hits({1, 2, 3}).check(true, true)
        .hits({1}).check(false, false)
        .full().check(false, false)
        .hits({3}).check(false, false).dropped();
    verify(Combine<AndNotFlow>(Blueprint::create_andnot_filter, child_list),
           Expect::empty());
}

TEST(FilterSearchTest, andnot_negative_pruning) {
    auto child_list = Children()
        .full().check(true, true)
        .empty().check(false, false)
        .empty().check(false, false)
        .empty().check(false, false);
    verify(Combine<AndNotFlow>(Blueprint::create_andnot_filter, child_list),
           Expect::full());
}

TEST(FilterSearchTest, andnot_partial_negative_pruning) {
    auto child_list = Children()
        .hits({1, 2, 3}).check(true, true)
        .hits({1}).check(false, false)
        .empty().check(false, false)
        .hits({3}).check(false, false);
    verify(Combine<AndNotFlow>(Blueprint::create_andnot_filter, child_list),
           Expect::hits({2}).child_count(3));
}

TEST(FilterSearchTest, first_or_child_can_be_partially_pruned) {
    auto child_list = Children()
        .empty().check(true, true)
        .hits({5, 10}).check(true, true)
        .hits({3, 11}).check(true, true);
    verify(Combine<OrFlow>(Blueprint::create_or_filter, child_list),
           Expect::hits({3, 5, 10, 11}).child_count(2));
}

TEST(FilterSearchTest, first_and_child_can_only_be_partially_pruned_when_nonstrict) {
    auto child_list = Children()
        .full().check(true, true)
        .hits({1, 2, 3}).check(false, true)
        .hits({2, 3, 4}).check(false, true);
    verify(Combine<AndFlow>(Blueprint::create_and_filter, child_list), true,
           Expect::hits({2, 3}).child_count(3));
    verify(Combine<AndFlow>(Blueprint::create_and_filter, child_list), false,
           Expect::hits({2, 3}).child_count(2));
}

TEST(FilterSearchTest, first_negative_andnot_child_can_be_partially_pruned) {
    auto child_list = Children()
        .hits({1, 2, 3}).check(true, true)
        .empty().check(false, false)
        .hits({1}).check(false, false)
        .hits({3}).check(false, false);
    verify(Combine<AndNotFlow>(Blueprint::create_andnot_filter, child_list),
           Expect::hits({2}).child_count(3));
}

TEST(FilterSearchTest, need_atleast_one_child) {
    verify(Combine<AndFlow>(Blueprint::create_and_filter, Children().full()), Expect::full());
    verify(Combine<OrFlow>(Blueprint::create_or_filter, Children().empty()), Expect::empty());
    verify(Combine<AndNotFlow>(Blueprint::create_andnot_filter, Children().full()), Expect::full());
    EXPECT_THROW(verify(Combine<AndFlow>(Blueprint::create_and_filter, Children()), Expect::empty()),
                 vespalib::RequireFailedException);
    EXPECT_THROW(verify(Combine<OrFlow>(Blueprint::create_or_filter, Children()), Expect::empty()),
                 vespalib::RequireFailedException);
    EXPECT_THROW(verify(Combine<AndNotFlow>(Blueprint::create_andnot_filter, Children()), Expect::empty()),
                 vespalib::RequireFailedException);
}

GTEST_MAIN_RUN_ALL_TESTS()