-
Notifications
You must be signed in to change notification settings - Fork 722
Add base fulltext helpers #24589
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
Merged
Merged
Add base fulltext helpers #24589
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,222 @@ | ||
#include "fulltext.h" | ||
#include <regex> | ||
|
||
namespace NKikimr::NFulltext { | ||
|
||
namespace { | ||
|
||
Ydb::Table::FulltextIndexSettings::Layout ParseLayout(const TString& layout, TString& error) { | ||
if (layout == "flat") | ||
return Ydb::Table::FulltextIndexSettings::FLAT; | ||
else { | ||
error = TStringBuilder() << "Invalid layout: " << layout; | ||
return Ydb::Table::FulltextIndexSettings::LAYOUT_UNSPECIFIED; | ||
} | ||
}; | ||
|
||
Ydb::Table::FulltextIndexSettings::Tokenizer ParseTokenizer(const TString& tokenizer, TString& error) { | ||
if (tokenizer == "whitespace") | ||
return Ydb::Table::FulltextIndexSettings::WHITESPACE; | ||
else if (tokenizer == "standard") | ||
return Ydb::Table::FulltextIndexSettings::STANDARD; | ||
else if (tokenizer == "keyword") | ||
return Ydb::Table::FulltextIndexSettings::KEYWORD; | ||
else { | ||
error = TStringBuilder() << "Invalid tokenizer: " << tokenizer; | ||
return Ydb::Table::FulltextIndexSettings::TOKENIZER_UNSPECIFIED; | ||
} | ||
}; | ||
|
||
i32 ParseInt32(const TString& name, const TString& value, TString& error) { | ||
i32 result = 0; | ||
if (!TryFromString(value, result) || result < 0) { // proto int32 fields with [(Ydb.value) = ">= 0"] annotation | ||
error = TStringBuilder() << "Invalid " << name << ": " << value; | ||
} | ||
return result; | ||
} | ||
|
||
bool ParseBool(const TString& name, const TString& value, TString& error) { | ||
bool result = false; | ||
if (!TryFromString(value, result)) { | ||
error = TStringBuilder() << "Invalid " << name << ": " << value; | ||
} | ||
return result; | ||
} | ||
|
||
// Note: written by llm, can be optimized a lot later | ||
TVector<TString> Tokenize(const TString& text, const Ydb::Table::FulltextIndexSettings::Tokenizer& tokenizer) { | ||
TVector<TString> tokens; | ||
switch (tokenizer) { | ||
case Ydb::Table::FulltextIndexSettings::WHITESPACE: { | ||
std::istringstream stream(text); | ||
TString token; | ||
while (stream >> token) { | ||
tokens.push_back(token); | ||
} | ||
break; | ||
} | ||
case Ydb::Table::FulltextIndexSettings::STANDARD: { | ||
std::regex word_regex(R"(\b\w+\b)"); // match alphanumeric words | ||
std::sregex_iterator it(text.begin(), text.end(), word_regex); | ||
std::sregex_iterator end; | ||
while (it != end) { | ||
tokens.push_back(it->str()); | ||
++it; | ||
} | ||
break; | ||
} | ||
case Ydb::Table::FulltextIndexSettings::KEYWORD: | ||
tokens.push_back(text); | ||
break; | ||
default: | ||
Y_ENSURE(TStringBuilder() << "Invalid tokenizer: " << static_cast<int>(tokenizer)); | ||
} | ||
|
||
return tokens; | ||
} | ||
|
||
bool ValidateSettings(const Ydb::Table::FulltextIndexSettings::Analyzers& settings, TString& error) { | ||
if (!settings.has_tokenizer() || settings.tokenizer() == Ydb::Table::FulltextIndexSettings::TOKENIZER_UNSPECIFIED) { | ||
error = "tokenizer should be set"; | ||
return false; | ||
} | ||
|
||
if (settings.has_language()) { | ||
error = "Unsupported language setting"; | ||
return false; | ||
} | ||
|
||
if (settings.use_filter_stopwords()) { | ||
error = "Unsupported use_filter_stopwords setting"; | ||
return false; | ||
} | ||
|
||
if (settings.use_filter_ngram()) { | ||
error = "Unsupported use_filter_ngram setting"; | ||
return false; | ||
} | ||
if (settings.use_filter_edge_ngram()) { | ||
error = "Unsupported use_filter_edge_ngram setting"; | ||
return false; | ||
} | ||
if (settings.has_filter_ngram_min_length()) { | ||
error = "Unsupported filter_ngram_min_length setting"; | ||
return false; | ||
} | ||
if (settings.has_filter_ngram_max_length()) { | ||
error = "Unsupported filter_ngram_max_length setting"; | ||
return false; | ||
} | ||
|
||
if (settings.use_filter_length()) { | ||
error = "Unsupported use_filter_length setting"; | ||
return false; | ||
} | ||
if (settings.has_filter_length_min()) { | ||
error = "Unsupported filter_length_min setting"; | ||
return false; | ||
} | ||
if (settings.has_filter_length_max()) { | ||
error = "Unsupported filter_length_max setting"; | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
TVector<TString> Analyze(const TString& text, const Ydb::Table::FulltextIndexSettings::Analyzers& settings) { | ||
TVector<TString> tokens = Tokenize(text, settings.tokenizer()); | ||
|
||
if (settings.use_filter_lowercase()) { | ||
for (auto& token : tokens) { | ||
token.to_lower(); | ||
} | ||
} | ||
|
||
return tokens; | ||
} | ||
|
||
bool ValidateSettings(const Ydb::Table::FulltextIndexSettings& settings, TString& error) { | ||
if (!settings.has_layout() || settings.layout() == Ydb::Table::FulltextIndexSettings::LAYOUT_UNSPECIFIED) { | ||
error = "layout should be set"; | ||
return false; | ||
} | ||
|
||
if (settings.columns().size() != 1) { | ||
error = TStringBuilder() << "fulltext index should have single column settings" | ||
<< " but have " << settings.columns().size() << " of them"; | ||
return false; | ||
} | ||
|
||
for (auto column : settings.columns()) { | ||
if (!column.has_column()) { | ||
error = "column should be set"; | ||
return false; | ||
} | ||
if (!column.has_analyzers()) { | ||
error = "column analyzers should be set"; | ||
return false; | ||
} | ||
if (!ValidateSettings(column.analyzers(), error)) { | ||
return false; | ||
} | ||
} | ||
|
||
error = ""; | ||
return true; | ||
} | ||
|
||
Ydb::Table::FulltextIndexSettings FillSettings(const TString& column, const TVector<std::pair<TString, TString>>& settings, TString& error) { | ||
Ydb::Table::FulltextIndexSettings result; | ||
Ydb::Table::FulltextIndexSettings::Analyzers resultAnalyzers; | ||
|
||
for (const auto& [name, value] : settings) { | ||
if (name == "layout") { | ||
result.set_layout(ParseLayout(value, error)); | ||
} else if (name == "tokenizer") { | ||
resultAnalyzers.set_tokenizer(ParseTokenizer(value, error)); | ||
} else if (name == "language") { | ||
resultAnalyzers.set_language(value); | ||
} else if (name == "use_filter_lowercase") { | ||
resultAnalyzers.set_use_filter_lowercase(ParseBool(name, value, error)); | ||
} else if (name == "use_filter_stopwords") { | ||
resultAnalyzers.set_use_filter_stopwords(ParseBool(name, value, error)); | ||
} else if (name == "use_filter_ngram") { | ||
resultAnalyzers.set_use_filter_ngram(ParseBool(name, value, error)); | ||
} else if (name == "use_filter_edge_ngram") { | ||
resultAnalyzers.set_use_filter_edge_ngram(ParseBool(name, value, error)); | ||
} else if (name == "filter_ngram_min_length") { | ||
resultAnalyzers.set_filter_ngram_min_length(ParseInt32(name, value, error)); | ||
} else if (name == "filter_ngram_max_length") { | ||
resultAnalyzers.set_filter_ngram_max_length(ParseInt32(name, value, error)); | ||
} else if (name == "use_filter_length") { | ||
resultAnalyzers.set_use_filter_length(ParseBool(name, value, error)); | ||
} else if (name == "filter_length_min") { | ||
resultAnalyzers.set_filter_length_min(ParseInt32(name, value, error)); | ||
} else if (name == "filter_length_max") { | ||
resultAnalyzers.set_filter_length_max(ParseInt32(name, value, error)); | ||
} else { | ||
error = TStringBuilder() << "Unknown index setting: " << name; | ||
return result; | ||
} | ||
|
||
if (error) { | ||
return result; | ||
} | ||
} | ||
|
||
{ | ||
// only single-columned index is supported for now | ||
auto columnAnalyzers = result.add_columns(); | ||
columnAnalyzers->set_column(column); | ||
columnAnalyzers->mutable_analyzers()->CopyFrom(resultAnalyzers); | ||
} | ||
|
||
ValidateSettings(result, error); | ||
|
||
return result; | ||
} | ||
|
||
|
||
} |
This file contains hidden or 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,14 @@ | ||
#pragma once | ||
|
||
#include "defs.h" | ||
|
||
#include <ydb/public/api/protos/ydb_table.pb.h> | ||
|
||
namespace NKikimr::NFulltext { | ||
|
||
TVector<TString> Analyze(const TString& text, const Ydb::Table::FulltextIndexSettings::Analyzers& settings); | ||
|
||
bool ValidateSettings(const Ydb::Table::FulltextIndexSettings& settings, TString& error); | ||
Ydb::Table::FulltextIndexSettings FillSettings(const TString& column, const TVector<std::pair<TString, TString>>& values, TString& error); | ||
|
||
} |
This file contains hidden or 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 hidden or 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,105 @@ | ||
#include "fulltext.h" | ||
|
||
#include <library/cpp/testing/unittest/registar.h> | ||
|
||
namespace NKikimr::NFulltext { | ||
|
||
Y_UNIT_TEST_SUITE(NFulltext) { | ||
|
||
Y_UNIT_TEST(ValidateSettings) { | ||
Ydb::Table::FulltextIndexSettings settings; | ||
TString error; | ||
|
||
UNIT_ASSERT(!ValidateSettings(settings, error)); | ||
UNIT_ASSERT_VALUES_EQUAL(error, "layout should be set"); | ||
settings.set_layout(Ydb::Table::FulltextIndexSettings::FLAT); | ||
|
||
UNIT_ASSERT(!ValidateSettings(settings, error)); | ||
UNIT_ASSERT_VALUES_EQUAL(error, "fulltext index should have single column settings but have 0 of them"); | ||
auto columnSettings = settings.add_columns(); | ||
|
||
UNIT_ASSERT(!ValidateSettings(settings, error)); | ||
UNIT_ASSERT_VALUES_EQUAL(error, "column should be set"); | ||
columnSettings->set_column("text"); | ||
|
||
UNIT_ASSERT(!ValidateSettings(settings, error)); | ||
UNIT_ASSERT_VALUES_EQUAL(error, "column analyzers should be set"); | ||
auto columnAnalyzers = columnSettings->mutable_analyzers(); | ||
|
||
UNIT_ASSERT(!ValidateSettings(settings, error)); | ||
UNIT_ASSERT_VALUES_EQUAL(error, "tokenizer should be set"); | ||
columnAnalyzers->set_tokenizer(Ydb::Table::FulltextIndexSettings::STANDARD); | ||
|
||
UNIT_ASSERT_C(ValidateSettings(settings, error), error); | ||
UNIT_ASSERT_VALUES_EQUAL(error, ""); | ||
} | ||
|
||
Y_UNIT_TEST(FillSettings) { | ||
TVector<std::pair<TString, TString>> list{ | ||
{"layout", "flat"}, | ||
{"tokenizer", "standard"}, | ||
{"use_filter_lowercase", "true"} | ||
}; | ||
|
||
TString error; | ||
auto settings = FillSettings("text", list, error); | ||
UNIT_ASSERT_VALUES_EQUAL(error, ""); | ||
|
||
UNIT_ASSERT_EQUAL(settings.layout(), Ydb::Table::FulltextIndexSettings::FLAT); | ||
UNIT_ASSERT_VALUES_EQUAL(settings.columns().size(), 1); | ||
UNIT_ASSERT_VALUES_EQUAL(settings.columns().at(0).column(), "text"); | ||
UNIT_ASSERT_EQUAL(settings.columns().at(0).analyzers().tokenizer(), Ydb::Table::FulltextIndexSettings::STANDARD); | ||
UNIT_ASSERT_VALUES_EQUAL(settings.columns().at(0).analyzers().use_filter_lowercase(), true); | ||
} | ||
|
||
Y_UNIT_TEST(FillSettingsInvalid) { | ||
{ | ||
TVector<std::pair<TString, TString>> list{ | ||
{"asdf", "qwer"} | ||
}; | ||
TString error; | ||
auto settings = FillSettings("text", list, error); | ||
UNIT_ASSERT_VALUES_EQUAL(error, "Unknown index setting: asdf"); | ||
} | ||
|
||
{ | ||
TVector<std::pair<TString, TString>> list{ | ||
{"layout", "flat"}, | ||
{"tokenizer", "standard"}, | ||
{"use_filter_lowercase", "asdf"} | ||
}; | ||
TString error; | ||
auto settings = FillSettings("text", list, error); | ||
UNIT_ASSERT_VALUES_EQUAL(error, "Invalid use_filter_lowercase: asdf"); | ||
} | ||
|
||
{ | ||
TVector<std::pair<TString, TString>> list{ | ||
{"layout", "flat"}, | ||
}; | ||
TString error; | ||
auto settings = FillSettings("text", list, error); | ||
UNIT_ASSERT_VALUES_EQUAL(error, "tokenizer should be set"); | ||
} | ||
} | ||
|
||
Y_UNIT_TEST(Analyze) { | ||
Ydb::Table::FulltextIndexSettings::Analyzers analyzers; | ||
TString text = "apple WaLLet spaced-dog"; | ||
|
||
analyzers.set_tokenizer(Ydb::Table::FulltextIndexSettings::WHITESPACE); | ||
UNIT_ASSERT_VALUES_EQUAL(Analyze(text, analyzers), (TVector<TString>{"apple", "WaLLet", "spaced-dog"})); | ||
|
||
analyzers.set_tokenizer(Ydb::Table::FulltextIndexSettings::STANDARD); | ||
UNIT_ASSERT_VALUES_EQUAL(Analyze(text, analyzers), (TVector<TString>{"apple", "WaLLet", "spaced", "dog"})); | ||
|
||
analyzers.set_tokenizer(Ydb::Table::FulltextIndexSettings::KEYWORD); | ||
UNIT_ASSERT_VALUES_EQUAL(Analyze(text, analyzers), (TVector<TString>{text})); | ||
|
||
analyzers.set_tokenizer(Ydb::Table::FulltextIndexSettings::WHITESPACE); | ||
analyzers.set_use_filter_lowercase(true); | ||
UNIT_ASSERT_VALUES_EQUAL(Analyze(text, analyzers), (TVector<TString>{"apple", "wallet", "spaced-dog"})); | ||
} | ||
} | ||
|
||
} |
This file contains hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FillSettings пока типа нигде не используется?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Да, просто аналогичен тому что в
kmeans_clusters