aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/queryeval/simple_phrase_blueprint.cpp
blob: 621fb101d5d93860b220aa2602a411f5aae5af5b (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "simple_phrase_blueprint.h"
#include "simple_phrase_search.h"
#include "field_spec.hpp"
#include <vespa/vespalib/objects/visit.hpp>
#include <map>

namespace search::queryeval {

SimplePhraseBlueprint::SimplePhraseBlueprint(const FieldSpec &field, bool expensive)
    : ComplexLeafBlueprint(field),
      _field(field),
      _estimate(),
      _layout(),
      _terms()
{
    if (expensive) {
        set_cost_tier(State::COST_TIER_EXPENSIVE);
    }
}

SimplePhraseBlueprint::~SimplePhraseBlueprint() = default;

FieldSpec
SimplePhraseBlueprint::getNextChildField(const FieldSpec &outer)
{
    return {outer.getName(), outer.getFieldId(), _layout.allocTermField(outer.getFieldId()), false};
}

void
SimplePhraseBlueprint::addTerm(Blueprint::UP term)
{
    const State &childState = term->getState();
    assert(childState.numFields() == 1);
    const FieldSpecBase &childField = childState.field(0);
    assert(childField.getFieldId() == _field.getFieldId());
    (void) childField;

    HitEstimate childEst = childState.estimate();
    if (_terms.empty() ||  childEst < _estimate) {
        _estimate = childEst;
    }
    setEstimate(_estimate);
    _terms.push_back(std::move(term));
}

void
SimplePhraseBlueprint::sort(InFlow in_flow)
{
    strict(in_flow.strict());
    for (auto &term: _terms) {
        term->sort(in_flow);
    }
}

FlowStats
SimplePhraseBlueprint::calculate_flow_stats(uint32_t docid_limit) const
{
    for (auto &term: _terms) {
        term->update_flow_stats(docid_limit);
    }
    double est = AndFlow::estimate_of(_terms);
    return {est,
            AndFlow::cost_of(_terms, false) + est * _terms.size(),
            AndFlow::cost_of(_terms, true) + est * _terms.size()};
}

SearchIterator::UP
SimplePhraseBlueprint::createLeafSearch(const fef::TermFieldMatchDataArray &tfmda) const
{
    assert(tfmda.size() == 1);
    fef::MatchData::UP md = _layout.createMatchData();
    fef::TermFieldMatchDataArray childMatch;
    SimplePhraseSearch::Children children;
    children.reserve(_terms.size());
    std::multimap<uint32_t, uint32_t> order_map;
    for (size_t i = 0; i < _terms.size(); ++i) {
        const State &childState = _terms[i]->getState();
        assert(childState.numFields() == 1);
        auto *child_term_field_match_data = childState.field(0).resolve(*md);
        child_term_field_match_data->setNeedInterleavedFeatures(tfmda[0]->needs_interleaved_features());
        child_term_field_match_data->setNeedNormalFeatures(true);
        childMatch.add(child_term_field_match_data);
        children.push_back(_terms[i]->createSearch(*md));
        order_map.insert(std::make_pair(childState.estimate().estHits, i));
    }
    std::vector<uint32_t> eval_order;
    eval_order.reserve(order_map.size());
    for (const auto & child : order_map) {
        eval_order.push_back(child.second);
    }
    return std::make_unique<SimplePhraseSearch>(std::move(children),
                                                std::move(md), std::move(childMatch),
                                                std::move(eval_order), *tfmda[0], strict());
}

SearchIterator::UP
SimplePhraseBlueprint::createFilterSearch(FilterConstraint constraint) const
{
    return create_atmost_and_filter(_terms, strict(), constraint);
}

void
SimplePhraseBlueprint::fetchPostings(const ExecuteInfo &execInfo)
{
    for (auto & term : _terms) {
        term->fetchPostings(execInfo);
    }
}

void
SimplePhraseBlueprint::visitMembers(vespalib::ObjectVisitor &visitor) const
{
    LeafBlueprint::visitMembers(visitor);
    visit(visitor, "terms", _terms);
}

}