mirror of
https://github.com/typesense/typesense.git
synced 2025-05-22 06:40:30 +08:00
Allow searching against string* fields.
This commit is contained in:
parent
9c448793bd
commit
6c284dba17
@ -114,7 +114,7 @@ struct field {
|
||||
}
|
||||
|
||||
bool is_dynamic() const {
|
||||
return name != ".*" && name.find(".*") != std::string::npos;
|
||||
return type == "string*" || (name != ".*" && name.find(".*") != std::string::npos);
|
||||
}
|
||||
|
||||
bool has_numerical_index() const {
|
||||
|
@ -2319,6 +2319,26 @@ Option<bool> Collection::check_and_update_schema(nlohmann::json& document, const
|
||||
if(std::regex_match (kv.key(), std::regex(dynamic_field.name))) {
|
||||
new_field = dynamic_field;
|
||||
new_field.name = fname;
|
||||
|
||||
if (field_types::is_string_or_array(dynamic_field.type)) {
|
||||
parseable = field::get_type(kv.value(), field_type);
|
||||
if(!parseable) {
|
||||
if(dirty_values == DIRTY_VALUES::REJECT || dirty_values == DIRTY_VALUES::COERCE_OR_REJECT) {
|
||||
return Option<bool>(400, "Type of field `" + kv.key() + "` is invalid.");
|
||||
} else {
|
||||
// DROP or COERCE_OR_DROP
|
||||
kv = document.erase(kv);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (field_type == field_types::STRING_ARRAY) {
|
||||
new_field.type = field_types::STRING_ARRAY;
|
||||
} else {
|
||||
new_field.type = field_types::STRING;
|
||||
}
|
||||
}
|
||||
|
||||
goto UPDATE_SCHEMA;
|
||||
}
|
||||
}
|
||||
|
@ -2511,7 +2511,7 @@ void Index::refresh_schemas(const std::vector<field>& new_fields) {
|
||||
sort_schema.emplace(new_field.name, new_field);
|
||||
|
||||
if(search_index.count(new_field.name) == 0) {
|
||||
if(new_field.is_string()) {
|
||||
if(new_field.is_string() || field_types::is_string_or_array(new_field.type)) {
|
||||
art_tree *t = new art_tree;
|
||||
art_tree_init(t);
|
||||
search_index.emplace(new_field.name, t);
|
||||
|
@ -442,6 +442,39 @@ TEST_F(CollectionAllFieldsTest, StringifyAllValues) {
|
||||
collectionManager.drop_collection("coll1");
|
||||
}
|
||||
|
||||
TEST_F(CollectionAllFieldsTest, SearchStringifiedField) {
|
||||
Collection *coll1;
|
||||
|
||||
std::vector<field> fields = {field("title", field_types::STRING, true),
|
||||
field("department", "string*", true, true),
|
||||
field(".*_name", "string*", true, true),};
|
||||
|
||||
coll1 = collectionManager.get_collection("coll1").get();
|
||||
if (coll1 == nullptr) {
|
||||
const Option<Collection*> &coll_op = collectionManager.create_collection("coll1", 1, fields, "", 0, "");
|
||||
ASSERT_TRUE(coll_op.ok());
|
||||
coll1 = coll_op.get();
|
||||
}
|
||||
|
||||
nlohmann::json doc;
|
||||
doc["title"] = "FIRST";
|
||||
doc["department"] = "ENGINEERING";
|
||||
doc["company_name"] = "Stark Inc.";
|
||||
|
||||
Option<nlohmann::json> add_op = coll1->add(doc.dump(), CREATE, "0");
|
||||
ASSERT_TRUE(add_op.ok());
|
||||
|
||||
auto results_op = coll1->search("stark", {"company_name"}, "", {}, sort_fields, 0, 10, 1, FREQUENCY, false);
|
||||
ASSERT_TRUE(results_op.ok());
|
||||
ASSERT_EQ(1, results_op.get()["hits"].size());
|
||||
|
||||
results_op = coll1->search("engineering", {"department"}, "", {}, sort_fields, 0, 10, 1, FREQUENCY, false);
|
||||
ASSERT_TRUE(results_op.ok());
|
||||
ASSERT_EQ(1, results_op.get()["hits"].size());
|
||||
|
||||
collectionManager.drop_collection("coll1");
|
||||
}
|
||||
|
||||
TEST_F(CollectionAllFieldsTest, StringSingularAllValues) {
|
||||
Collection *coll1;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user