Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Fix stemming for non-English locales (#1745)

Ozan Armağaned87e63961ca
diff --git a/include/collection.h b/include/collection.h
index 956d2d9c..0d3a8633 100644
--- a/include/collection.h
+++ b/include/collection.h
@@ -491,10 +491,16 @@ public:
bool is_exceeding_memory_threshold() const;
- void parse_search_query(const std::string &query, std::vector<std::string>& q_include_tokens,
+ void parse_search_query(const std::string &query, std::vector<std::string>& q_include_tokens, std::vector<std::string>& q_include_tokens_non_stemmed,
std::vector<std::vector<std::string>>& q_exclude_tokens,
std::vector<std::vector<std::string>>& q_phrases,
- const std::string& locale, const bool already_segmented, const std::string& stopword_set="") const;
+ const std::string& locale, const bool already_segmented, const std::string& stopword_set="", std::shared_ptr<Stemmer> stemmer = nullptr) const;
+
+ void process_tokens(std::vector<std::string>& tokens, std::vector<std::string>& q_include_tokens,
+ std::vector<std::vector<std::string>>& q_exclude_tokens,
+ std::vector<std::vector<std::string>>& q_phrases, bool& exclude_operator_prior,
+ bool& phrase_search_op_prior, std::vector<std::string>& phrase, const std::string& stopwords_set,
+ const bool& already_segmented, const std::string& locale, std::shared_ptr<Stemmer> stemmer) const;
// PUBLIC OPERATIONS
diff --git a/include/index.h b/include/index.h
index 316dc4e4..5c9d9be3 100644
--- a/include/index.h
+++ b/include/index.h
@@ -76,6 +76,7 @@ struct tok_candidates {
struct query_tokens_t {
std::vector<token_t> q_include_tokens;
+ std::vector<token_t> q_unstemmed_tokens;
std::vector<std::vector<std::string>> q_exclude_tokens;
std::vector<std::vector<std::string>> q_phrases;
std::vector<std::vector<std::string>> q_synonyms;
@@ -193,7 +194,7 @@ struct search_args {
const bool filter_curated_hits, const enable_t split_join_tokens, vector_query_t& vector_query,
size_t facet_sample_percent, size_t facet_sample_threshold, drop_tokens_param_t drop_tokens_mode,
bool enable_lazy_filter) :
- field_query_tokens(field_query_tokens),
+ field_query_tokens(field_query_tokens),
search_fields(search_fields), match_type(match_type), filter_tree_root(filter_tree_root), facets(facets),
included_ids(included_ids), excluded_ids(excluded_ids), sort_fields_std(sort_fields_std),
facet_query(facet_query), num_typos(num_typos), max_facet_values(max_facet_values), per_page(per_page),
diff --git a/include/tokenizer.h b/include/tokenizer.h
index 2b133efa..83820a52 100644
--- a/include/tokenizer.h
+++ b/include/tokenizer.h
@@ -8,10 +8,11 @@
#include <unicode/translit.h>
#include "japanese_localizer.h"
#include "logger.h"
+#include "stemmer_manager.h"
class Tokenizer {
private:
- std::string_view text;
+ std::string text;
size_t i;
const bool normalize;
const bool no_op;
@@ -47,6 +48,8 @@ private:
icu::Transliterator* transliterator = nullptr;
+ std::shared_ptr<Stemmer> stemmer = nullptr;
+
inline size_t get_stream_mode(char c) {
return (std::isalnum(c) || index_symbols[uint8_t(c)] == 1) ? INDEX : (
(c == ' ' || c == '\n' || separator_symbols[uint8_t(c)] == 1) ? SEPARATE : SKIP
@@ -59,7 +62,8 @@ public:
bool normalize=true, bool no_op=false,
const std::string& locale = "",
const std::vector<char>& symbols_to_index = {},
- const std::vector<char>& separators = {});
+ const std::vector<char>& separators = {},
+ std::shared_ptr<Stemmer> stemmer = nullptr);
~Tokenizer() {
iconv_close(cd);
diff --git a/src/collection.cpp b/src/collection.cpp
index c00b6522..2408dee8 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -2340,11 +2340,12 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
std::vector<query_tokens_t> field_query_tokens;
std::vector<std::string> q_tokens; // used for auxillary highlighting
std::vector<std::string> q_include_tokens;
+ std::vector<std::string> q_unstemmed_tokens;
if(weighted_search_fields.size() == 0) {
// has to be a wildcard query
field_query_tokens.emplace_back(query_tokens_t{});
- parse_search_query(query, q_include_tokens,
+ parse_search_query(query, q_include_tokens, q_unstemmed_tokens,
field_query_tokens[0].q_exclude_tokens, field_query_tokens[0].q_phrases, "",
false, stopwords_set);
@@ -2357,15 +2358,22 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
field_query_tokens[0].q_include_tokens.emplace_back(i, q_include_token, (i == q_include_tokens.size() - 1),
q_include_token.size(), 0);
}
+
+ for(size_t i = 0; i < q_unstemmed_tokens.size(); i++) {
+ auto& q_include_token = q_unstemmed_tokens[i];
+ field_query_tokens[0].q_unstemmed_tokens.emplace_back(i, q_include_token, (i == q_include_tokens.size() - 1),
+ q_include_token.size(), 0);
+ }
+
} else {
field_query_tokens.emplace_back(query_tokens_t{});
auto most_weighted_field = search_schema.at(weighted_search_fields[0].name);
const std::string & field_locale = most_weighted_field.locale;
- parse_search_query(query, q_include_tokens,
+ parse_search_query(query, q_include_tokens, q_unstemmed_tokens,
field_query_tokens[0].q_exclude_tokens,
field_query_tokens[0].q_phrases,
- field_locale, pre_segmented_query, stopwords_set);
+ field_locale, pre_segmented_query, stopwords_set, most_weighted_field.get_stemmer());
// process filter overrides first, before synonyms (order is important)
@@ -2381,6 +2389,12 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
q_include_token.size(), 0);
}
+ for(size_t i = 0; i < q_unstemmed_tokens.size(); i++) {
+ auto& q_include_token = q_unstemmed_tokens[i];
+ field_query_tokens[0].q_unstemmed_tokens.emplace_back(i, q_include_token, (i == q_include_tokens.size() - 1),
+ q_include_token.size(), 0);
+ }
+
for(auto& phrase: field_query_tokens[0].q_phrases) {
for(auto& token: phrase) {
q_tokens.push_back(token);
@@ -2534,7 +2548,7 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
std::vector<std::string> facet_query_tokens;
Tokenizer(facet_query.query, normalise, !fq_field.is_string(), fq_field.locale,
- symbols_to_index, token_separators).tokenize(facet_query_tokens);
+ symbols_to_index, token_separators, fq_field.get_stemmer()).tokenize(facet_query_tokens);
facet_query_num_tokens = facet_query_tokens.size();
facet_query_last_token = facet_query_tokens.empty() ? "" : facet_query_tokens.back();
@@ -2976,7 +2990,7 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
std::vector<std::string> fquery_tokens;
Tokenizer(facet_query.query, true, false, the_field.locale, symbols_to_index,
- token_separators).tokenize(fquery_tokens);
+ token_separators, the_field.get_stemmer()).tokenize(fquery_tokens);
if(fquery_tokens.empty()) {
continue;
@@ -2998,7 +3012,7 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
}
Tokenizer(facet_query.query, true, false, the_field.locale, symbols_to_index,
- token_separators).tokenize(ftokens[ti]);
+ token_separators, the_field.get_stemmer()).tokenize(ftokens[ti]);
const std::string& resolved_token = ftokens[ti];
size_t root_len = (fquery_tokens.size() == ftokens.size()) ?
@@ -3011,7 +3025,7 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
std::vector<std::string> raw_fquery_tokens;
Tokenizer(facet_query.query, normalise, false, the_field.locale, symbols_to_index,
- token_separators).tokenize(raw_fquery_tokens);
+ token_separators, the_field.get_stemmer()).tokenize(raw_fquery_tokens);
if(raw_fquery_tokens.empty()) {
continue;
@@ -3144,7 +3158,7 @@ void Collection::expand_search_query(const string& raw_query, size_t offset, siz
}
const auto& qleaves = search_params->searched_queries[q_index];
- Tokenizer tokenizer(raw_query, true, false, search_field_it->locale, symbols_to_index, token_separators);
+ Tokenizer tokenizer(raw_query, true, false, search_field_it->locale, symbols_to_index, token_separators, search_field_it->get_stemmer());
std::string raw_token;
size_t raw_token_index = 0, tok_start = 0, tok_end = 0;
@@ -3473,15 +3487,112 @@ void Collection::process_filter_overrides(std::vector<const override_t*>& filter
}
}
-void Collection::parse_search_query(const std::string &query, std::vector<std::string>& q_include_tokens,
+void Collection::process_tokens(std::vector<std::string>& tokens, std::vector<std::string>& q_include_tokens,
+ std::vector<std::vector<std::string>>& q_exclude_tokens,
+ std::vector<std::vector<std::string>>& q_phrases, bool& exclude_operator_prior,
+ bool& phrase_search_op_prior, std::vector<std::string>& phrase, const std::string& stopwords_set,
+ const bool& already_segmented, const std::string& locale, std::shared_ptr<Stemmer> stemmer) const{
+
+
+
+ auto symbols_to_index_has_minus =
+ std::find(symbols_to_index.begin(), symbols_to_index.end(), '-') != symbols_to_index.end();
+
+ for(auto& token: tokens) {
+ bool end_of_phrase = false;
+
+ if(token == "-" && !symbols_to_index_has_minus) {
+ continue;
+ } else if(token[0] == '-' && !symbols_to_index_has_minus) {
+ exclude_operator_prior = true;
+ token = token.substr(1);
+ }
+
+ if(token[0] == '"' && token.size() > 1) {
+ phrase_search_op_prior = true;
+ token = token.substr(1);
+ }
+
+ if(!token.empty() && (token.back() == '"' || (token[0] == '"' && token.size() == 1))) {
+ if(phrase_search_op_prior) {
+ // handles single token phrase and a phrase with padded space, like: "some query "
+ end_of_phrase = true;
+ token = token.substr(0, token.size()-1);
+ } else if(token[0] == '"' && token.size() == 1) {
+ // handles front padded phrase query, e.g. " some query"
+ phrase_search_op_prior = true;
+ }
+ }
+
+ std::vector<std::string> sub_tokens;
+
+ if(already_segmented) {
+ StringUtils::split(token, sub_tokens, " ");
+ } else {
+ Tokenizer(token, true, false, locale, symbols_to_index, token_separators, stemmer).tokenize(sub_tokens);
+ }
+
+ for(auto& sub_token: sub_tokens) {
+ if(sub_token.size() > 100) {
+ sub_token.erase(100);
+ }
+
+ if(exclude_operator_prior) {
+ if(phrase_search_op_prior) {
+ phrase.push_back(sub_token);
+ } else {
+ q_exclude_tokens.push_back({sub_token});
+ exclude_operator_prior = false;
+ }
+ } else if(phrase_search_op_prior) {
+ phrase.push_back(sub_token);
+ } else {
+ q_include_tokens.push_back(sub_token);
+ }
+ }
+
+ if(end_of_phrase && phrase_search_op_prior) {
+ if(exclude_operator_prior) {
+ q_exclude_tokens.push_back(phrase);
+ } else {
+ q_phrases.push_back(phrase);
+ }
+
+ phrase_search_op_prior = false;
+ exclude_operator_prior = false;
+ phrase.clear();
+ }
+ }
+
+ if(!phrase.empty()) {
+ if(exclude_operator_prior) {
+ q_exclude_tokens.push_back(phrase);
+ } else {
+ q_include_tokens.insert(q_include_tokens.end(), phrase.begin(), phrase.end());
+ }
+ }
+
+ if(q_include_tokens.empty()) {
+ if(!stopwords_set.empty()) {
+ //this can happen when all tokens in the include are stopwords
+ q_include_tokens.emplace_back("##hrhdh##");
+ } else {
+ // this can happen if the only query token is an exclusion token
+ q_include_tokens.emplace_back("*");
+ }
+ }
+}
+
+void Collection::parse_search_query(const std::string &query, std::vector<std::string>& q_include_tokens, std::vector<std::string>& q_unstemmed_tokens,
std::vector<std::vector<std::string>>& q_exclude_tokens,
std::vector<std::vector<std::string>>& q_phrases,
- const std::string& locale, const bool already_segmented, const std::string& stopwords_set) const {
+ const std::string& locale, const bool already_segmented, const std::string& stopwords_set, std::shared_ptr<Stemmer> stemmer) const {
if(query == "*") {
q_exclude_tokens = {};
q_include_tokens = {query};
} else {
std::vector<std::string> tokens;
+ std::vector<std::string> tokens_non_stemmed;
stopword_struct_t stopwordStruct;
if(!stopwords_set.empty()) {
const auto &stopword_op = StopwordsManager::get_instance().get_stopword(stopwords_set, stopwordStruct);
@@ -3498,104 +3609,32 @@ void Collection::parse_search_query(const std::string &query, std::vector<std::s
custom_symbols.push_back('-');
custom_symbols.push_back('"');
- Tokenizer(query, true, false, locale, custom_symbols, token_separators).tokenize(tokens);
+ Tokenizer(query, true, false, locale, custom_symbols, token_separators, stemmer).tokenize(tokens);
+ if(stemmer) {
+ Tokenizer(query, true, false, locale, custom_symbols, token_separators, nullptr).tokenize(tokens_non_stemmed);
+ }
}
for (const auto val: stopwordStruct.stopwords) {
tokens.erase(std::remove(tokens.begin(), tokens.end(), val), tokens.end());
+ tokens_non_stemmed.erase(std::remove(tokens_non_stemmed.begin(), tokens_non_stemmed.end(), val), tokens_non_stemmed.end());
}
bool exclude_operator_prior = false;
bool phrase_search_op_prior = false;
std::vector<std::string> phrase;
- auto symbols_to_index_has_minus =
- std::find(symbols_to_index.begin(), symbols_to_index.end(), '-') != symbols_to_index.end();
-
- for(auto& token: tokens) {
- bool end_of_phrase = false;
-
- if(token == "-" && !symbols_to_index_has_minus) {
- continue;
- } else if(token[0] == '-' && !symbols_to_index_has_minus) {
- exclude_operator_prior = true;
- token = token.substr(1);
- }
-
- if(token[0] == '"' && token.size() > 1) {
- phrase_search_op_prior = true;
- token = token.substr(1);
- }
-
- if(!token.empty() && (token.back() == '"' || (token[0] == '"' && token.size() == 1))) {
- if(phrase_search_op_prior) {
- // handles single token phrase and a phrase with padded space, like: "some query "
- end_of_phrase = true;
- token = token.substr(0, token.size()-1);
- } else if(token[0] == '"' && token.size() == 1) {
- // handles front padded phrase query, e.g. " some query"
- phrase_search_op_prior = true;
- }
- }
-
-
- // retokenize using collection config (handles hyphens being part of the query)
- std::vector<std::string> sub_tokens;
-
- if(already_segmented) {
- StringUtils::split(token, sub_tokens, " ");
- } else {
- Tokenizer(token, true, false, locale, symbols_to_index, token_separators).tokenize(sub_tokens);
- }
-
- for(auto& sub_token: sub_tokens) {
- if(sub_token.size() > 100) {
- sub_token.erase(100);
- }
-
- if(exclude_operator_prior) {
- if(phrase_search_op_prior) {
- phrase.push_back(sub_token);
- } else {
- q_exclude_tokens.push_back({sub_token});
- exclude_operator_prior = false;
- }
- } else if(phrase_search_op_prior) {
- phrase.push_back(sub_token);
- } else {
- q_include_tokens.push_back(sub_token);
- }
- }
-
- if(end_of_phrase && phrase_search_op_prior) {
- if(exclude_operator_prior) {
- q_exclude_tokens.push_back(phrase);
- } else {
- q_phrases.push_back(phrase);
- }
-
- phrase_search_op_prior = false;
- exclude_operator_prior = false;
- phrase.clear();
- }
- }
-
- if(!phrase.empty()) {
- if(exclude_operator_prior) {
- q_exclude_tokens.push_back(phrase);
- } else {
- q_include_tokens.insert(q_include_tokens.end(), phrase.begin(), phrase.end());
- }
- }
+ process_tokens(tokens, q_include_tokens, q_exclude_tokens, q_phrases, exclude_operator_prior, phrase_search_op_prior, phrase, stopwords_set, already_segmented, locale, stemmer);
+
+ if(stemmer) {
+ exclude_operator_prior = false;
+ phrase_search_op_prior = false;
+ phrase.clear();
+ // those are unused
+ std::vector<std::vector<std::string>> q_exclude_tokens_dummy;
+ std::vector<std::vector<std::string>> q_phrases_dummy;
- if(q_include_tokens.empty()) {
- if(!stopwords_set.empty()) {
- //this can happen when all tokens in the include are stopwords
- q_include_tokens.emplace_back("##hrhdh##");
- } else {
- // this can happen if the only query token is an exclusion token
- q_include_tokens.emplace_back("*");
- }
+ process_tokens(tokens_non_stemmed, q_unstemmed_tokens, q_exclude_tokens_dummy, q_phrases_dummy, exclude_operator_prior, phrase_search_op_prior, phrase, stopwords_set, already_segmented, locale, nullptr);
}
}
}
@@ -3841,7 +3880,7 @@ void Collection::highlight_result(const std::string& raw_query, const field &sea
bool normalise = !use_word_tokenizer;
std::vector<std::string> raw_query_tokens;
- Tokenizer(raw_query, normalise, false, search_field.locale, symbols_to_index, token_separators).tokenize(raw_query_tokens);
+ Tokenizer(raw_query, normalise, false, search_field.locale, symbols_to_index, token_separators, search_field.get_stemmer()).tokenize(raw_query_tokens);
if(raw_query_tokens.empty()) {
return ;
@@ -4128,10 +4167,10 @@ bool Collection::handle_highlight_text(std::string& text, bool normalise, const
const Match& match = match_index.match;
- Tokenizer tokenizer(text, normalise, false, search_field.locale, symbols_to_index, token_separators);
+ Tokenizer tokenizer(text, normalise, false, search_field.locale, symbols_to_index, token_separators, search_field.get_stemmer());
// word tokenizer is a secondary tokenizer used for specific languages that requires transliteration
- Tokenizer word_tokenizer("", true, false, search_field.locale, symbols_to_index, token_separators);
+ Tokenizer word_tokenizer("", true, false, search_field.locale, symbols_to_index, token_separators, search_field.get_stemmer());
if(search_field.locale == "ko") {
text = string_utils.unicode_nfkd(text);
diff --git a/src/index.cpp b/src/index.cpp
index 3b2b4521..07baaa60 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -1154,11 +1154,10 @@ void Index::tokenize_string(const std::string& text, const field& a_field,
const std::vector<char>& token_separators,
std::unordered_map<std::string, std::vector<uint32_t>>& token_to_offsets) {
- Tokenizer tokenizer(text, true, !a_field.is_string(), a_field.locale, symbols_to_index, token_separators);
+ Tokenizer tokenizer(text, true, !a_field.is_string(), a_field.locale, symbols_to_index, token_separators, a_field.get_stemmer());
std::string token;
std::string last_token;
size_t token_index = 0;
-
while(tokenizer.next(token, token_index)) {
if(token.empty()) {
continue;
@@ -1168,15 +1167,6 @@ void Index::tokenize_string(const std::string& text, const field& a_field,
token.erase(100);
}
- if(a_field.is_stem()) {
- auto stemmer = a_field.get_stemmer();
- if(stemmer) {
- token = stemmer->stem(token);
- } else {
- LOG(INFO) << "Stemmer couldn't be initialized for field: " << a_field.name;
- }
- }
-
token_to_offsets[token].push_back(token_index + 1);
last_token = token;
}
@@ -1197,7 +1187,7 @@ void Index::tokenize_string_array(const std::vector<std::string>& strings,
const std::string& str = strings[array_index];
std::set<std::string> token_set; // required to deal with repeating tokens
- Tokenizer tokenizer(str, true, !a_field.is_string(), a_field.locale, symbols_to_index, token_separators);
+ Tokenizer tokenizer(str, true, !a_field.is_string(), a_field.locale, symbols_to_index, token_separators, a_field.get_stemmer());
std::string token, last_token;
size_t token_index = 0;
@@ -1211,15 +1201,6 @@ void Index::tokenize_string_array(const std::vector<std::string>& strings,
token.erase(100);
}
- if(a_field.is_stem()) {
- auto stemmer = a_field.get_stemmer();
- if(stemmer) {
- token = stemmer->stem(token);
- } else {
- LOG(INFO) << "Stemmer couldn't be initialized for field: " << a_field.name;
- }
- }
-
token_to_offsets[token].push_back(token_index + 1);
token_set.insert(token);
last_token = token;
@@ -2215,6 +2196,7 @@ Option<bool> Index::run_search(search_args* search_params, const std::string& co
const std::vector<facet_index_type_t>& facet_index_types, bool enable_typos_for_numerical_tokens,
bool enable_synonyms, bool synonym_prefix, uint32_t synonym_num_typos,
bool enable_typos_for_alpha_numerical_tokens) {
+
auto res = search(search_params->field_query_tokens,
search_params->search_fields,
search_params->match_type,
@@ -3107,12 +3089,19 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, cons
std::set<uint64> query_hashes;
// resolve synonyms so that we can compute `syn_orig_num_tokens`
- std::vector<std::vector<token_t>> all_queries = {field_query_tokens[0].q_include_tokens};
+ std::vector<std::vector<token_t>> all_queries = {field_query_tokens[0].q_unstemmed_tokens.empty() ?
+ field_query_tokens[0].q_include_tokens : field_query_tokens[0].q_unstemmed_tokens};
std::vector<std::vector<token_t>> q_pos_synonyms;
std::vector<std::string> q_include_tokens;
int syn_orig_num_tokens = -1;
- for(size_t j = 0; j < field_query_tokens[0].q_include_tokens.size(); j++) {
- q_include_tokens.push_back(field_query_tokens[0].q_include_tokens[j].value);
+ if(!field_query_tokens[0].q_unstemmed_tokens.empty()) {
+ for(size_t j = 0; j < field_query_tokens[0].q_unstemmed_tokens.size(); j++) {
+ q_include_tokens.push_back(field_query_tokens[0].q_unstemmed_tokens[j].value);
+ }
+ } else {
+ for(size_t j = 0; j < field_query_tokens[0].q_include_tokens.size(); j++) {
+ q_include_tokens.push_back(field_query_tokens[0].q_include_tokens[j].value);
+ }
}
if(enable_synonyms) {
@@ -3120,22 +3109,11 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, cons
synonym_prefix, synonym_num_typos);
}
- const bool& do_stemming = (search_schema.find(the_fields[0].name) != search_schema.end() && search_schema.at(the_fields[0].name).stem);
- if (do_stemming) {
- auto stemmer = search_schema.at(the_fields[0].name).get_stemmer();
- for(auto& q_include_token: q_include_tokens) {
- q_include_token = stemmer->stem(q_include_token);
- }
-
- for(auto& q_token: field_query_tokens[0].q_include_tokens) {
- q_token.value = stemmer->stem(q_token.value);
- }
- }
-
if(!field_query_tokens[0].q_synonyms.empty()) {
syn_orig_num_tokens = field_query_tokens[0].q_include_tokens.size();
}
+ const bool& do_stemming = (search_schema.find(the_fields[0].name) != search_schema.end() && search_schema.at(the_fields[0].name).stem);
for(const auto& q_syn_vec: field_query_tokens[0].q_synonyms) {
std::vector<token_t> q_pos_syn;
for(size_t j=0; j < q_syn_vec.size(); j++) {
diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp
index dee76827..f9bb5062 100644
--- a/src/tokenizer.cpp
+++ b/src/tokenizer.cpp
@@ -6,8 +6,8 @@
Tokenizer::Tokenizer(const std::string& input, bool normalize, bool no_op, const std::string& locale,
const std::vector<char>& symbols_to_index,
- const std::vector<char>& separators):
- i(0), normalize(normalize), no_op(no_op), locale(locale) {
+ const std::vector<char>& separators, std::shared_ptr<Stemmer> stemmer) :
+ i(0), normalize(normalize), no_op(no_op), locale(locale), stemmer(stemmer) {
for(char c: symbols_to_index) {
index_symbols[uint8_t(c)] = 1;
@@ -29,7 +29,12 @@ Tokenizer::Tokenizer(const std::string& input, bool normalize, bool no_op, const
cd = iconv_open("ASCII//TRANSLIT", "UTF-8");
- init(input);
+ if(stemmer) {
+ auto stemmed_input = stemmer->stem(input);
+ init(stemmed_input);
+ } else {
+ init(input);
+ }
}
@@ -229,7 +234,7 @@ bool Tokenizer::next(std::string &token, size_t& token_index, size_t& start_inde
return true;
}
- while(i < text.size()) {
+ while(i < text.length()) {
if(is_ascii_char(text[i])) {
size_t this_stream_mode = get_stream_mode(text[i]);
diff --git a/test/collection_specific_more_test.cpp b/test/collection_specific_more_test.cpp
index 1188a176..829e776d 100644
--- a/test/collection_specific_more_test.cpp
+++ b/test/collection_specific_more_test.cpp
@@ -2856,7 +2856,7 @@ TEST_F(CollectionSpecificMoreTest, TestStemming) {
auto stem_res = coll_stem->search("run", {"name"}, {}, {}, {}, {0}, 10, 1, FREQUENCY, {false}, 1);
ASSERT_TRUE(stem_res.ok());
ASSERT_EQ(1, stem_res.get()["hits"].size());
- ASSERT_EQ("running", stem_res.get()["hits"][0]["highlight"]["name"]["matched_tokens"][0].get<std::string>());
+ ASSERT_EQ("run", stem_res.get()["hits"][0]["highlight"]["name"]["matched_tokens"][0].get<std::string>());
auto no_stem_res = coll_no_stem->search("run", {"name"}, {}, {}, {}, {0}, 10, 1, FREQUENCY, {false}, 1);
ASSERT_TRUE(no_stem_res.ok());
@@ -2977,8 +2977,10 @@ TEST_F(CollectionSpecificMoreTest, TestStemmingWithSynonym) {
"synonyms": ["making", "foobar"]
}
)"_json;
+ LOG(INFO) << "Adding synonym...";
auto synonym_op = coll_stem->add_synonym(synonym_json);
- LOG(INFO) << synonym_op.error();
+ LOG(INFO) << "Synonym added...";
+
ASSERT_TRUE(synonym_op.ok());
ASSERT_TRUE(coll_stem->add(R"({"word": "foobar"})"_json.dump()).ok());
@@ -3094,4 +3096,23 @@ TEST_F(CollectionSpecificMoreTest, EnableTyposForAlphaNumericalTokens) {
ASSERT_EQ("A-136/14", res["hits"][2]["document"]["title"].get<std::string>());
ASSERT_EQ("(136)214", res["hits"][3]["document"]["title"].get<std::string>());
ASSERT_EQ("13/14", res["hits"][4]["document"]["title"].get<std::string>());
+}
+
+TEST_F(CollectionSpecificMoreTest, TestStemmingCyrilic) {
+ nlohmann::json schema = R"({
+ "name": "words",
+ "fields": [
+ {"name": "word", "type": "string", "stem": true, "locale": "ru"}
+ ]
+ })"_json;
+
+ auto coll_stem_res = collectionManager.create_collection(schema);
+ ASSERT_TRUE(coll_stem_res.ok());
+ auto coll_stem = coll_stem_res.get();
+
+ ASSERT_TRUE(coll_stem->add(R"({"word": "доверенное"})"_json.dump()).ok());
+ ASSERT_TRUE(coll_stem->add(R"({"word": "доверенные"})"_json.dump()).ok());
+
+ auto res = coll_stem->search("доверенное", {"word"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {true}, 0).get();
+ ASSERT_EQ(2, res["hits"].size());
}
\ No newline at end of file
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index 7df81a1a..48656ee9 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -4258,12 +4258,12 @@ TEST_F(CollectionTest, QueryParsingForPhraseSearch) {
coll1 = collectionManager.create_collection("coll1", 1, fields, "points").get();
}
- std::vector<std::string> q_include_tokens;
+ std::vector<std::string> q_include_tokens, q_unstemmed_tokens;
std::vector<std::vector<std::string>> q_exclude_tokens;
std::vector<std::vector<std::string>> q_phrases;
std::string q = R"(the "phrase search" query)";
- /*coll1->parse_search_query(q, q_include_tokens, q_exclude_tokens, q_phrases, "en", false);
+ /*coll1->parse_search_query(q, q_include_tokens, q_unstemmed_tokens, q_exclude_tokens, q_phrases, "en", false);
ASSERT_EQ(2, q_include_tokens.size());
ASSERT_EQ("the", q_include_tokens[0]);
@@ -4278,9 +4278,10 @@ TEST_F(CollectionTest, QueryParsingForPhraseSearch) {
q = R"("space padded " query)";
q_include_tokens.clear();
q_exclude_tokens.clear();
+ q_unstemmed_tokens.clear();
q_phrases.clear();
- coll1->parse_search_query(q, q_include_tokens, q_exclude_tokens, q_phrases, "en", false);
+ coll1->parse_search_query(q, q_include_tokens, q_unstemmed_tokens, q_exclude_tokens, q_phrases, "en", false);
ASSERT_EQ(1, q_include_tokens.size());
ASSERT_EQ("query", q_include_tokens[0]);
ASSERT_EQ(1, q_phrases.size());
@@ -4295,7 +4296,7 @@ TEST_F(CollectionTest, QueryParsingForPhraseSearch) {
q_exclude_tokens.clear();
q_phrases.clear();
- coll1->parse_search_query(q, q_include_tokens, q_exclude_tokens, q_phrases, "en", false);
+ coll1->parse_search_query(q, q_include_tokens, q_unstemmed_tokens, q_exclude_tokens, q_phrases, "en", false);
ASSERT_EQ(1, q_include_tokens.size());
ASSERT_EQ("*", q_include_tokens[0]);
ASSERT_EQ(2, q_phrases.size());
@@ -4312,7 +4313,7 @@ TEST_F(CollectionTest, QueryParsingForPhraseSearch) {
q_exclude_tokens.clear();
q_phrases.clear();
- coll1->parse_search_query(q, q_include_tokens, q_exclude_tokens, q_phrases, "en", false);
+ coll1->parse_search_query(q, q_include_tokens, q_unstemmed_tokens, q_exclude_tokens, q_phrases, "en", false);
ASSERT_EQ(1, q_include_tokens.size());
ASSERT_EQ("*", q_include_tokens[0]);
ASSERT_EQ(1, q_phrases.size());
@@ -4326,7 +4327,7 @@ TEST_F(CollectionTest, QueryParsingForPhraseSearch) {
q_exclude_tokens.clear();
q_phrases.clear();
- coll1->parse_search_query(q, q_include_tokens, q_exclude_tokens, q_phrases, "en", false);
+ coll1->parse_search_query(q, q_include_tokens, q_unstemmed_tokens, q_exclude_tokens, q_phrases, "en", false);
ASSERT_EQ(1, q_include_tokens.size());
ASSERT_EQ("hello", q_include_tokens[0]);
ASSERT_EQ(0, q_phrases.size());
@@ -4337,7 +4338,7 @@ TEST_F(CollectionTest, QueryParsingForPhraseSearch) {
q_exclude_tokens.clear();
q_phrases.clear();
- coll1->parse_search_query(q, q_include_tokens, q_exclude_tokens, q_phrases, "en", false);
+ coll1->parse_search_query(q, q_include_tokens, q_unstemmed_tokens, q_exclude_tokens, q_phrases, "en", false);
ASSERT_EQ(1, q_include_tokens.size());
ASSERT_EQ("here", q_include_tokens[0]);
ASSERT_EQ(1, q_phrases.size());
@@ -4350,7 +4351,7 @@ TEST_F(CollectionTest, QueryParsingForPhraseSearch) {
q_include_tokens.clear();
q_exclude_tokens.clear();
q_phrases.clear();
- coll1->parse_search_query(q, q_include_tokens, q_exclude_tokens, q_phrases, "en", false);
+ coll1->parse_search_query(q, q_include_tokens, q_unstemmed_tokens, q_exclude_tokens, q_phrases, "en", false);
ASSERT_EQ(1, q_include_tokens.size());
ASSERT_EQ("here", q_include_tokens[0]);
ASSERT_EQ(0, q_phrases.size());
@@ -4364,7 +4365,7 @@ TEST_F(CollectionTest, QueryParsingForPhraseSearch) {
q_include_tokens.clear();
q_exclude_tokens.clear();
q_phrases.clear();
- coll1->parse_search_query(q, q_include_tokens, q_exclude_tokens, q_phrases, "en", false);
+ coll1->parse_search_query(q, q_include_tokens, q_unstemmed_tokens, q_exclude_tokens, q_phrases, "en", false);
ASSERT_EQ(1, q_include_tokens.size());
ASSERT_EQ("here", q_include_tokens[0]);
ASSERT_EQ(0, q_phrases.size());