aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib
diff options
context:
space:
mode:
authorHåvard Pettersen <havardpe@oath.com>2021-05-12 10:12:27 +0000
committerHåvard Pettersen <havardpe@oath.com>2021-05-18 15:54:24 +0000
commitcb3035a72c42053db2130084209e611f49d9b037 (patch)
tree7c0a22bc6e9cb21061a1ac66d662d95d3ae8db19 /searchlib
parente0107a0d6c6dbd829bfd8a72c3d5f685bda0fb5c (diff)
external ranking expressions
loaded from potentially compressed files
Diffstat (limited to 'searchlib')
-rw-r--r--searchlib/src/tests/features/ranking_expression/ranking_expression_test.cpp29
-rw-r--r--searchlib/src/vespa/searchlib/features/rankingexpressionfeature.cpp5
-rw-r--r--searchlib/src/vespa/searchlib/fef/iindexenvironment.h5
-rw-r--r--searchlib/src/vespa/searchlib/fef/test/indexenvironment.cpp16
-rw-r--r--searchlib/src/vespa/searchlib/fef/test/indexenvironment.h5
5 files changed, 53 insertions, 7 deletions
diff --git a/searchlib/src/tests/features/ranking_expression/ranking_expression_test.cpp b/searchlib/src/tests/features/ranking_expression/ranking_expression_test.cpp
index 251040ecfa7..6baa6581edf 100644
--- a/searchlib/src/tests/features/ranking_expression/ranking_expression_test.cpp
+++ b/searchlib/src/tests/features/ranking_expression/ranking_expression_test.cpp
@@ -67,28 +67,38 @@ struct SetupResult {
RankingExpressionBlueprint rank;
DummyDependencyHandler deps;
bool setup_ok;
- SetupResult(const TypeMap &object_inputs, const vespalib::string &expression);
+ SetupResult(const TypeMap &object_inputs, const vespalib::string &expression,
+ bool external_expression = false);
~SetupResult();
};
SetupResult::SetupResult(const TypeMap &object_inputs,
- const vespalib::string &expression)
+ const vespalib::string &expression,
+ bool external_expression)
: stash(), index_env(), query_env(&index_env), rank(make_replacer()), deps(rank), setup_ok(false)
{
rank.setName("self");
- index_env.getProperties().add("self.rankingScript", expression);
for (const auto &input: object_inputs) {
deps.define_object_input(input.first, ValueType::from_spec(input.second));
}
- setup_ok = rank.setup(index_env, {});
+ std::vector<vespalib::string> params;
+ if (external_expression) {
+ params.push_back("my_expr");
+ index_env.addRankingExpression("my_expr", expression);
+ } else {
+ index_env.getProperties().add("self.rankingScript", expression);
+ }
+ Blueprint &bp = rank;
+ setup_ok = bp.setup(index_env, params);
EXPECT_TRUE(!deps.accept_type_mismatch);
}
SetupResult::~SetupResult() = default;
void verify_output_type(const TypeMap &object_inputs,
- const vespalib::string &expression, const FeatureType &expect)
+ const vespalib::string &expression, const FeatureType &expect,
+ bool external_expression = false)
{
- SetupResult result(object_inputs, expression);
+ SetupResult result(object_inputs, expression, external_expression);
EXPECT_TRUE(result.setup_ok);
EXPECT_EQUAL(1u, result.deps.output.size());
ASSERT_EQUAL(1u, result.deps.output_type.size());
@@ -126,6 +136,13 @@ TEST("require that ranking expression can resolve to concrete complex type") {
FeatureType::object(ValueType::from_spec("tensor(x{},y{},z{})"))));
}
+TEST("require that ranking expression can be external") {
+ TEST_DO(verify_output_type({}, "a*b", FeatureType::number(), true));
+ TEST_DO(verify_output_type({{"b", "double"}}, "a*b", FeatureType::object(ValueType::double_type()), true));
+ TEST_DO(verify_output_type({{"a", "tensor(x{},y{})"}, {"b", "tensor(y{},z{})"}}, "a*b",
+ FeatureType::object(ValueType::from_spec("tensor(x{},y{},z{})")), true));
+}
+
TEST("require that setup fails for incompatible types") {
TEST_DO(verify_setup_fail({{"a", "tensor(x{},y{})"}, {"b", "tensor(y[10],z{})"}}, "a*b"));
}
diff --git a/searchlib/src/vespa/searchlib/features/rankingexpressionfeature.cpp b/searchlib/src/vespa/searchlib/features/rankingexpressionfeature.cpp
index 8cba6013627..070d70997e6 100644
--- a/searchlib/src/vespa/searchlib/features/rankingexpressionfeature.cpp
+++ b/searchlib/src/vespa/searchlib/features/rankingexpressionfeature.cpp
@@ -250,7 +250,10 @@ RankingExpressionBlueprint::setup(const fef::IIndexEnvironment &env,
}
//LOG(debug, "Script from config: '%s'\n", script.c_str());
} else if (params.size() == 1) {
- script = params[0].getValue();
+ script = env.getRankingExpression(params[0].getValue());
+ if (script.empty()) {
+ script = params[0].getValue();
+ }
//LOG(debug, "Script from param: '%s'\n", script.c_str());
} else {
return fail("No expression given.");
diff --git a/searchlib/src/vespa/searchlib/fef/iindexenvironment.h b/searchlib/src/vespa/searchlib/fef/iindexenvironment.h
index 0cd017d8ffa..a01d7d8762b 100644
--- a/searchlib/src/vespa/searchlib/fef/iindexenvironment.h
+++ b/searchlib/src/vespa/searchlib/fef/iindexenvironment.h
@@ -123,6 +123,11 @@ public:
virtual std::unique_ptr<vespalib::eval::ConstantValue> getConstantValue(const vespalib::string &name) const = 0;
/**
+ * Returns the ranking expression with the given name or empty string if not found.
+ **/
+ virtual vespalib::string getRankingExpression(const vespalib::string &name) const = 0;
+
+ /**
* Get configuration for the given onnx model.
**/
virtual const OnnxModel *getOnnxModel(const vespalib::string &name) const = 0;
diff --git a/searchlib/src/vespa/searchlib/fef/test/indexenvironment.cpp b/searchlib/src/vespa/searchlib/fef/test/indexenvironment.cpp
index d2d336dcdc8..85b20710f7a 100644
--- a/searchlib/src/vespa/searchlib/fef/test/indexenvironment.cpp
+++ b/searchlib/src/vespa/searchlib/fef/test/indexenvironment.cpp
@@ -54,6 +54,22 @@ IndexEnvironment::addConstantValue(const vespalib::string &name,
(void) insertRes;
}
+vespalib::string
+IndexEnvironment::getRankingExpression(const vespalib::string &name) const
+{
+ auto pos = _expressions.find(name);
+ if (pos != _expressions.end()) {
+ return pos->second;
+ }
+ return {};
+}
+
+void
+IndexEnvironment::addRankingExpression(const vespalib::string &name, const vespalib::string &value)
+{
+ _expressions.insert_or_assign(name, value);
+}
+
const OnnxModel *
IndexEnvironment::getOnnxModel(const vespalib::string &name) const
{
diff --git a/searchlib/src/vespa/searchlib/fef/test/indexenvironment.h b/searchlib/src/vespa/searchlib/fef/test/indexenvironment.h
index 0d8d0091921..ca63117971a 100644
--- a/searchlib/src/vespa/searchlib/fef/test/indexenvironment.h
+++ b/searchlib/src/vespa/searchlib/fef/test/indexenvironment.h
@@ -48,6 +48,7 @@ public:
};
using ConstantsMap = std::map<vespalib::string, Constant>;
+ using ExprMap = std::map<vespalib::string, vespalib::string>;
using ModelMap = std::map<vespalib::string, OnnxModel>;
IndexEnvironment();
@@ -85,6 +86,9 @@ public:
vespalib::eval::ValueType type,
std::unique_ptr<vespalib::eval::Value> value);
+ vespalib::string getRankingExpression(const vespalib::string &name) const override;
+ void addRankingExpression(const vespalib::string &name, const vespalib::string &value);
+
const OnnxModel *getOnnxModel(const vespalib::string &name) const override;
void addOnnxModel(const OnnxModel &model);
@@ -98,6 +102,7 @@ private:
AttributeMap _attrMap;
TableManager _tableMan;
ConstantsMap _constants;
+ ExprMap _expressions;
ModelMap _models;
};