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

Don't consider dynamic field name having .* as a field.

Kishore Nallana99dd45bdc26
diff --git a/src/collection.cpp b/src/collection.cpp
index 67140cd6..ebc671a4 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -3095,6 +3095,12 @@ Option<bool> Collection::detect_new_fields(nlohmann::json& document,
break;
}
+ // to prevent confusion we also disallow dynamic field names that contain ".*"
+ if((kv.key() != ".*" && kv.key().find(".*") != std::string::npos)) {
+ skip_field = true;
+ break;
+ }
+
new_field = dynamic_field.second;
new_field.name = fname;
found_dynamic_field = true;
diff --git a/src/index.cpp b/src/index.cpp
index e686168c..7a84e07a 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -689,10 +689,11 @@ void Index::index_field_in_memory(const field& afield, std::vector<index_record>
fhashvalues.hashes[i] = field_index_it->second.facet_hashes[i];
}
- if(facet_index_v3[afield.name][seq_id % ARRAY_FACET_DIM] == nullptr) {
+ auto& facet_dim_index = facet_index_v3[afield.name][seq_id % ARRAY_FACET_DIM];
+ if(facet_dim_index == nullptr) {
LOG(ERROR) << "Error, facet index not initialized for field " << afield.name;
} else {
- facet_index_v3[afield.name][seq_id % ARRAY_FACET_DIM]->emplace(seq_id, std::move(fhashvalues));
+ facet_dim_index->emplace(seq_id, std::move(fhashvalues));
}
}
diff --git a/test/collection_all_fields_test.cpp b/test/collection_all_fields_test.cpp
index f3542b49..3d4c56cf 100644
--- a/test/collection_all_fields_test.cpp
+++ b/test/collection_all_fields_test.cpp
@@ -1517,6 +1517,7 @@ TEST_F(CollectionAllFieldsTest, FieldNameMatchingRegexpShouldNotBeIndexed) {
doc1["id"] = "0";
doc1["title"] = "One Two Three";
doc1["name.*"] = "Rowling";
+ doc1["name.*barbaz"] = "JK";
doc1[".*"] = "foo";
std::vector<std::string> json_lines;
@@ -1539,3 +1540,36 @@ TEST_F(CollectionAllFieldsTest, FieldNameMatchingRegexpShouldNotBeIndexed) {
ASSERT_EQ(1, results["hits"].size());
}
+TEST_F(CollectionAllFieldsTest, FieldNameMatchingRegexpShouldNotBeIndexedInNonAutoSchema) {
+ std::vector<field> fields = {field("title", field_types::STRING, false),
+ field("name.*", field_types::STRING, true, true)};
+
+ Collection* coll1 = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO).get();
+
+ nlohmann::json doc1;
+ doc1["id"] = "0";
+ doc1["title"] = "One Two Three";
+ doc1["name.*"] = "Rowling";
+ doc1["name.*barbaz"] = "JK";
+ doc1[".*"] = "foo";
+
+ std::vector<std::string> json_lines;
+ json_lines.push_back(doc1.dump());
+
+ coll1->add_many(json_lines, doc1, UPSERT);
+ json_lines[0] = doc1.dump();
+ coll1->add_many(json_lines, doc1, UPSERT);
+
+ ASSERT_EQ(1, coll1->_get_index()->_get_search_index().size());
+ ASSERT_EQ(2, coll1->get_fields().size());
+
+ auto results = coll1->search("one", {"title"},
+ "", {}, {}, {2}, 10,
+ 1, FREQUENCY, {true},
+ 1, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "title", 5, {}, {}, {}, 0,
+ "<mark>", "</mark>", {}, 1000, true).get();
+
+ ASSERT_EQ(1, results["hits"].size());
+}
+