Fix faceting for field name with wildcard + type auto.

This commit is contained in:
Kishore Nallan 2021-05-13 19:22:55 +05:30
parent 239f4443e8
commit f55c5d2b13
2 changed files with 50 additions and 26 deletions

View File

@ -2406,42 +2406,28 @@ Option<bool> Collection::check_and_update_schema(nlohmann::json& document, const
std::string field_type;
bool parseable;
bool found_dynamic_field = false;
// check against dynamic field definitions
for(const auto& dynamic_field: dynamic_fields) {
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;
found_dynamic_field = true;
break;
}
}
if(fallback_field_type.empty()) {
// we will not auto detect schema if auto detection is not enabled
if(!found_dynamic_field && fallback_field_type.empty()) {
// we will not auto detect schema for non-dynamic fields if auto detection is not enabled
kv++;
continue;
}
if(fallback_field_type == field_types::AUTO || field_types::is_string_or_array(fallback_field_type)) {
// detect the actual type
if(fallback_field_type.empty() || fallback_field_type == field_types::AUTO ||
field_types::is_string_or_array(fallback_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) {
@ -2468,8 +2454,6 @@ Option<bool> Collection::check_and_update_schema(nlohmann::json& document, const
new_field.type = fallback_field_type;
}
UPDATE_SCHEMA:
if(!new_field.index) {
kv++;
continue;

View File

@ -714,6 +714,46 @@ TEST_F(CollectionAllFieldsTest, WildcardFacetFieldsOnAutoSchema) {
collectionManager.drop_collection("coll1");
}
TEST_F(CollectionAllFieldsTest, WildcardFacetFieldsWithAuoFacetFieldType) {
Collection *coll1;
std::vector<field> fields = {field("title", field_types::STRING, true),
field(".*_name", field_types::AUTO, true, true),};
coll1 = collectionManager.get_collection("coll1").get();
if (coll1 == nullptr) {
coll1 = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO).get();
}
nlohmann::json doc;
doc["title"] = "Org";
doc["org_name"] = "Amazon";
doc["year_name"] = 1990;
auto add_op = coll1->add(doc.dump(), CREATE);
ASSERT_TRUE(add_op.ok());
doc["title"] = "Org";
doc["org_name"] = "Walmart";
add_op = coll1->add(doc.dump(), CREATE);
ASSERT_TRUE(add_op.ok());
auto results = coll1->search("org", {"title"}, "", {"org_name"}, sort_fields, 0, 10, 1, FREQUENCY, false).get();
ASSERT_EQ(2, results["hits"].size());
ASSERT_EQ("Walmart", results["hits"][0]["document"]["org_name"].get<std::string>());
ASSERT_EQ("Amazon", results["hits"][1]["document"]["org_name"].get<std::string>());
ASSERT_EQ("Amazon", results["facet_counts"][0]["counts"][0]["value"].get<std::string>());
ASSERT_EQ(1, (int) results["facet_counts"][0]["counts"][0]["count"]);
ASSERT_EQ("Walmart", results["facet_counts"][0]["counts"][1]["value"].get<std::string>());
ASSERT_EQ(1, (int) results["facet_counts"][0]["counts"][1]["count"]);
collectionManager.drop_collection("coll1");
}
TEST_F(CollectionAllFieldsTest, WildcardFacetFieldsWithoutAutoSchema) {
Collection *coll1;