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

#include "fieldlengthfeature.h"
#include "valuefeature.h"
#include "utils.h"
#include <vespa/searchlib/fef/itermdata.h>
#include <vespa/searchlib/fef/featurenamebuilder.h>
#include <vespa/searchlib/fef/fieldinfo.h>
#include <vespa/vespalib/util/stash.h>


using namespace search::fef;

namespace search::features {

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

void
FieldLengthExecutor::execute(uint32_t docId)
{
    uint32_t val = 0;
    bool validVal = false;
    for (auto handle : _fieldHandles) {
        const TermFieldMatchData &tfmd = *_md->resolveTermField(handle);
        if (tfmd.getDocId() == docId) {
            FieldPositionsIterator it = tfmd.getIterator();
            if (it.valid()) {
                if (val < it.getFieldLength())
                    val = it.getFieldLength();
                validVal = true;
            }
        }
    }
    if (!validVal) {
        val = fef::FieldPositionsIterator::UNKNOWN_LENGTH;
    }
    feature_t value = val;
    outputs().set_number(0, value); // field length
}

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

FieldLengthBlueprint::FieldLengthBlueprint()
    : Blueprint("fieldLength"),
      _field(nullptr)
{
}

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

bool
FieldLengthBlueprint::setup(const IIndexEnvironment &env,
                            const ParameterList &params)
{
    (void) env;
    _field = params[0].asField();
    describeOutput("out", "The length of this field.");
    return true;
}

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

FeatureExecutor &
FieldLengthBlueprint::createExecutor(const IQueryEnvironment &env, vespalib::Stash &stash) const
{
    if (_field == 0) {
        return stash.create<SingleValueExecutor>(fef::FieldPositionsIterator::UNKNOWN_LENGTH);
    }
    return stash.create<FieldLengthExecutor>(env, _field->id());
}

}