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

Flat fields must not be returned in nested fields with include fields.

Kishore Nallan3d033d1ae973
diff --git a/src/collection.cpp b/src/collection.cpp
index 00e3dd86..0ea060fa 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1622,8 +1622,8 @@ Option<nlohmann::json> Collection::search(const std::string & raw_query,
}
}
- prune_doc(document, include_fields_full, exclude_fields_full);
remove_flat_fields(document);
+ prune_doc(document, include_fields_full, exclude_fields_full);
wrapper_doc["document"] = document;
wrapper_doc["highlight"] = highlight_res;
@@ -3502,7 +3502,7 @@ Option<bool> Collection::alter(nlohmann::json& alter_payload) {
}
void Collection::remove_flat_fields(nlohmann::json& document) {
- if(document.contains(".flat")) {
+ if(document.count(".flat") != 0) {
for(const auto& flat_key: document[".flat"].get<std::vector<std::string>>()) {
document.erase(flat_key);
}
diff --git a/test/collection_nested_fields_test.cpp b/test/collection_nested_fields_test.cpp
index 29435745..c0ebfb2d 100644
--- a/test/collection_nested_fields_test.cpp
+++ b/test/collection_nested_fields_test.cpp
@@ -593,7 +593,7 @@ TEST_F(CollectionNestedFieldsTest, SearchOnFieldsOnWildcardSchema) {
ASSERT_EQ("Could not find a field named `locations.foo.street` in the schema.", res_op.error());
}
-TEST_F(CollectionNestedFieldsTest, IncludeExcludeFields) {
+TEST_F(CollectionNestedFieldsTest, IncludeExcludeFieldsPruning) {
auto doc_str = R"({
"company": {"name": "Nike Inc."},
"employees": {
@@ -645,6 +645,36 @@ TEST_F(CollectionNestedFieldsTest, IncludeExcludeFields) {
ASSERT_EQ(R"({"locations":[{"address":{"products":["shoes","tshirts"]}},{"address":{"products":["sneakers","shoes"]}}]})", doc.dump());
}
+TEST_F(CollectionNestedFieldsTest, IncludeFieldsSearch) {
+ nlohmann::json schema = R"({
+ "name": "coll1",
+ "enable_nested_fields": true,
+ "fields": [
+ {"name": "name", "type": "object" }
+ ]
+ })"_json;
+
+ auto op = collectionManager.create_collection(schema);
+ ASSERT_TRUE(op.ok());
+ Collection* coll1 = op.get();
+
+ auto doc1 = R"({
+ "name": {"first": "John", "last": "Smith"}
+ })"_json;
+
+ auto add_op = coll1->add(doc1.dump(), CREATE);
+ ASSERT_TRUE(add_op.ok());
+
+ auto results = coll1->search("*", {},
+ "", {}, sort_fields, {0}, 10, 1,
+ token_ordering::FREQUENCY, {true}, 10, {"name.first"},
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4).get();
+
+ ASSERT_EQ(1, results["hits"][0]["document"].size());
+ ASSERT_EQ(1, results["hits"][0]["document"].count("name"));
+ ASSERT_EQ(1, results["hits"][0]["document"]["name"].size());
+}
+
TEST_F(CollectionNestedFieldsTest, HighlightNestedFieldFully) {
std::vector<field> fields = {field(".*", field_types::AUTO, false, true)};