aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/server/searchview.cpp
blob: 9830048cdd8939c8243223f9cb7bf5acc42a5f4b (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "searchview.h"
#include <vespa/searchcore/proton/docsummary/docsumcontext.h>
#include <vespa/searchlib/engine/searchreply.h>
#include <vespa/log/log.h>
LOG_SETUP(".proton.server.searchview");

using proton::matching::MatchContext;
using search::AttributeGuard;
using search::AttributeVector;
using search::attribute::IAttributeContext;
using search::docsummary::IDocsumStore;
using search::docsummary::ResultConfig;
using search::engine::DocsumReply;
using search::engine::DocsumRequest;
using search::engine::SearchReply;
using vespalib::ThreadBundle;

namespace proton {

using matching::ISearchContext;
using matching::Matcher;

namespace {

/**
 * Maps the gids in the request to lids using the given document meta store.
 * A reader guard must be taken before calling this function.
 **/
void
convertGidsToLids(const DocsumRequest & request,
                  const search::IDocumentMetaStore &metaStore,
                  uint32_t docIdLimit)
{
    document::GlobalId empty;
    uint32_t lid = 0;
    for (size_t i = 0; i < request.hits.size(); ++i) {
        const DocsumRequest::Hit & h = request.hits[i];
        if (metaStore.getLid(h.gid, lid) && lid < docIdLimit) {
            h.docid = lid;
        } else {
            h.docid = search::endDocId;
            LOG(debug, "Document with global id '%s' is not in the document db, will return empty docsum", h.gid.toString().c_str());
        }
        LOG(spam, "convertGidToLid(DocsumRequest): hit[%zu]: gid(%s) -> lid(%u)", i, h.gid.toString().c_str(), h.docid);
    }
}

bool
requestHasLidAbove(const DocsumRequest & request, uint32_t docIdLimit)
{
    for (const DocsumRequest::Hit & h : request.hits) {
        if (h.docid >= docIdLimit) {
            return true;
        }
    }
    return false;
}

bool
hasAnyLidsMoved(const DocsumRequest & request,
                const search::IDocumentMetaStore &metaStore)
{
    for (const DocsumRequest::Hit & h : request.hits) {
        uint32_t lid = 0;
        if (h.docid != search::endDocId) {
            if (!metaStore.getLid(h.gid, lid) || (lid != h.docid)) {
                LOG(debug, "lid = %d moved to %d", h.docid, lid);
                return true;
            }
        }
    }
    return false;
}

/**
 * Maps the lids in the reply to gids using the original request.
 **/
void
convertLidsToGids(DocsumReply &reply, const DocsumRequest &request)
{
    LOG_ASSERT(reply.docsums.size() == request.hits.size());
    for (size_t i = 0; i < reply.docsums.size(); ++i) {
        const DocsumRequest::Hit & h = request.hits[i];
        DocsumReply::Docsum & d = reply.docsums[i];
        d.gid = h.gid;
        LOG(spam, "convertLidToGid(DocsumReply): docsum[%zu]: lid(%u) -> gid(%s)", i, d.docid, d.gid.toString().c_str());
    }
}

/**
 * Create empty docsum reply
 **/
DocsumReply::UP
createEmptyReply(const DocsumRequest & request)
{
    auto reply = std::make_unique<DocsumReply>();
    for (size_t i = 0; i < request.hits.size(); ++i) {
        reply->docsums.push_back(DocsumReply::Docsum());
        reply->docsums.back().gid = request.hits[i].gid;
    }
    return reply;
}

}

SearchView::SearchView(const ISummaryManager::ISummarySetup::SP & summarySetup,
                       const MatchView::SP & matchView)
    : ISearchHandler(),
      _summarySetup(summarySetup),
      _matchView(matchView)
{ }

SearchView::~SearchView() = default;

DocsumReply::UP
SearchView::getDocsums(const DocsumRequest & req)
{
    LOG(spam, "getDocsums(): resultClass(%s), numHits(%zu)", req.resultClassName.c_str(), req.hits.size());
    if (_summarySetup->getResultConfig().  LookupResultClassId(req.resultClassName.c_str()) == ResultConfig::NoClassID()) {
        LOG(warning, "There is no summary class with name '%s' in the summary config. Returning empty document summary for %zu hit(s)",
                     req.resultClassName.c_str(), req.hits.size());
        return createEmptyReply(req);
    }
    SearchView::InternalDocsumReply reply = getDocsumsInternal(req);
    while ( ! reply.second ) {
        LOG(debug, "Must refetch docsums since the lids have moved.");
        reply = getDocsumsInternal(req);
    }
    if ( ! req.useRootSlime()) {
        convertLidsToGids(*reply.first, req);
    }
    return std::move(reply.first);
}

SearchView::InternalDocsumReply
SearchView::getDocsumsInternal(const DocsumRequest & req)
{
    IDocumentMetaStoreContext::IReadGuard::UP readGuard = _matchView->getDocumentMetaStore()->getReadGuard();
    const search::IDocumentMetaStore & metaStore = readGuard->get();
    uint32_t numUsedLids = metaStore.getNumUsedLids();
    uint64_t startGeneration = readGuard->get().getCurrentGeneration();

    convertGidsToLids(req, metaStore, _matchView->getDocIdLimit().get());
    IDocsumStore::UP store(_summarySetup->createDocsumStore(req.resultClassName));
    MatchContext::UP mctx = _matchView->createContext();
    auto ctx = std::make_unique<DocsumContext>(req, _summarySetup->getDocsumWriter(), *store, _matchView->getMatcher(req.ranking),
                                               mctx->getSearchContext(), mctx->getAttributeContext(),
                                               *_summarySetup->getAttributeManager(), *getSessionManager());
    SearchView::InternalDocsumReply reply(ctx->getDocsums(), true);
    uint64_t endGeneration = readGuard->get().getCurrentGeneration();
    if (startGeneration != endGeneration) {
        if (requestHasLidAbove(req, std::min(numUsedLids, metaStore.getNumUsedLids()))) {
            if (hasAnyLidsMoved(req, metaStore)) {
                reply.second = false;
            }
        }
    }
    return reply;
}

std::unique_ptr<SearchReply>
SearchView::match(const ISearchHandler::SP &self, const SearchRequest &req, ThreadBundle &threadBundle) const {
    return _matchView->match(self, req, threadBundle);
}

} // namespace proton