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

return whole object for nested facets

krunal6fa848c6a26c
diff --git a/src/collection.cpp b/src/collection.cpp
index 7680e839..193fa41c 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -2070,24 +2070,31 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
auto & facet_count = kv.second;
std::string value;
+ const std::string& seq_id_key = get_seq_id_key((uint32_t) facet_count.doc_id);
+ nlohmann::json document;
+ const Option<bool> & document_op = get_document_from_store(seq_id_key, document);
+ if(!document_op.ok()) {
+ LOG(ERROR) << "Facet fetch error. " << document_op.error();
+ continue;
+ }
+
if(a_facet.is_intersected) {
value = kv.first;
//LOG(INFO) << "used intersection";
} else {
// fetch actual facet value from representative doc id
//LOG(INFO) << "used hashes";
- const std::string& seq_id_key = get_seq_id_key((uint32_t) facet_count.doc_id);
- nlohmann::json document;
- const Option<bool> & document_op = get_document_from_store(seq_id_key, document);
- if(!document_op.ok()) {
- LOG(ERROR) << "Facet fetch error. " << document_op.error();
- continue;
- }
bool facet_found = facet_value_to_string(a_facet, facet_count, document, value);
if(!facet_found) {
continue;
}
}
+
+ if(the_field.nested) {
+ auto parent = the_field.name.substr(0, the_field.name.find("."));
+ value = document[parent].dump();
+ }
+
std::unordered_map<std::string, size_t> ftoken_pos;
std::vector<string>& ftokens = a_facet.hash_tokens[kv.first];
//LOG(INFO) << "working on hash_tokens for hash " << kv.first << " with size " << ftokens.size();
diff --git a/test/collection_faceting_test.cpp b/test/collection_faceting_test.cpp
index 111943fa..4bcc319f 100644
--- a/test/collection_faceting_test.cpp
+++ b/test/collection_faceting_test.cpp
@@ -1629,3 +1629,58 @@ TEST_F(CollectionFacetingTest, FloatFieldValueTruncation) {
ASSERT_EQ("300", results["facet_counts"][0]["counts"][0]["value"].get<std::string>());
}
+
+TEST_F(CollectionFacetingTest, FacetingObjectTest) {
+ nlohmann::json schema = R"({
+ "name": "coll1",
+ "enable_nested_fields": true,
+ "fields": [
+ {"name": "value.color", "type": "string", "optional": false, "facet": true },
+ {"name": "value.r", "type": "int32", "optional": false, "facet": true },
+ {"name": "value.g", "type": "int32", "optional": false, "facet": true },
+ {"name": "value.b", "type": "int32", "optional": false, "facet": true }
+ ]
+ })"_json;
+
+ auto op = collectionManager.create_collection(schema);
+ ASSERT_TRUE(op.ok());
+ Collection* coll1 = op.get();
+
+ nlohmann::json doc1 = R"({
+ "value": {
+ "color": "red",
+ "r": 255,
+ "g": 0,
+ "b": 0
+ }
+ })"_json;
+
+ nlohmann::json doc2 = R"({
+ "value": {
+ "color": "blue",
+ "r": 0,
+ "g": 0,
+ "b": 255
+ }
+ })"_json;
+
+ auto add_op = coll1->add(doc1.dump(), CREATE);
+ ASSERT_TRUE(add_op.ok());
+ add_op = coll1->add(doc2.dump(), CREATE);
+ ASSERT_TRUE(add_op.ok());
+
+ auto search_op = coll1->search("*", {},
+ "", {"value.color"}, {}, {2}, 10, 1,
+ FREQUENCY, {true}, 1, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(),
+ 10, "value.color : blue");
+
+ if(!search_op.ok()) {
+ LOG(ERROR) << search_op.error();
+ FAIL();
+ }
+ auto results = search_op.get();
+ LOG (INFO) << results.dump();
+ ASSERT_EQ(1, results["facet_counts"].size());
+ ASSERT_EQ(1, results["facet_counts"][0]["counts"].size());
+}