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

#pragma once

#include <vespa/searchlib/common/feature.h>
#include <vespa/searchlib/fef/iqueryenvironment.h>
#include <vespa/searchlib/fef/itermdata.h>

namespace search::features {

/**
 * This class represents a query term with the relevant data. Now also
 * with an optional attachment of a TermFieldData pointer.
 */
class QueryTerm {
private:
    const fef::ITermData *_termData;
    fef::TermFieldHandle  _handle;
    feature_t             _significance;
    feature_t             _connectedness;
public:
    QueryTerm()
        : _termData(nullptr),
          _handle(fef::IllegalHandle),
          _significance(0),
          _connectedness(0)
    { }
    QueryTerm(const fef::ITermData *td, feature_t sig = 0, feature_t con = 0)
        : _termData(td),
          _handle(fef::IllegalHandle),
          _significance(sig),
          _connectedness(con)
    { }
    const fef::ITermData *termData() const { return _termData; }
    feature_t significance() const { return _significance; }
    feature_t connectedness() const { return _connectedness; }
    fef::TermFieldHandle fieldHandle() const { return _handle; }
    void fieldHandle(fef::TermFieldHandle handle) { _handle = handle; }
    void fieldHandle(const fef::ITermFieldData *fd) {
        if (fd) {
            _handle = fd->getHandle();
        }
    }
};

/**
 * Convenience typedef for a vector of QueryTerm objects.
 */
using QueryTermVector = std::vector<QueryTerm>;

/**
 * This class is a factory for creating QueryTerm objects.
 */
class QueryTermFactory {
public:
    /**
     * Creates a new QueryTerm object for the term with the given term index.
     *
     * @param env the environment used to lookup TermData object, significance, and connectedness.
     * @param termIndex the index to use when looking up the TermData object.
     * @param lookupConnectedness whether we should look up the connectedness this term has with the previous term.
     */
    static QueryTerm create(const fef::IQueryEnvironment & env, uint32_t termIndex, bool lookupConnectedness = false);
};

/**
 * Helper class to allow simple reuse of processed QueryTermVector
 * containing all terms in the query. Primary reason is to reduce expensive recomputation
 * when multiple features need the same and also only compute it once per query and one once per search thread.
 */
class QueryTermHelper {
public:
    QueryTermHelper(const fef::IQueryEnvironment & env);
    const QueryTermVector & terms() const { return *_queryTerms; }
    static const QueryTermVector & lookupAndStoreQueryTerms(const fef::IQueryEnvironment & env, fef::IObjectStore & objectStore);
private:
    static const QueryTermVector * lookupQueryTerms(const fef::IQueryEnvironment & env);
    static QueryTermVector createQueryTermvector(const fef::IQueryEnvironment & env);
    QueryTermVector         _fallBack;
    const QueryTermVector * _queryTerms;
};

}