aboutsummaryrefslogtreecommitdiffstats
path: root/searchsummary/src/vespa/juniper/querymodifier.h
blob: 5816740033f0c0cefd048e105689f067c8b6c4ae (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "simplemap.h"
#include "query.h"
#include "rewriter.h"
#include <vespa/vespalib/stllike/string.h>
#include <string>
#include <vector>

class QueryTerm;
class QueryExpr;
class MatchObject;

// This module encapsulates the preinitialized data structure for handling
// query or document rewriting. Eg. it is configured based on AddRewriter calls
// (See external header rewriter.h) and used until system shutdown..

// Note that per query state (reducematcher.h, expcache.h) and per hit state
// (Matcher.h) is maintained elsewhere..

namespace juniper
{

class string_matcher;
class QueryModifier;

// Wrapper around supplied IRewriter that in addition offer
// the way it is configured in the system
class Rewriter
{
public:
    Rewriter(IRewriter* rewriter, bool for_query, bool for_document);
    inline bool ForQuery()    { return _for_query; }
    inline bool ForDocument() { return _for_document; }
    inline RewriteHandle* Rewrite(uint32_t langid, const char* term)
    { return _rewriter->Rewrite(langid, term); }
    inline RewriteHandle* Rewrite(uint32_t langid, const char* term, size_t len)
    { return _rewriter->Rewrite(langid, term, len); }
    inline const char* NextTerm(RewriteHandle* exp, size_t& length)
    { return _rewriter->NextTerm(exp, length); }
private:
    IRewriter* _rewriter;
    bool _for_query;
    bool _for_document;

    Rewriter(Rewriter &);
    Rewriter &operator=(Rewriter &);
};


class QueryModifier
{
public:
    QueryModifier();
    virtual ~QueryModifier();

    /** See rewriter.h for doc */
    void AddRewriter(const char* index_name, IRewriter* rewriter,
		     bool for_query, bool for_document);

    inline bool HasExpanders() { return _has_expanders; }
    inline bool HasReducers() { return _has_reducers; }
    inline bool HasRewriters() { return _has_expanders || _has_reducers; }

    /* Return any configured reducer/expander for the index, if any */
    Rewriter* FindRewriter(vespalib::stringref index_name);

    /* Delete/dereference all rewriters (needed for testing/debugging) */
    void FlushRewriters();
private:
    simplemap<std::string, Rewriter*> _rewriters;
    bool _has_expanders;
    bool _has_reducers;
};


} // end namespace juniper