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

#include "phrase_query_node.h"
#include "hit_iterator_pack.h"
#include <cassert>

namespace search::streaming {

PhraseQueryNode::PhraseQueryNode(std::unique_ptr<QueryNodeResultBase> result_base, const string& index, uint32_t num_terms)
    : MultiTerm(std::move(result_base), index, num_terms)
{
}

PhraseQueryNode::~PhraseQueryNode() = default;

bool
PhraseQueryNode::evaluate() const
{
  HitList hl;
  return ! evaluateHits(hl).empty();
}

size_t
PhraseQueryNode::width() const
{
    return get_terms().size();
}

const HitList &
PhraseQueryNode::evaluateHits(HitList & hl) const
{
    hl.clear();
    auto& terms = get_terms();
    HitIteratorPack itr_pack(terms);
    if (!itr_pack.all_valid()) {
        return hl;
    }
    while (itr_pack.seek_to_matching_field_element()) {
        uint32_t first_position = itr_pack.front()->position();
        bool retry_element = true;
        while (retry_element) {
            uint32_t position_offset = 0;
            bool match = true;
            for (auto& it : itr_pack) {
                if (!it.seek_in_field_element(first_position + position_offset, itr_pack.get_field_element_ref())) {
                    retry_element = false;
                    match = false;
                    break;
                }
                if (it->position() > first_position + position_offset) {
                    first_position = it->position() - position_offset;
                    match = false;
                    break;
                }
                ++position_offset;
            }
            if (match) {
                auto h = *itr_pack.front();
                hl.push_back(h);
                if (!itr_pack.front().step_in_field_element(itr_pack.get_field_element_ref())) {
                    retry_element = false;
                }
            }
        }
    }
    return hl;
}

void
PhraseQueryNode::unpack_match_data(uint32_t docid, const fef::ITermData& td, fef::MatchData& match_data, const fef::IIndexEnvironment& index_env)
{
    HitList list;
    const HitList & hit_list = evaluateHits(list);
    unpack_match_data_helper(docid, td, match_data, hit_list, *get_terms().front(), is_filter(), index_env);
}

}