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

#pragma once

#include "attributevector.h"
#include "bitvector_search_cache.h"
#include <vespa/searchcommon/attribute/i_search_context.h>
#include <vespa/searchlib/attribute/posting_list_merger.h>
#include <vespa/searchlib/common/i_document_meta_store_context.h>
#include <vespa/vespalib/util/arrayref.h>

namespace search::fef { class TermFieldMatchData; }

namespace search::attribute {

class ReferenceAttribute;
class ImportedAttributeVector;
class SearchContextParams;

/**
 * Search context exposing iteraton over an imported attribute vector.
 *
 * Iterator doc id matching is performed via the GID->LID indirection of the
 * associated reference attribute. This means that if the _referenced_ document
 * matches the search term, the doc id of the _referring_ document will be
 * considered a match.
 */
class ImportedSearchContext : public ISearchContext {
    using TargetLids = vespalib::ConstArrayRef<uint32_t>;
    const ImportedAttributeVector&                  _imported_attribute;
    vespalib::string                                _queryTerm;
    bool                                            _useSearchCache;
    BitVectorSearchCache::Entry::SP                 _searchCacheLookup;
    IDocumentMetaStoreContext::IReadGuard::UP       _dmsReadGuard;
    const ReferenceAttribute&                       _reference_attribute;
    const IAttributeVector                         &_target_attribute;
    std::unique_ptr<ISearchContext>                 _target_search_context;
    TargetLids                                      _targetLids;
    PostingListMerger<int32_t>                      _merger;
    bool                                            _fetchPostingsDone;

    uint32_t getTargetLid(uint32_t lid) const {
        return _targetLids[lid];
    }

    void makeMergedPostings(bool isFilter);
    void considerAddSearchCacheEntry();
public:
    ImportedSearchContext(std::unique_ptr<QueryTermSimple> term,
                          const SearchContextParams& params,
                          const ImportedAttributeVector& imported_attribute,
                          const attribute::IAttributeVector &target_attribute);
    ~ImportedSearchContext();


    std::unique_ptr<queryeval::SearchIterator>
    createIterator(fef::TermFieldMatchData* matchData, bool strict) override;
    unsigned int approximateHits() const override;
    void fetchPostings(bool strict) override;
    bool valid() const override;
    Int64Range getAsIntegerTerm() const override;
    const QueryTermBase& queryTerm() const override;
    const vespalib::string& attributeName() const override;

    using DocId = IAttributeVector::DocId;

    bool cmp(DocId docId, int32_t& weight) const {
        return _target_search_context->cmp(getTargetLid(docId), weight);
    }

    bool cmp(DocId docId) const {
        return _target_search_context->cmp(getTargetLid(docId));
    }

    bool onCmp(uint32_t docId, int32_t &weight) const override { return cmp(docId, weight); }
    bool onCmp(uint32_t docId) const override { return cmp(docId); }

    const ReferenceAttribute& attribute() const noexcept { return _reference_attribute; }

    const ISearchContext &target_search_context() const noexcept {
        return *_target_search_context;
    }
};

}