aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/features/attributematchfeature.h
blob: f6b15ba44c492bc1a62956fcae91dee8197e1d6f (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "queryterm.h"
#include <vespa/searchlib/fef/blueprint.h>

namespace search::features {

struct AttributeMatchParams {
    AttributeMatchParams() :
        attrInfo(nullptr), attribute(nullptr), weightedSet(false), maxWeight(256), fieldCompletenessImportance(0.05f) {}
    const fef::FieldInfo * attrInfo;
    const attribute::IAttributeVector * attribute;
    bool weightedSet;
    // config values
    int32_t maxWeight;
    feature_t fieldCompletenessImportance;
};

/**
 * Implements the executor for the attribute match feature.
 */
template <typename T>
class AttributeMatchExecutor : public fef::FeatureExecutor {
private:
    /**
     * This class is used to compute metrics for match in an attribute vector.
     */
    class Computer {
    private:
        // TermData pointer and significance
        AttributeMatchParams _params;
        mutable T _buffer; // used when fetching weights from a weighted set attribute

        // per query
        uint32_t  _numAttrTerms;
        uint32_t  _totalTermWeight;       // total weight of all terms
        feature_t _totalTermSignificance; // total significance of all terms
        uint32_t  _totalAttrTermWeight;   // weight of all attribute terms
        QueryTermVector _queryTerms; // the terms searching this attribute

        // per doc
        uint32_t  _matches;
        uint32_t  _matchedTermWeight;       // term weight of matched terms
        feature_t _matchedTermSignificance; // significance of matched terms
        int32_t   _totalWeight;
        int32_t   _maxWeight;
        feature_t _normalizedWeightedWeight;
        int32_t   _weightSum; // sum of the weights for a weighted set attribute
        uint32_t  _valueCount; // the number of values for a non-weighted set attribute
        const fef::MatchData *_md;

    public:
        Computer(const fef::IQueryEnvironment & env, AttributeMatchParams params);
        void run(uint32_t docId);
        void reset();
        uint32_t getNumTerms() const { return _queryTerms.size(); }
        uint32_t getMatches() const { return _matches; }
        int32_t getTotalWeight() const { return _totalWeight; }
        int32_t getMaxWeight() const { return _maxWeight; }
        feature_t getAverageWeight() const;
        feature_t getQueryCompleteness() const;
        feature_t getNormalizedWeight() const;
        feature_t getNormalizedWeightedWeight() const;
        feature_t getFieldCompleteness() const;
        feature_t getCompleteness() const;
        feature_t getWeight() const;
        feature_t getSignificance() const;
        feature_t getImportance() const { return (getWeight() + getSignificance()) * 0.5; }
        void bind_match_data(const fef::MatchData &md) { _md = &md; }
    };

    Computer _cmp;

    void handle_bind_match_data(const fef::MatchData &md) override;

public:
    /**
     * Constructs an executor.
     */
    AttributeMatchExecutor(const fef::IQueryEnvironment & env, AttributeMatchParams params);
    void execute(uint32_t docId) override;
};


/**
 * Implements the blueprint for the attribute match executor.
 */
class AttributeMatchBlueprint : public fef::Blueprint {
private:
    AttributeMatchParams _params;

public:
    AttributeMatchBlueprint();

    void visitDumpFeatures(const fef::IIndexEnvironment & env, fef::IDumpFeatureVisitor & visitor) const override;
    fef::Blueprint::UP createInstance() const override;
    fef::ParameterDescriptions getDescriptions() const override {
        return fef::ParameterDescriptions().desc().attributeField(fef::ParameterDataTypeSet::normalTypeSet(), fef::ParameterCollection::ANY);
    }

    bool setup(const fef::IIndexEnvironment & env, const fef::ParameterList & params) override;

    fef::FeatureExecutor &createExecutor(const fef::IQueryEnvironment &env, vespalib::Stash &stash) const override;

    void prepareSharedState(const fef::IQueryEnvironment &queryEnv, fef::IObjectStore &objectStore) const override;
};

}