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

namespace search::streaming {

namespace {

constexpr bool normalizing_implies_cased(Normalizing norm) noexcept {
    return (norm == Normalizing::NONE);
}

}

FuzzyTerm::FuzzyTerm(std::unique_ptr<QueryNodeResultBase> result_base, stringref term,
                     const string& index, Type type, Normalizing normalizing,
                     uint8_t max_edits, uint32_t prefix_size)
    : QueryTerm(std::move(result_base), term, index, type, normalizing),
      _dfa_matcher(),
      _fallback_matcher()
{
    setFuzzyMaxEditDistance(max_edits);
    setFuzzyPrefixLength(prefix_size);

    std::string_view term_view(term.data(), term.size());
    const bool cased = normalizing_implies_cased(normalizing);
    if (attribute::DfaFuzzyMatcher::supports_max_edits(max_edits)) {
        _dfa_matcher = std::make_unique<attribute::DfaFuzzyMatcher>(term_view, max_edits, prefix_size, cased);
    } else {
        _fallback_matcher = std::make_unique<vespalib::FuzzyMatcher>(term_view, max_edits, prefix_size, cased);
    }
}

FuzzyTerm::~FuzzyTerm() = default;

bool FuzzyTerm::is_match(std::string_view term) const {
    if (_dfa_matcher) {
        return _dfa_matcher->is_match(term);
    } else {
        return _fallback_matcher->isMatch(term);
    }
}

}