aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src
diff options
context:
space:
mode:
authorHÃ¥vard Pettersen <3535158+havardpe@users.noreply.github.com>2021-05-20 10:14:00 +0200
committerGitHub <noreply@github.com>2021-05-20 10:14:00 +0200
commitc8746c75e6ad4dad17ecd3daf2916fe45fd594f9 (patch)
tree394c82dd3da28d65e1d567d65ce80f1399c9f006 /searchlib/src
parentfc0711f7870b55ea77d18d87ec3e70b75e0de2e0 (diff)
parent58c6c0405e588908df90801e69ed7931ddc577e9 (diff)
Merge pull request #17885 from vespa-engine/havardpe/external-ranking-expressions
external ranking expressions
Diffstat (limited to 'searchlib/src')
-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;
};