Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Tokenizer support build with params and clone for concurrency #37048

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions internal/core/src/common/FieldMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,9 @@ TokenizerParams
ParseTokenizerParams(const TypeParams& params) {
auto iter = params.find("tokenizer_params");
if (iter == params.end()) {
return {};
return "{}";
}
nlohmann::json j = nlohmann::json::parse(iter->second);
std::map<std::string, std::string> ret;
for (const auto& [k, v] : j.items()) {
try {
ret[k] = v.get<std::string>();
} catch (std::exception& e) {
ret[k] = v.dump();
}
}
return ret;
return iter->second;
}

bool
Expand Down
2 changes: 1 addition & 1 deletion internal/core/src/common/FieldMeta.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

namespace milvus {
using TypeParams = std::map<std::string, std::string>;
using TokenizerParams = std::map<std::string, std::string>;
using TokenizerParams = std::string;

TokenizerParams
ParseTokenizerParams(const TypeParams& params);
Expand Down
26 changes: 11 additions & 15 deletions internal/core/src/index/TextMatchIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
namespace milvus::index {
constexpr const char* TMP_TEXT_LOG_PREFIX = "/tmp/milvus/text-log/";

TextMatchIndex::TextMatchIndex(
int64_t commit_interval_in_ms,
const char* tokenizer_name,
const std::map<std::string, std::string>& tokenizer_params)
TextMatchIndex::TextMatchIndex(int64_t commit_interval_in_ms,
aoiasd marked this conversation as resolved.
Show resolved Hide resolved
const char* tokenizer_name,
const char* tokenizer_params)
: commit_interval_in_ms_(commit_interval_in_ms),
last_commit_time_(stdclock::now()) {
d_type_ = TantivyDataType::Text;
Expand All @@ -31,10 +30,9 @@
field_name.c_str(), true, "", tokenizer_name, tokenizer_params);
}

TextMatchIndex::TextMatchIndex(
const std::string& path,
const char* tokenizer_name,
const std::map<std::string, std::string>& tokenizer_params)
TextMatchIndex::TextMatchIndex(const std::string& path,

Check warning on line 33 in internal/core/src/index/TextMatchIndex.cpp

View check run for this annotation

Codecov / codecov/patch

internal/core/src/index/TextMatchIndex.cpp#L33

Added line #L33 was not covered by tests
const char* tokenizer_name,
const char* tokenizer_params)

Check warning on line 35 in internal/core/src/index/TextMatchIndex.cpp

View check run for this annotation

Codecov / codecov/patch

internal/core/src/index/TextMatchIndex.cpp#L35

Added line #L35 was not covered by tests
: commit_interval_in_ms_(std::numeric_limits<int64_t>::max()),
last_commit_time_(stdclock::now()) {
path_ = path;
Expand All @@ -47,10 +45,9 @@
tokenizer_params);
}

TextMatchIndex::TextMatchIndex(
const storage::FileManagerContext& ctx,
const char* tokenizer_name,
const std::map<std::string, std::string>& tokenizer_params)
TextMatchIndex::TextMatchIndex(const storage::FileManagerContext& ctx,

Check warning on line 48 in internal/core/src/index/TextMatchIndex.cpp

View check run for this annotation

Codecov / codecov/patch

internal/core/src/index/TextMatchIndex.cpp#L48

Added line #L48 was not covered by tests
const char* tokenizer_name,
const char* tokenizer_params)

Check warning on line 50 in internal/core/src/index/TextMatchIndex.cpp

View check run for this annotation

Codecov / codecov/patch

internal/core/src/index/TextMatchIndex.cpp#L50

Added line #L50 was not covered by tests
: commit_interval_in_ms_(std::numeric_limits<int64_t>::max()),
last_commit_time_(stdclock::now()) {
schema_ = ctx.fieldDataMeta.field_schema;
Expand Down Expand Up @@ -174,9 +171,8 @@
}

