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

#include "fake_search.h"
#include <vespa/searchlib/fef/termfieldmatchdataposition.h>
#include <vespa/searchlib/fef/termfieldmatchdata.h>
#include <vespa/vespalib/objects/visit.h>
#include <vespa/searchcommon/attribute/i_search_context.h>
#include <cassert>

namespace search::queryeval {

FakeSearch::FakeSearch(const vespalib::string &tag, const vespalib::string &field,
                       const vespalib::string &term, const FakeResult &res,
                       fef::TermFieldMatchDataArray tfmda)
    : _tag(tag), _field(field), _term(term),
      _result(res), _offset(0), _tfmda(std::move(tfmda)),
      _ctx(nullptr)
{
    assert(_tfmda.size() == 1);
}

void
FakeSearch::doSeek(uint32_t docid)
{
    while (valid() && docid > currId()) {
        next();
    }
    if (valid()) {
        setDocId(currId());
    } else {
        setAtEnd();
    }
}

void
FakeSearch::doUnpack(uint32_t docid)
{
    using PosCtx = fef::TermFieldMatchDataPosition;
    using Doc = FakeResult::Document;
    using Elem = FakeResult::Element;

    assert(valid());
    const Doc &doc = _result.inspect()[_offset];
    assert(doc.docId == docid);
    _tfmda[0]->reset(docid);
    int32_t sum_weight = 0;
    for (uint32_t i = 0; i < doc.elements.size(); ++i) {
        const Elem &elem = doc.elements[i];
        sum_weight += elem.weight;
        if (!is_attr()) {
            for (uint32_t j = 0; j < elem.positions.size(); ++j) {
                _tfmda[0]->appendPosition(PosCtx(elem.id, elem.positions[j],
                                elem.weight, elem.length));
            }
        }
    }
    if (is_attr()) {
        _tfmda[0]->appendPosition(PosCtx(0, 0, sum_weight, 1));
    }
    if (_tfmda[0]->needs_interleaved_features()) {
        _tfmda[0]->setNumOccs(doc.num_occs);
        _tfmda[0]->setFieldLength(doc.field_length);
    }
}

void
FakeSearch::visitMembers(vespalib::ObjectVisitor &visitor) const
{
    visit(visitor, "tag",   _tag);
    visit(visitor, "field", _field);
    visit(visitor, "term",  _term);
}

}