aboutsummaryrefslogtreecommitdiffstats
path: root/searchcommon
diff options
context:
space:
mode:
authorGeir Storli <geirst@verizonmedia.com>2020-05-12 15:52:15 +0000
committerGeir Storli <geirst@verizonmedia.com>2020-05-12 15:52:15 +0000
commitfe5a4c73f70cc451f461bc007d1ce7d32481edb2 (patch)
treebd8007d72484c81f11d43ca4a3ec0003f74d0bec /searchcommon
parent2d5ddf827dcef2af9619f8a9fee148f9edd12b02 (diff)
Support specifying a distance metric for nearest neighbor search when not having a hnsw index.
This also changes the syntax in the sd file to specifying the distance metric in the attribute tag.
Diffstat (limited to 'searchcommon')
-rw-r--r--searchcommon/src/vespa/searchcommon/attribute/config.cpp3
-rw-r--r--searchcommon/src/vespa/searchcommon/attribute/config.h8
-rw-r--r--searchcommon/src/vespa/searchcommon/attribute/hnsw_index_params.h1
3 files changed, 12 insertions, 0 deletions
diff --git a/searchcommon/src/vespa/searchcommon/attribute/config.cpp b/searchcommon/src/vespa/searchcommon/attribute/config.cpp
index b4e05875820..7752baa603a 100644
--- a/searchcommon/src/vespa/searchcommon/attribute/config.cpp
+++ b/searchcommon/src/vespa/searchcommon/attribute/config.cpp
@@ -18,6 +18,7 @@ Config::Config() :
_compactionStrategy(),
_predicateParams(),
_tensorType(vespalib::eval::ValueType::error_type()),
+ _distance_metric(DistanceMetric::Euclidean),
_hnsw_index_params()
{
}
@@ -36,6 +37,7 @@ Config::Config(BasicType bt, CollectionType ct, bool fastSearch_, bool huge_)
_compactionStrategy(),
_predicateParams(),
_tensorType(vespalib::eval::ValueType::error_type()),
+ _distance_metric(DistanceMetric::Euclidean),
_hnsw_index_params()
{
}
@@ -63,6 +65,7 @@ Config::operator==(const Config &b) const
_predicateParams == b._predicateParams &&
(_basicType.type() != BasicType::Type::TENSOR ||
_tensorType == b._tensorType) &&
+ _distance_metric == b._distance_metric &&
_hnsw_index_params == b._hnsw_index_params;
}
diff --git a/searchcommon/src/vespa/searchcommon/attribute/config.h b/searchcommon/src/vespa/searchcommon/attribute/config.h
index df5aa9e217a..822a4e4e028 100644
--- a/searchcommon/src/vespa/searchcommon/attribute/config.h
+++ b/searchcommon/src/vespa/searchcommon/attribute/config.h
@@ -9,6 +9,7 @@
#include <vespa/searchcommon/common/compaction_strategy.h>
#include <vespa/searchcommon/common/growstrategy.h>
#include <vespa/eval/eval/value_type.h>
+#include <cassert>
#include <optional>
namespace search::attribute {
@@ -35,6 +36,7 @@ public:
bool huge() const { return _huge; }
const PredicateParams &predicateParams() const { return _predicateParams; }
vespalib::eval::ValueType tensorType() const { return _tensorType; }
+ DistanceMetric distance_metric() const { return _distance_metric; }
const std::optional<HnswIndexParams>& hnsw_index_params() const { return _hnsw_index_params; }
/**
@@ -67,7 +69,12 @@ public:
_tensorType = tensorType_in;
return *this;
}
+ Config& set_distance_metric(DistanceMetric value) {
+ _distance_metric = value;
+ return *this;
+ }
Config& set_hnsw_index_params(const HnswIndexParams& params) {
+ assert(_distance_metric == params.distance_metric());
_hnsw_index_params = params;
return *this;
}
@@ -122,6 +129,7 @@ private:
CompactionStrategy _compactionStrategy;
PredicateParams _predicateParams;
vespalib::eval::ValueType _tensorType;
+ DistanceMetric _distance_metric;
std::optional<HnswIndexParams> _hnsw_index_params;
};
diff --git a/searchcommon/src/vespa/searchcommon/attribute/hnsw_index_params.h b/searchcommon/src/vespa/searchcommon/attribute/hnsw_index_params.h
index 94d3fda49f3..3e3683ce60f 100644
--- a/searchcommon/src/vespa/searchcommon/attribute/hnsw_index_params.h
+++ b/searchcommon/src/vespa/searchcommon/attribute/hnsw_index_params.h
@@ -14,6 +14,7 @@ class HnswIndexParams {
private:
uint32_t _max_links_per_node;
uint32_t _neighbors_to_explore_at_insert;
+ // This is always the same as in the attribute config, and is duplicated here to simplify usage.
DistanceMetric _distance_metric;
public: