aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/matching/query.cpp
blob: a7e8c4d2f71e1f09d4cb8b3602357497badf959f (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "query.h"
#include "blueprintbuilder.h"
#include "matchdatareservevisitor.h"
#include "resolveviewvisitor.h"
#include "termdataextractor.h"
#include <vespa/document/datatype/positiondatatype.h>
#include <vespa/searchlib/common/location.h>
#include <vespa/searchlib/parsequery/stackdumpiterator.h>
#include <vespa/searchlib/query/tree/point.h>
#include <vespa/searchlib/query/tree/rectangle.h>
#include <vespa/searchlib/queryeval/intermediate_blueprints.h>

#include <vespa/log/log.h>
LOG_SETUP(".proton.matching.query");
#include <vespa/searchlib/query/tree/querytreecreator.h>

using document::PositionDataType;
using search::SimpleQueryStackDumpIterator;
using search::fef::IIndexEnvironment;
using search::fef::ITermData;
using search::fef::MatchData;
using search::fef::MatchDataLayout;
using search::fef::Location;
using search::query::Node;
using search::query::QueryTreeCreator;
using search::query::Weight;
using search::queryeval::AndBlueprint;
using search::queryeval::Blueprint;
using search::queryeval::IRequestContext;
using search::queryeval::SearchIterator;
using vespalib::string;
using std::vector;

namespace proton::matching {

namespace {
void AddLocationNode(const string &location_str, Node::UP &query_tree, Location &fef_location) {
    if (location_str.empty()) {
        return;
    }
    string::size_type pos = location_str.find(':');
    if (pos == string::npos) {
        LOG(warning, "Location string lacks attribute vector specification. loc='%s'",
            location_str.c_str());
        return;
    }
    const string view = PositionDataType::getZCurveFieldName(
            location_str.substr(0, pos));
    const string loc = location_str.substr(pos + 1);

    search::common::Location locationSpec;
    if (!locationSpec.parse(loc)) {
        LOG(warning, "Location parse error (location: '%s'): %s",
            location_str.c_str(),
            locationSpec.getParseError());
        return;
    }

    int32_t id = -1;
    Weight weight(100);

    ProtonAnd::UP new_base(new ProtonAnd);
    new_base->append(std::move(query_tree));

    if (locationSpec.getRankOnDistance()) {
        new_base->append(Node::UP(new ProtonLocationTerm(loc, view, id, weight)));
        fef_location.setAttribute(view);
        fef_location.setXPosition(locationSpec.getX());
        fef_location.setYPosition(locationSpec.getY());
        fef_location.setXAspect(locationSpec.getXAspect());
        fef_location.setValid(true);
    } else if (locationSpec.getPruneOnDistance()) {
        new_base->append(Node::UP(new ProtonLocationTerm(loc, view, id, weight)));
    }
    query_tree = std::move(new_base);
}
}  // namespace

Query::Query() {}
Query::~Query() {}

bool
Query::buildTree(const vespalib::stringref &stack, const string &location,
                 const ViewResolver &resolver, const IIndexEnvironment &indexEnv)
{
    SimpleQueryStackDumpIterator stack_dump_iterator(stack);
    _query_tree = QueryTreeCreator<ProtonNodeTypes>::create(stack_dump_iterator);
    if (_query_tree.get()) {
        AddLocationNode(location, _query_tree, _location);
        ResolveViewVisitor resolve_visitor(resolver, indexEnv);
        _query_tree->accept(resolve_visitor);
        return true;
    } else {
        // TODO(havardpe): log warning or pass message upwards
        return false;
    }
}

void
Query::extractTerms(vector<const ITermData *> &terms)
{
    TermDataExtractor::extractTerms(*_query_tree, terms);
}

void
Query::extractLocations(vector<const Location *> &locations)
{
    locations.clear();
    locations.push_back(&_location);
}

void
Query::setWhiteListBlueprint(Blueprint::UP whiteListBlueprint)
{
    _whiteListBlueprint = std::move(whiteListBlueprint);
}

void
Query::reserveHandles(const IRequestContext & requestContext, ISearchContext &context, MatchDataLayout &mdl)
{
    MatchDataReserveVisitor reserve_visitor(mdl);
    _query_tree->accept(reserve_visitor);

    _blueprint = BlueprintBuilder::build(requestContext, *_query_tree, context);
    LOG(debug, "original blueprint:\n%s\n", _blueprint->asString().c_str());
    if (_whiteListBlueprint) {
        std::unique_ptr<AndBlueprint> andBlueprint(new AndBlueprint());
        (*andBlueprint)
            .addChild(std::move(_blueprint))
            .addChild(std::move(_whiteListBlueprint));
        _blueprint = std::move(andBlueprint);
        _blueprint->setDocIdLimit(context.getDocIdLimit());
        LOG(debug, "blueprint after white listing:\n%s\n", _blueprint->asString().c_str());
    }
}

void
Query::optimize()
{
    _blueprint = Blueprint::optimize(std::move(_blueprint));
    LOG(debug, "optimized blueprint:\n%s\n", _blueprint->asString().c_str());
}

void
Query::fetchPostings()
{
    _blueprint->fetchPostings(true, nullptr);
}

void
Query::freeze()
{
    _blueprint->freeze();
}

Blueprint::HitEstimate
Query::estimate() const
{
    return _blueprint->getState().estimate();
}

SearchIterator::UP
Query::createSearch(MatchData &md) const
{
    return _blueprint->createSearch(md, true);
}

}