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

#include "string_search_context.h"
#include "enumhintsearchcontext.h"
#include "enumstore.h"
#include <vespa/searchlib/query/query_term_ucs4.h>
#include <vespa/vespalib/util/regexp.h>
#include <vespa/vespalib/fuzzy/fuzzy_matcher.h>

namespace search::attribute {

StringSearchContext::StringSearchContext(const AttributeVector& to_be_searched, std::unique_ptr<QueryTermSimple> query_term,
                                         bool cased, vespalib::FuzzyMatchingAlgorithm fuzzy_matching_algorithm)
    : SearchContext(to_be_searched),
      StringMatcher(std::move(query_term), cased, fuzzy_matching_algorithm)
{
}

StringSearchContext::StringSearchContext(const AttributeVector& to_be_searched, StringMatcher &&matcher)
    : SearchContext(to_be_searched),
      StringMatcher(std::move(matcher))
{
}

StringSearchContext::StringSearchContext(StringSearchContext &&) noexcept = default;
StringSearchContext::~StringSearchContext() = default;

const QueryTermUCS4*
StringSearchContext::queryTerm() const
{
    return get_query_term_ptr();
}

bool
StringSearchContext::valid() const
{
    return StringMatcher::isValid();
}

void
StringSearchContext::setup_enum_hint_sc(const EnumStoreT<const char*>& enum_store, EnumHintSearchContext& enum_hint_sc)
{
    _plsc = &enum_hint_sc;
    if (valid()) {
        if (isPrefix()) {
            auto comp = enum_store.make_folded_comparator_prefix(queryTerm()->getTerm());
            enum_hint_sc.lookupRange(comp, comp);
        } else if (isRegex()) {
            vespalib::string prefix(vespalib::RegexpUtil::get_prefix(queryTerm()->getTerm()));
            auto comp = enum_store.make_folded_comparator_prefix(prefix.c_str());
            enum_hint_sc.lookupRange(comp, comp);
        } else if (isFuzzy()) {
            vespalib::string prefix(getFuzzyMatcher().getPrefix());
            auto comp = enum_store.make_folded_comparator_prefix(prefix.c_str());
            enum_hint_sc.lookupRange(comp, comp);
        } else {
            auto comp = enum_store.make_folded_comparator(queryTerm()->getTerm());
            enum_hint_sc.lookupTerm(comp);
        }
    }
}

}