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

#include "matchcountfeature.h"
#include "utils.h"
#include "valuefeature.h"
#include <vespa/vespalib/util/stash.h>

using namespace search::fef;

namespace search::features {

namespace {

/**
* Implements the executor for the matchCount feature for index and
* attribute fields.
*/
class MatchCountExecutor : public fef::FeatureExecutor {
private:
    std::vector<fef::TermFieldHandle> _handles;
    const fef::MatchData *_md;

    void handle_bind_match_data(const fef::MatchData &md) override {
        _md = &md;
    }

public:
    MatchCountExecutor(uint32_t fieldId, const fef::IQueryEnvironment &env);

    void execute(uint32_t docId) override;
};

MatchCountExecutor::MatchCountExecutor(uint32_t fieldId, const IQueryEnvironment &env)
    : FeatureExecutor(),
      _handles(),
      _md(nullptr)
{
    for (uint32_t i = 0; i < env.getNumTerms(); ++i) {
        TermFieldHandle handle = util::getTermFieldHandle(env, i, fieldId);
        if (handle != IllegalHandle) {
            _handles.push_back(handle);
        }
    }
}

void
MatchCountExecutor::execute(uint32_t docId) {
    size_t output = 0;
    for (uint32_t i = 0; i < _handles.size(); ++i) {
        const TermFieldMatchData *tfmd = _md->resolveTermField(_handles[i]);
        if (tfmd->getDocId() == docId) {
            output++;
        }
    }
    outputs().set_number(0, static_cast<feature_t>(output));
}

}

MatchCountBlueprint::MatchCountBlueprint() :
    Blueprint("matchCount"),
    _field(nullptr)
{
}

void
MatchCountBlueprint::visitDumpFeatures(const IIndexEnvironment &, IDumpFeatureVisitor &) const
{
}

bool
MatchCountBlueprint::setup(const IIndexEnvironment &, const ParameterList & params)
{
    _field = params[0].asField();
    describeOutput("out", "Returns number of matches in the field of all terms in the query");
    return true;
}

Blueprint::UP
MatchCountBlueprint::createInstance() const
{
    return std::make_unique<MatchCountBlueprint>();
}

FeatureExecutor &
MatchCountBlueprint::createExecutor(const IQueryEnvironment & queryEnv, vespalib::Stash &stash) const
{
    if (_field == nullptr) {
        return stash.create<SingleZeroValueExecutor>();
    }
    return stash.create<MatchCountExecutor>(_field->id(), queryEnv);
}

}