summaryrefslogtreecommitdiffstats
path: root/searchsummary/src/vespa/searchsummary/docsummary/keywordextractor.cpp
blob: 8b544eb09f674d7d1475bdadff823a7097b1332f (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "keywordextractor.h"
#include "idocsumenvironment.h"
#include <vespa/searchlib/parsequery/stackdumpiterator.h>
#include <vespa/vespalib/stllike/hashtable.hpp>
#include <vespa/vespalib/util/size_literals.h>

/** Tell us what parts of the query we are interested in */

namespace search::docsummary {


bool useful(search::ParseItem::ItemCreator creator)
{
    return creator == search::ParseItem::CREA_ORIG;
}


KeywordExtractor::KeywordExtractor(IDocsumEnvironment * env)
    : _env(env),
      _legalPrefixes(),
      _legalIndexes()
{
}


KeywordExtractor::~KeywordExtractor() = default;

bool
KeywordExtractor::IsLegalIndexName(const char *idxName) const
{
    return _legalIndexes.find(idxName) != _legalIndexes.end();
}

KeywordExtractor::IndexPrefix::IndexPrefix(const char *prefix)
    : _prefix(prefix)
{
}

KeywordExtractor::IndexPrefix::~IndexPrefix() = default;

bool
KeywordExtractor::IndexPrefix::Match(const char *idxName) const
{
    return vespalib::starts_with(idxName, _prefix);
}

void
KeywordExtractor::AddLegalIndexSpec(const char *spec)
{
    if (spec == nullptr)
        return;

    vespalib::string toks(spec); // tokens
    vespalib::string tok; // single token
    size_t           offset; // offset into tokens buffer
    size_t           seppos; // separator position

    offset = 0;
    while ((seppos = toks.find(';', offset)) != vespalib::string::npos) {
        if (seppos == offset) {
            offset++; // don't want empty tokens
        } else {
            tok = toks.substr(offset, seppos - offset);
            offset = seppos + 1;
            if (tok[tok.size() - 1] == '*') {
                tok.resize(tok.size() - 1);
                AddLegalIndexPrefix(tok.c_str());
            } else {
                AddLegalIndexName(tok.c_str());
            }
        }
    }
    if (toks.size() > offset) { // catch last token
        tok = toks.substr(offset);
        if (tok[tok.size() - 1] == '*') {
            tok.resize(tok.size() - 1);
            AddLegalIndexPrefix(tok.c_str());
        } else {
            AddLegalIndexName(tok.c_str());
        }
    }
}


vespalib::string
KeywordExtractor::GetLegalIndexSpec()
{
    vespalib::string spec;

    if (!_legalPrefixes.empty()) {
        for (auto& prefix : _legalPrefixes) {
            if (!spec.empty()) {
                spec.append(';');
            }
            spec.append(prefix.get_prefix());
            spec.append('*');
        }
    }

    for (const auto & index : _legalIndexes) {
        if (!spec.empty())
            spec.append(';');
        spec.append(index);
    }
    return spec;
}


bool
KeywordExtractor::IsLegalIndex(vespalib::stringref idxS) const
{
    vespalib::string resolvedIdxName;

    if (_env != nullptr) {
        resolvedIdxName = _env->lookupIndex(idxS);
    } else {

        if ( ! idxS.empty() ) {
            resolvedIdxName = idxS;
        } else {
            resolvedIdxName = "__defaultindex";
        }
    }

    if (resolvedIdxName.empty())
        return false;

    return (IsLegalIndexPrefix(resolvedIdxName.c_str()) ||
            IsLegalIndexName(resolvedIdxName.c_str()));
}

}