-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(search): Hnsw Vector Search Plan Operator & Executor (#2434)
Co-authored-by: Twice <twice.mliu@gmail.com>
- Loading branch information
1 parent
45ba475
commit 79a740c
Showing
9 changed files
with
634 additions
and
253 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
src/search/executors/hnsw_vector_field_knn_scan_executor.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
|
||
#include "db_util.h" | ||
#include "encoding.h" | ||
#include "search/hnsw_indexer.h" | ||
#include "search/plan_executor.h" | ||
#include "search/search_encoding.h" | ||
#include "storage/redis_db.h" | ||
#include "storage/redis_metadata.h" | ||
#include "storage/storage.h" | ||
#include "string_util.h" | ||
|
||
namespace kqir { | ||
|
||
// TODO(Beihao): Add DB context to improve consistency and isolation - see #2332 | ||
struct HnswVectorFieldKnnScanExecutor : ExecutorNode { | ||
HnswVectorFieldKnnScan *scan; | ||
redis::LatestSnapShot ss; | ||
bool initialized = false; | ||
|
||
IndexInfo *index; | ||
redis::SearchKey search_key; | ||
redis::HnswVectorFieldMetadata field_metadata; | ||
redis::HnswIndex hnsw_index; | ||
std::vector<redis::KeyWithDistance> row_keys; | ||
decltype(row_keys)::iterator row_keys_iter; | ||
|
||
HnswVectorFieldKnnScanExecutor(ExecutorContext *ctx, HnswVectorFieldKnnScan *scan) | ||
: ExecutorNode(ctx), | ||
scan(scan), | ||
ss(ctx->storage), | ||
index(scan->field->info->index), | ||
search_key(index->ns, index->name, scan->field->name), | ||
field_metadata(*(scan->field->info->MetadataAs<redis::HnswVectorFieldMetadata>())), | ||
hnsw_index(redis::HnswIndex(search_key, &field_metadata, ctx->storage)) {} | ||
|
||
StatusOr<Result> Next() override { | ||
if (!initialized) { | ||
row_keys = GET_OR_RET(hnsw_index.KnnSearch(scan->vector, scan->k)); | ||
row_keys_iter = row_keys.begin(); | ||
initialized = true; | ||
} | ||
|
||
if (row_keys_iter == row_keys.end()) { | ||
return end; | ||
} | ||
|
||
auto key_str = row_keys_iter->second; | ||
row_keys_iter++; | ||
return RowType{key_str, {}, scan->field->info->index}; | ||
} | ||
}; | ||
|
||
} // namespace kqir |
86 changes: 86 additions & 0 deletions
86
src/search/executors/hnsw_vector_field_range_scan_executor.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
|
||
#include "db_util.h" | ||
#include "encoding.h" | ||
#include "search/hnsw_indexer.h" | ||
#include "search/plan_executor.h" | ||
#include "search/search_encoding.h" | ||
#include "storage/redis_db.h" | ||
#include "storage/redis_metadata.h" | ||
#include "storage/storage.h" | ||
#include "string_util.h" | ||
|
||
namespace kqir { | ||
|
||
// TODO(Beihao): Add DB context to improve consistency and isolation - see #2332 | ||
struct HnswVectorFieldRangeScanExecutor : ExecutorNode { | ||
HnswVectorFieldRangeScan *scan; | ||
redis::LatestSnapShot ss; | ||
bool initialized = false; | ||
|
||
IndexInfo *index; | ||
redis::SearchKey search_key; | ||
redis::HnswVectorFieldMetadata field_metadata; | ||
redis::HnswIndex hnsw_index; | ||
std::vector<redis::KeyWithDistance> row_keys; | ||
std::unordered_set<std::string> visited; | ||
decltype(row_keys)::iterator row_keys_iter; | ||
|
||
HnswVectorFieldRangeScanExecutor(ExecutorContext *ctx, HnswVectorFieldRangeScan *scan) | ||
: ExecutorNode(ctx), | ||
scan(scan), | ||
ss(ctx->storage), | ||
index(scan->field->info->index), | ||
search_key(index->ns, index->name, scan->field->name), | ||
field_metadata(*(scan->field->info->MetadataAs<redis::HnswVectorFieldMetadata>())), | ||
hnsw_index(redis::HnswIndex(search_key, &field_metadata, ctx->storage)) {} | ||
|
||
StatusOr<Result> Next() override { | ||
if (!initialized) { | ||
row_keys = GET_OR_RET(hnsw_index.KnnSearch(scan->vector, field_metadata.ef_runtime)); | ||
row_keys_iter = row_keys.begin(); | ||
initialized = true; | ||
} | ||
|
||
auto effective_range = scan->range * (1 + field_metadata.epsilon); | ||
if (row_keys_iter == row_keys.end() || row_keys_iter->first > abs(effective_range) || | ||
row_keys_iter->first < -abs(effective_range)) { | ||
row_keys = GET_OR_RET(hnsw_index.ExpandSearchScope(scan->vector, std::move(row_keys), visited)); | ||
if (row_keys.empty()) return end; | ||
row_keys_iter = row_keys.begin(); | ||
} | ||
|
||
if (row_keys_iter->first > abs(effective_range) || row_keys_iter->first < -abs(effective_range)) { | ||
return end; | ||
} | ||
|
||
auto key_str = row_keys_iter->second; | ||
row_keys_iter++; | ||
visited.insert(key_str); | ||
return RowType{key_str, {}, scan->field->info->index}; | ||
} | ||
}; | ||
|
||
} // namespace kqir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.