summaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/queryeval/create_blueprint_visitor_helper.cpp
blob: d9338641a39439f0b7dfd32bf864772caad41898 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.


#include "create_blueprint_visitor_helper.h"
#include "leaf_blueprints.h"
#include "dot_product_blueprint.h"
#include "get_weight_from_node.h"
#include "wand/parallel_weak_and_blueprint.h"
#include "simple_phrase_blueprint.h"
#include "weighted_set_term_blueprint.h"
#include "split_float.h"
#include "irequestcontext.h"

namespace search::queryeval {

CreateBlueprintVisitorHelper::CreateBlueprintVisitorHelper(Searchable &searchable, const FieldSpec &field, const IRequestContext & requestContext)
    : _requestContext(requestContext),
      _searchable(searchable),
      _field(field),
      _result()
{}

CreateBlueprintVisitorHelper::~CreateBlueprintVisitorHelper() = default;

attribute::SearchContextParams
CreateBlueprintVisitorHelper::createContextParams() const {
    return attribute::SearchContextParams().metaStoreReadGuard(_requestContext.getMetaStoreReadGuard());
}
attribute::SearchContextParams
CreateBlueprintVisitorHelper::createContextParams(bool useBitVector) const {
    return createContextParams().useBitVector(useBitVector);
}

Blueprint::UP
CreateBlueprintVisitorHelper::getResult()
{
    return _result
        ? std::move(_result)
        : std::make_unique<EmptyBlueprint>(_field);
}

void
CreateBlueprintVisitorHelper::visitPhrase(query::Phrase &n) {
    auto phrase = std::make_unique<SimplePhraseBlueprint>(_field, n.is_expensive());
    for (const query::Node * child : n.getChildren()) {
        FieldSpecList fields;
        fields.add(phrase->getNextChildField(_field));
        phrase->addTerm(_searchable.createBlueprint(_requestContext, fields, *child));
    }
    setResult(std::move(phrase));
}

void
CreateBlueprintVisitorHelper::handleNumberTermAsText(query::NumberTerm &n)
{
    vespalib::string termStr = termAsString(n);
    queryeval::SplitFloat splitter(termStr);
    if (splitter.parts() > 1) {
        query::SimplePhrase phraseNode(n.getView(), n.getId(), n.getWeight());
        phraseNode.setStateFrom(n);
        for (size_t i = 0; i < splitter.parts(); ++i) {
            phraseNode.append(std::make_unique<query::SimpleStringTerm>(splitter.getPart(i), "", 0, query::Weight(0)));
        }
        visitPhrase(phraseNode);
    } else {
        if (splitter.parts() == 1) {
            termStr = splitter.getPart(0);
        }
        query::SimpleStringTerm stringNode(termStr, n.getView(), n.getId(), n.getWeight());
        stringNode.setStateFrom(n);
        visit(stringNode);
    }
}

template <typename WS, typename NODE>
void
CreateBlueprintVisitorHelper::createWeightedSet(std::unique_ptr<WS> bp, NODE &n) {
    FieldSpecList fields;
    for (size_t i = 0; i < n.getNumTerms(); ++i) {
        fields.clear();
        fields.add(bp->getNextChildField(_field));
        auto term = n.getAsString(i);
        query::SimpleStringTerm node(term.first, n.getView(), 0, term.second); // TODO Temporary
        bp->addTerm(_searchable.createBlueprint(_requestContext, fields, node), term.second.percent());
    }
    setResult(std::move(bp));
}
void
CreateBlueprintVisitorHelper::visitWeightedSetTerm(query::WeightedSetTerm &n) {
    createWeightedSet(std::make_unique<WeightedSetTermBlueprint>(_field), n);
}
void
CreateBlueprintVisitorHelper::visitDotProduct(query::DotProduct &n) {
    createWeightedSet(std::make_unique<DotProductBlueprint>(_field), n);
}
void
CreateBlueprintVisitorHelper::visitWandTerm(query::WandTerm &n) {
    createWeightedSet(std::make_unique<ParallelWeakAndBlueprint>(_field, n.getTargetNumHits(),
                                                                 n.getScoreThreshold(), n.getThresholdBoostFactor()),
                      n);
}

void CreateBlueprintVisitorHelper::visit(query::TrueQueryNode &) {
    setResult(std::make_unique<AlwaysTrueBlueprint>());
}

void CreateBlueprintVisitorHelper::visit(query::FalseQueryNode &) {
    setResult(std::make_unique<EmptyBlueprint>());
}

}