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 regression: allow include/exclude of stored fields.

Kishore Nallan67f45d8813e2
diff --git a/src/collection.cpp b/src/collection.cpp
index 93fba46b..ca4a577e 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1008,7 +1008,12 @@ Option<nlohmann::json> Collection::search(const std::string & raw_query,
for(auto& f_name: include_fields) {
auto field_op = extract_field_name(f_name, search_schema, include_fields_vec, false, enable_nested_fields);
if(!field_op.ok()) {
- return Option<nlohmann::json>(404, field_op.error());
+ if(field_op.code() == 404) {
+ // field need not be part of schema to be included (could be a stored value in the doc)
+ include_fields_vec.push_back(f_name);
+ continue;
+ }
+ return Option<nlohmann::json>(field_op.code(), field_op.error());
}
}
@@ -1020,7 +1025,12 @@ Option<nlohmann::json> Collection::search(const std::string & raw_query,
auto field_op = extract_field_name(f_name, search_schema, exclude_fields_vec, false, enable_nested_fields);
if(!field_op.ok()) {
- return Option<nlohmann::json>(404, field_op.error());
+ if(field_op.code() == 404) {
+ // field need not be part of schema to be excluded (could be a stored value in the doc)
+ exclude_fields_vec.push_back(f_name);
+ continue;
+ }
+ return Option<nlohmann::json>(field_op.code(), field_op.error());
}
}
@@ -1596,7 +1606,7 @@ Option<nlohmann::json> Collection::search(const std::string & raw_query,
wrapper_doc["highlights"].push_back(h_json);
}
- wrapper_doc["seq_id"] = (uint32_t) field_order_kv->key;
+ //wrapper_doc["seq_id"] = (uint32_t) field_order_kv->key;
if(group_limit && group_key.empty()) {
for(const auto& field_name: group_by_fields) {
diff --git a/test/collection_specific_more_test.cpp b/test/collection_specific_more_test.cpp
index 3450a6fb..2c82f7ed 100644
--- a/test/collection_specific_more_test.cpp
+++ b/test/collection_specific_more_test.cpp
@@ -1507,4 +1507,39 @@ TEST_F(CollectionSpecificMoreTest, NonNestedFieldNameWithDot) {
"<mark>", "</mark>", {1}).get();
ASSERT_EQ(1, res["hits"].size());
ASSERT_EQ("0", res["hits"][0]["document"]["id"].get<std::string>());
-}
\ No newline at end of file
+}
+
+TEST_F(CollectionSpecificMoreTest, IncludeExcludeUnIndexedField) {
+ nlohmann::json schema = R"({
+ "name": "coll1",
+ "fields": [
+ {"name": "title", "type": "string"}
+ ]
+ })"_json;
+
+ Collection* coll1 = collectionManager.create_collection(schema).get();
+
+ nlohmann::json doc;
+ doc["id"] = "0";
+ doc["title"] = "Sample Title 1";
+ doc["src"] = "Internet";
+ ASSERT_TRUE(coll1->add(doc.dump()).ok());
+
+ auto res = coll1->search("sample", {"title"}, "", {}, {}, {2}, 10, 1, FREQUENCY, {true}, 0,
+ {"src"}).get();
+
+ ASSERT_EQ(1, res["hits"].size());
+ ASSERT_EQ(1, res["hits"][0]["document"].size());
+ ASSERT_EQ("Internet", res["hits"][0]["document"]["src"].get<std::string>());
+
+ // check for exclude field of indexed field
+
+ spp::sparse_hash_set<std::string> include_fields;
+ res = coll1->search("sample", {"title"}, "", {}, {}, {2}, 10, 1, FREQUENCY, {true}, 0,
+ include_fields, {"src"}).get();
+
+ ASSERT_EQ(1, res["hits"].size());
+ ASSERT_EQ(2, res["hits"][0]["document"].size());
+ ASSERT_EQ("Sample Title 1", res["hits"][0]["document"]["title"].get<std::string>());
+ ASSERT_EQ("0", res["hits"][0]["document"]["id"].get<std::string>());
+}