void
TextMatchIndex::RegisterTokenizer(
const char* tokenizer_name,
const std::map<std::string, std::string>& tokenizer_params) {
TextMatchIndex::RegisterTokenizer(const char* tokenizer_name,
const char* tokenizer_params) {
wrapper_->register_tokenizer(tokenizer_name, tokenizer_params);
}

Expand Down
25 changes: 10 additions & 15 deletions internal/core/src/index/TextMatchIndex.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,17 @@ using stdclock = std::chrono::high_resolution_clock;
class TextMatchIndex : public InvertedIndexTantivy<std::string> {
public:
// for growing segment.
explicit TextMatchIndex(
int64_t commit_interval_in_ms,
const char* tokenizer_name,
const std::map<std::string, std::string>& tokenizer_params);
explicit TextMatchIndex(int64_t commit_interval_in_ms,
const char* tokenizer_name,
const char* tokenizer_params);
// for sealed segment.
explicit TextMatchIndex(
const std::string& path,
const char* tokenizer_name,
const std::map<std::string, std::string>& tokenizer_params);
explicit TextMatchIndex(const std::string& path,
const char* tokenizer_name,
const char* tokenizer_params);
// for building index.
explicit TextMatchIndex(
const storage::FileManagerContext& ctx,
const char* tokenizer_name,
const std::map<std::string, std::string>& tokenizer_params);
explicit TextMatchIndex(const storage::FileManagerContext& ctx,
const char* tokenizer_name,
const char* tokenizer_params);
// for loading index
explicit TextMatchIndex(const storage::FileManagerContext& ctx);

Expand Down Expand Up @@ -67,9 +64,7 @@ class TextMatchIndex : public InvertedIndexTantivy<std::string> {
CreateReader();

void
RegisterTokenizer(
const char* tokenizer_name,
const std::map<std::string, std::string>& tokenizer_params);
RegisterTokenizer(const char* tokenizer_name, const char* tokenizer_params);

TargetBitmap
MatchQuery(const std::string& query);
Expand Down
2 changes: 1 addition & 1 deletion internal/core/src/indexbuilder/index_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@
auto index = std::make_unique<index::TextMatchIndex>(
fileManagerContext,
"milvus_tokenizer",
field_schema.get_tokenizer_params());
field_schema.get_tokenizer_params().c_str());

Check warning on line 287 in internal/core/src/indexbuilder/index_c.cpp

View check run for this annotation

Codecov / codecov/patch

internal/core/src/indexbuilder/index_c.cpp#L287

Added line #L287 was not covered by tests
index->Build(config);
auto binary =
std::make_unique<knowhere::BinarySet>(index->Upload(config));
Expand Down
8 changes: 4 additions & 4 deletions internal/core/src/segcore/ChunkedSegmentSealedImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1613,13 +1613,13 @@
index = std::make_unique<index::TextMatchIndex>(
std::numeric_limits<int64_t>::max(),
"milvus_tokenizer",
field_meta.get_tokenizer_params());
field_meta.get_tokenizer_params().c_str());

Check warning on line 1616 in internal/core/src/segcore/ChunkedSegmentSealedImpl.cpp

View check run for this annotation

Codecov / codecov/patch

internal/core/src/segcore/ChunkedSegmentSealedImpl.cpp#L1616

Added line #L1616 was not covered by tests
} else {
// build text index using mmap.
index = std::make_unique<index::TextMatchIndex>(
cfg.GetMmapPath(),
"milvus_tokenizer",
field_meta.get_tokenizer_params());
field_meta.get_tokenizer_params().c_str());

Check warning on line 1622 in internal/core/src/segcore/ChunkedSegmentSealedImpl.cpp

View check run for this annotation

Codecov / codecov/patch

internal/core/src/segcore/ChunkedSegmentSealedImpl.cpp#L1622

Added line #L1622 was not covered by tests
}

{
Expand Down Expand Up @@ -1669,7 +1669,7 @@
index->Reload();

index->RegisterTokenizer("milvus_tokenizer",
field_meta.get_tokenizer_params());
field_meta.get_tokenizer_params().c_str());

Check warning on line 1672 in internal/core/src/segcore/ChunkedSegmentSealedImpl.cpp

View check run for this annotation

Codecov / codecov/patch

internal/core/src/segcore/ChunkedSegmentSealedImpl.cpp#L1672

Added line #L1672 was not covered by tests

