summaryrefslogtreecommitdiffstats
path: root/searchsummary/src/vespa/searchsummary/docsummary/docsum_field_writer_factory.cpp
blob: cebfac4c86d0709688caa858b3577e4d676d43b1 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "docsum_field_writer_factory.h"
#include "attribute_combiner_dfw.h"
#include "copy_dfw.h"
#include "document_id_dfw.h"
#include "empty_dfw.h"
#include "geoposdfw.h"
#include "idocsumenvironment.h"
#include "juniperdfw.h"
#include "matched_elements_filter_dfw.h"
#include "positionsdfw.h"
#include "rankfeaturesdfw.h"
#include "summaryfeaturesdfw.h"
#include <vespa/searchlib/common/matching_elements_fields.h>
#include <vespa/vespalib/util/exceptions.h>

using vespalib::IllegalArgumentException;

namespace search::docsummary {

DocsumFieldWriterFactory::DocsumFieldWriterFactory(bool use_v8_geo_positions, IDocsumEnvironment& env)
    : _use_v8_geo_positions(use_v8_geo_positions),
      _env(env),
      _matching_elems_fields(std::make_shared<MatchingElementsFields>())
{
}

DocsumFieldWriterFactory::~DocsumFieldWriterFactory() = default;

bool
DocsumFieldWriterFactory::has_attribute_manager() const noexcept
{
    return getEnvironment().getAttributeManager() != nullptr;
}

std::unique_ptr<DocsumFieldWriter>
DocsumFieldWriterFactory::create_docsum_field_writer(const vespalib::string& fieldName, const vespalib::string& overrideName, const vespalib::string& argument, bool& rc)
{
    rc = false;
    std::unique_ptr<DocsumFieldWriter> fieldWriter;
    if (overrideName == "dynamicteaser") {
        if ( ! argument.empty() ) {
            auto fw = std::make_unique<DynamicTeaserDFW>(getEnvironment().getJuniper());
            auto fw_ptr = fw.get();
            fieldWriter = std::move(fw);
            rc = fw_ptr->Init(fieldName.c_str(), argument);
        } else {
            throw IllegalArgumentException("Missing argument");
        }
    } else if (overrideName == "summaryfeatures") {
        fieldWriter = std::make_unique<SummaryFeaturesDFW>();
        rc = true;
    } else if (overrideName == "rankfeatures") {
        fieldWriter = std::make_unique<RankFeaturesDFW>();
        rc = true;
    } else if (overrideName == "empty") {
        fieldWriter = std::make_unique<EmptyDFW>();
        rc = true;
    } else if (overrideName == "copy") {
        if ( ! argument.empty() ) {
            fieldWriter = std::make_unique<CopyDFW>(argument);
            rc = true;
        } else {
            throw IllegalArgumentException("Missing argument");
        }
    } else if (overrideName == "absdist") {
        if (has_attribute_manager()) {
            fieldWriter = AbsDistanceDFW::create(argument.c_str(), getEnvironment().getAttributeManager());
            rc = static_cast<bool>(fieldWriter);
        }
    } else if (overrideName == "positions") {
        if (has_attribute_manager()) {
            fieldWriter = PositionsDFW::create(argument.c_str(), getEnvironment().getAttributeManager(), _use_v8_geo_positions);
            rc = static_cast<bool>(fieldWriter);
        }
    } else if (overrideName == "geopos") {
        if (has_attribute_manager()) {
            fieldWriter = GeoPositionDFW::create(argument.c_str(), getEnvironment().getAttributeManager(), _use_v8_geo_positions);
            rc = static_cast<bool>(fieldWriter);
        }
    } else if (overrideName == "attribute") {
        if (has_attribute_manager()) {
            fieldWriter = AttributeDFWFactory::create(*getEnvironment().getAttributeManager(), argument);
            rc = true; // Allow missing attribute vector
        }
    } else if (overrideName == "attributecombiner") {
        if (has_attribute_manager()) {
            auto attr_ctx = getEnvironment().getAttributeManager()->createContext();
            const vespalib::string& source_field = argument.empty() ? fieldName : argument;
            fieldWriter = AttributeCombinerDFW::create(source_field, *attr_ctx, false, std::shared_ptr<MatchingElementsFields>());
            rc = static_cast<bool>(fieldWriter);
        }
    } else if (overrideName == "matchedattributeelementsfilter") {
        const vespalib::string& source_field = argument.empty() ? fieldName : argument;
        if (has_attribute_manager()) {
            auto attr_ctx = getEnvironment().getAttributeManager()->createContext();
            if (attr_ctx->getAttribute(source_field) != nullptr) {
                fieldWriter = AttributeDFWFactory::create(*getEnvironment().getAttributeManager(), source_field, true, _matching_elems_fields);
            } else {
                fieldWriter = AttributeCombinerDFW::create(source_field, *attr_ctx, true, _matching_elems_fields);
            }
            rc = static_cast<bool>(fieldWriter);
        }
    } else if (overrideName == "matchedelementsfilter") {
        const vespalib::string& source_field = argument.empty() ? fieldName : argument;
        if (has_attribute_manager()) {
            auto attr_ctx = getEnvironment().getAttributeManager()->createContext();
            fieldWriter = MatchedElementsFilterDFW::create(source_field,*attr_ctx, _matching_elems_fields);
            rc = static_cast<bool>(fieldWriter);
        }
    } else if (overrideName == "documentid") {
        fieldWriter = std::make_unique<DocumentIdDFW>();
        rc = true;
    } else {
        throw IllegalArgumentException("unknown override operation '" + overrideName + "' for field '" + fieldName + "'.");
    }
    return fieldWriter;
}

}