|
|
@@ -7,16 +7,11 @@
|
|
|
#include "project_settings.hpp"
|
|
|
#include "json_http.hpp"
|
|
|
#include "server.hpp"
|
|
|
+#include "vector_search.hpp"
|
|
|
#include <algorithm>
|
|
|
-#include <unordered_set>
|
|
|
namespace svapi {
|
|
|
namespace {
|
|
|
|
|
|
-// Upper bound on how deep a filtered search will scan the similarity ranking,
|
|
|
-// and on how many filter-matching ids we fetch. A filtered search stays exact
|
|
|
-// while the collection is within this bound.
|
|
|
-constexpr uint32_t kFilterScanCap = 10000;
|
|
|
-
|
|
|
// Parse the optional `filters` body field into DB filters. Same grammar and
|
|
|
// operator set as the documents endpoint, because the DB evaluates them.
|
|
|
std::vector<smartbotic::database::Client::Filter> parseSearchFilters(const nlohmann::json& body) {
|
|
|
@@ -116,44 +111,8 @@ void registerVectorRoutes(ApiServer& s) {
|
|
|
auto filters = parseSearchFilters(body);
|
|
|
const std::string coll = qualify(project, name);
|
|
|
|
|
|
- std::vector<smartbotic::database::Client::SimilarityResult> results;
|
|
|
- if (filters.empty()) {
|
|
|
- results = d->db.client().similaritySearch(coll, q, topK, minScore);
|
|
|
- } else {
|
|
|
- // The DB evaluates the filters (same engine as /documents), so every
|
|
|
- // operator behaves identically on both endpoints. It will not return
|
|
|
- // the stored vectors, so scoring still has to come from
|
|
|
- // similaritySearch - we intersect the two.
|
|
|
- smartbotic::database::Client::QueryOptions opts;
|
|
|
- opts.filters = filters;
|
|
|
- opts.limit = kFilterScanCap;
|
|
|
- std::unordered_set<std::string> allowed;
|
|
|
- for (const auto& doc : d->db.client().find(coll, opts)) {
|
|
|
- const std::string id = doc.value("_id", "");
|
|
|
- if (!id.empty()) allowed.insert(id);
|
|
|
- }
|
|
|
- if (!allowed.empty()) {
|
|
|
- // Walk the ranking from the top, widening until topK matches are
|
|
|
- // found or the collection is exhausted. Because the scan is in
|
|
|
- // descending score order, stopping as soon as topK matches are
|
|
|
- // collected still yields the true topK: anything unseen scores
|
|
|
- // lower than everything already kept.
|
|
|
- uint32_t scan = std::max<uint32_t>(topK * 4, 64);
|
|
|
- while (true) {
|
|
|
- auto page = d->db.client().similaritySearch(coll, q, scan, minScore);
|
|
|
- results.clear();
|
|
|
- for (const auto& r : page) {
|
|
|
- if (!allowed.count(r.id)) continue;
|
|
|
- results.push_back(r);
|
|
|
- if (results.size() >= topK) break;
|
|
|
- }
|
|
|
- if (results.size() >= topK) break; // enough matches
|
|
|
- if (page.size() < scan) break; // ranking exhausted
|
|
|
- if (scan >= kFilterScanCap) break; // bounded work
|
|
|
- scan = std::min(scan * 4, kFilterScanCap);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ const std::vector<smartbotic::database::Client::SimilarityResult> results =
|
|
|
+ vectorSearch(d->db.client(), coll, q, topK, minScore, filters);
|
|
|
|
|
|
nlohmann::json arr = nlohmann::json::array();
|
|
|
for (const auto& r : results) arr.push_back({{"id", r.id}, {"score", r.score}, {"data", r.data}});
|