text_indexes_[field_id] = std::move(index);
}
Expand All @@ -1680,7 +1680,7 @@
std::unique_lock lck(mutex_);
const auto& field_meta = schema_->operator[](field_id);
index->RegisterTokenizer("milvus_tokenizer",
field_meta.get_tokenizer_params());
field_meta.get_tokenizer_params().c_str());

Check warning on line 1683 in internal/core/src/segcore/ChunkedSegmentSealedImpl.cpp

View check run for this annotation

Codecov / codecov/patch

internal/core/src/segcore/ChunkedSegmentSealedImpl.cpp#L1683

Added line #L1683 was not covered by tests
text_indexes_[field_id] = std::move(index);
}

Expand Down
4 changes: 2 additions & 2 deletions internal/core/src/segcore/SegmentGrowingImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -859,11 +859,11 @@ SegmentGrowingImpl::CreateTextIndex(FieldId field_id) {
"cannot create text index on non-string type");
// todo: make this(200) configurable.
auto index = std::make_unique<index::TextMatchIndex>(
200, "milvus_tokenizer", field_meta.get_tokenizer_params());
200, "milvus_tokenizer", field_meta.get_tokenizer_params().c_str());
index->Commit();
index->CreateReader();
index->RegisterTokenizer("milvus_tokenizer",
field_meta.get_tokenizer_params());
field_meta.get_tokenizer_params().c_str());
text_indexes_[field_id] = std::move(index);
}

Expand Down
8 changes: 4 additions & 4 deletions internal/core/src/segcore/SegmentSealedImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2043,13 +2043,13 @@
index = std::make_unique<index::TextMatchIndex>(
std::numeric_limits<int64_t>::max(),
"milvus_tokenizer",
field_meta.get_tokenizer_params());
field_meta.get_tokenizer_params().c_str());
} else {
// build text index using mmap.
index = std::make_unique<index::TextMatchIndex>(
cfg.GetMmapPath(),
"milvus_tokenizer",
field_meta.get_tokenizer_params());
field_meta.get_tokenizer_params().c_str());

Check warning on line 2052 in internal/core/src/segcore/SegmentSealedImpl.cpp

View check run for this annotation

Codecov / codecov/patch

internal/core/src/segcore/SegmentSealedImpl.cpp#L2052

Added line #L2052 was not covered by tests
}

{
Expand Down Expand Up @@ -2098,7 +2098,7 @@
index->Reload();

index->RegisterTokenizer("milvus_tokenizer",
field_meta.get_tokenizer_params());
field_meta.get_tokenizer_params().c_str());

text_indexes_[field_id] = std::move(index);
}
Expand All @@ -2109,7 +2109,7 @@
std::unique_lock lck(mutex_);
const auto& field_meta = schema_->operator[](field_id);
index->RegisterTokenizer("milvus_tokenizer",
field_meta.get_tokenizer_params());
field_meta.get_tokenizer_params().c_str());

Check warning on line 2112 in internal/core/src/segcore/SegmentSealedImpl.cpp

View check run for this annotation

Codecov / codecov/patch

internal/core/src/segcore/SegmentSealedImpl.cpp#L2112

Added line #L2112 was not covered by tests
text_indexes_[field_id] = std::move(index);
}

Expand Down
17 changes: 14 additions & 3 deletions internal/core/src/segcore/tokenizer_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// or implied. See the License for the specific language governing permissions and limitations under the License

#include "segcore/tokenizer_c.h"
#include <memory>
#include "common/FieldMeta.h"
#include "common/protobuf_utils.h"
#include "pb/schema.pb.h"
Expand All @@ -19,17 +20,27 @@
using Map = std::map<std::string, std::string>;

