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

Handle update of field indexed with object value, but later on as string.

Kishore Nallan943b2ef06fe5
diff --git a/src/index.cpp b/src/index.cpp
index 17e95c4a..62b7c570 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -4858,6 +4858,12 @@ void Index::remove_field(uint32_t seq_id, const nlohmann::json& document, const
return;
}
+ if(document[field_name].is_object() && !search_field.is_object()) {
+ // Handle an edge case with auto schema detection:
+ // Value as object field (ignored), but later as string field will cause documents with incorrect on-disk type
+ return ;
+ }
+
// Go through all the field names and find the keys+values so that they can be removed from in-memory index
if(search_field.type == field_types::STRING_ARRAY || search_field.type == field_types::STRING) {
std::vector<std::string> tokens;
diff --git a/test/collection_specific_more_test.cpp b/test/collection_specific_more_test.cpp
index 80eaa25f..278c39ef 100644
--- a/test/collection_specific_more_test.cpp
+++ b/test/collection_specific_more_test.cpp
@@ -1185,6 +1185,38 @@ TEST_F(CollectionSpecificMoreTest, QueryWithOnlySpecialChars) {
ASSERT_EQ("0", res["hits"][0]["document"]["id"].get<std::string>());
}
+TEST_F(CollectionSpecificMoreTest, HandleStringFieldWithObjectValueEarlier) {
+ nlohmann::json schema = R"({
+ "name": "coll1",
+ "fields": [
+ {"name": ".*", "type": "auto"}
+ ]
+ })"_json;
+
+ Collection* coll1 = collectionManager.create_collection(schema).get();
+
+ // index a "bad" document with title as an object field
+
+ nlohmann::json doc;
+ doc["id"] = "12345";
+ doc["title"] = R"({"id": 12345})"_json;
+
+ auto add_op = coll1->add(doc.dump());
+ ASSERT_TRUE(add_op.ok());
+
+ // now add another document where `title` is a string
+ doc["id"] = "12346";
+ doc["title"] = "Title 2";
+ add_op = coll1->add(doc.dump());
+ ASSERT_TRUE(add_op.ok());
+
+ // try to update the former document
+ doc["id"] = "12345";
+ doc["title"] = "Title 1";
+ add_op = coll1->add(doc.dump(), UPSERT);
+ ASSERT_TRUE(add_op.ok());
+}
+
TEST_F(CollectionSpecificMoreTest, CopyDocHelper) {
std::vector<highlight_field_t> hightlight_items = {
highlight_field_t("foo.bar", false, false),