CStatus
create_tokenizer(CMap m, CTokenizer* tokenizer) {
create_tokenizer(const char* params, CTokenizer* tokenizer) {
try {
auto mm = reinterpret_cast<Map*>(m);
auto impl = std::make_unique<milvus::tantivy::Tokenizer>(*mm);
auto impl = std::make_unique<milvus::tantivy::Tokenizer>(params);
*tokenizer = impl.release();
return milvus::SuccessCStatus();
} catch (std::exception& e) {
return milvus::FailureCStatus(&e);
}
}

CStatus
clone_tokenizer(CTokenizer* tokenizer, CTokenizer* rst) {
try {
auto impl = reinterpret_cast<milvus::tantivy::Tokenizer*>(*tokenizer);
*rst = impl->Clone().release();
return milvus::SuccessCStatus();
} catch (std::exception& e) {
return milvus::FailureCStatus(&e);
}

Check warning on line 41 in internal/core/src/segcore/tokenizer_c.cpp

View check run for this annotation

Codecov / codecov/patch

internal/core/src/segcore/tokenizer_c.cpp#L34-L41

Added lines #L34 - L41 were not covered by tests
}

void
free_tokenizer(CTokenizer tokenizer) {
auto impl = reinterpret_cast<milvus::tantivy::Tokenizer*>(tokenizer);
Expand Down
5 changes: 4 additions & 1 deletion internal/core/src/segcore/tokenizer_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ extern "C" {
typedef void* CTokenizer;

CStatus
create_tokenizer(CMap m, CTokenizer* tokenizer);
create_tokenizer(const char* params, CTokenizer* tokenizer);

CStatus
clone_tokenizer(CTokenizer* tokenizer, CTokenizer* rst);

void
free_tokenizer(CTokenizer tokenizer);
Expand Down
6 changes: 4 additions & 2 deletions internal/core/thirdparty/tantivy/tantivy-binding/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ env_logger = "0.11.3"
log = "0.4.21"
tantivy-jieba = "0.10.0"
lazy_static = "1.4.0"
serde_json = "1.0.128"

[build-dependencies]
cbindgen = "0.26.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ RustArray tantivy_regex_query(void *ptr, const char *pattern);

RustArray tantivy_match_query(void *ptr, const char *query);

void tantivy_register_tokenizer(void *ptr, const char *tokenizer_name, void *tokenizer_params);
void tantivy_register_tokenizer(void *ptr,
const char *tokenizer_name,
const char *tokenizer_params);

void *tantivy_create_index(const char *field_name,
TantivyDataType data_type,
Expand Down Expand Up @@ -142,7 +144,7 @@ void tantivy_index_add_multi_keywords(void *ptr,
void *tantivy_create_text_writer(const char *field_name,
const char *path,
const char *tokenizer_name,
void *tokenizer_params,
const char *tokenizer_params,
uintptr_t num_threads,
uintptr_t overall_memory_budget_in_bytes,
bool in_ram);
Expand All @@ -157,7 +159,9 @@ bool tantivy_token_stream_advance(void *token_stream);

const char *tantivy_token_stream_get_token(void *token_stream);

void *tantivy_create_tokenizer(void *tokenizer_params);
void *tantivy_create_tokenizer(const char *tokenizer_params);

void *tantivy_clone_tokenizer(void *ptr);

void tantivy_free_tokenizer(void *tokenizer);

Expand Down
40 changes: 40 additions & 0 deletions internal/core/thirdparty/tantivy/tantivy-binding/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use serde_json as json;

#[derive(Debug)]
pub struct TantivyError{
reason: String,
}

impl TantivyError{
fn new(reason:String) -> Self{
TantivyError{reason:reason}
}

pub fn reason(&self) -> String{
return self.reason.clone()
}
}

impl From<&str> for TantivyError{
fn from(value: &str) -> Self {
Self::new(value.to_string())
}
}

impl From<String> for TantivyError{
fn from(value: String) -> Self {
Self::new(value)
}
}

impl From<json::Error> for TantivyError{
fn from(value: json::Error) -> Self {
Self::new(value.to_string())
}
}

impl ToString for TantivyError{
fn to_string(&self) -> String {
return self.reason()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use tantivy::{
Term,
};

use crate::{index_reader::IndexReaderWrapper, tokenizer::default_tokenizer};
use crate::{index_reader::IndexReaderWrapper, tokenizer::standard_analyzer};

impl IndexReaderWrapper {
// split the query string into multiple tokens using index's default tokenizer,
Expand All @@ -14,7 +14,7 @@ impl IndexReaderWrapper {
let mut tokenizer = self
.index
.tokenizer_for_field(self.field)
.unwrap_or(default_tokenizer())
.unwrap_or(standard_analyzer(vec![]))
.clone();
let mut token_stream = tokenizer.token_stream(q);
let mut terms: Vec<Term> = Vec::new();
Expand Down
Loading
Loading