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

Deal with repeated facet values in arrays.

Kishore Nallan06b64d8879a4
diff --git a/src/index.cpp b/src/index.cpp
index 7192faba..6a1c99bf 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -1056,6 +1056,8 @@ void Index::tokenize_string_array_with_facets(const std::vector<std::string>& st
std::unordered_map<std::string, std::vector<uint32_t>>& token_to_offsets,
std::vector<uint64_t>& facet_hashes) {
+ std::set<uint64_t> facet_hash_set; // required to deal with repeating phrases
+
for(size_t array_index = 0; array_index < strings.size(); array_index++) {
const std::string& str = strings[array_index];
std::set<std::string> token_set; // required to deal with repeating tokens
@@ -1089,8 +1091,9 @@ void Index::tokenize_string_array_with_facets(const std::vector<std::string>& st
}
}
- if(is_facet) {
+ if(is_facet && facet_hash_set.count(facet_hash) == 0) {
facet_hashes.push_back(facet_hash);
+ facet_hash_set.insert(facet_hash);
}
if(token_set.empty()) {
diff --git a/test/collection_faceting_test.cpp b/test/collection_faceting_test.cpp
index ee9b798f..fec66c48 100644
--- a/test/collection_faceting_test.cpp
+++ b/test/collection_faceting_test.cpp
@@ -994,7 +994,7 @@ TEST_F(CollectionFacetingTest, FacetArrayValuesShouldBeNormalized) {
ASSERT_EQ(1, results["facet_counts"][0]["counts"].size());
// any document is chosen as representative
- ASSERT_EQ("bu-qu", results["facet_counts"][0]["counts"][0]["value"].get<std::string>());
+ ASSERT_EQ("BUQU", results["facet_counts"][0]["counts"][0]["value"].get<std::string>());
collectionManager.drop_collection("coll1");
}
@@ -1057,6 +1057,78 @@ TEST_F(CollectionFacetingTest, FacetByNestedIntField) {
ASSERT_EQ("companyRank", wildcard_facets[1].field_name);
}
+TEST_F(CollectionFacetingTest, FacetByNestedArrayField) {
+ nlohmann::json schema = R"({
+ "name": "coll1",
+ "enable_nested_fields": true,
+ "fields": [
+ {"name": "data", "type": "object", "optional": false, "facet": true }
+ ]
+ })"_json;
+
+ auto op = collectionManager.create_collection(schema);
+ ASSERT_TRUE(op.ok());
+ Collection* coll1 = op.get();
+
+ auto doc1 = R"({
+ "data": {"details": [{"name": "Foo"}, {"name": "Foo"}]}
+ })"_json;
+
+ auto doc2 = R"({
+ "data": {"details": [{"name": "Foo"}, {"name": "Foo"}]}
+ })"_json;
+
+ ASSERT_TRUE(coll1->add(doc1.dump(), CREATE).ok());
+ ASSERT_TRUE(coll1->add(doc2.dump(), CREATE).ok());
+
+ auto results = coll1->search("*", {}, "", {"data.details.name"}, {}, {0}, 10, 1,
+ token_ordering::FREQUENCY, {true}, 10, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4).get();
+
+ ASSERT_EQ(2, results["found"].get<size_t>());
+ ASSERT_EQ(1, results["facet_counts"].size());
+ ASSERT_EQ("data.details.name", results["facet_counts"][0]["field_name"]);
+ ASSERT_EQ(1, results["facet_counts"][0]["counts"].size());
+ ASSERT_EQ(2, results["facet_counts"][0]["counts"][0]["count"].get<size_t>());
+ ASSERT_EQ("Foo", results["facet_counts"][0]["counts"][0]["value"].get<std::string>());
+}
+
+TEST_F(CollectionFacetingTest, FacetByArrayField) {
+ nlohmann::json schema = R"({
+ "name": "coll1",
+ "enable_nested_fields": true,
+ "fields": [
+ {"name": "data", "type": "string[]", "optional": false, "facet": true }
+ ]
+ })"_json;
+
+ auto op = collectionManager.create_collection(schema);
+ ASSERT_TRUE(op.ok());
+ Collection* coll1 = op.get();
+
+ auto doc1 = R"({
+ "data": ["Foo", "Foo"]
+ })"_json;
+
+ auto doc2 = R"({
+ "data": ["Foo", "Foo"]
+ })"_json;
+
+ ASSERT_TRUE(coll1->add(doc1.dump(), CREATE).ok());
+ ASSERT_TRUE(coll1->add(doc2.dump(), CREATE).ok());
+
+ auto results = coll1->search("*", {}, "", {"data"}, {}, {0}, 10, 1,
+ token_ordering::FREQUENCY, {true}, 10, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4).get();
+
+ ASSERT_EQ(2, results["found"].get<size_t>());
+ ASSERT_EQ(1, results["facet_counts"].size());
+ ASSERT_EQ("data", results["facet_counts"][0]["field_name"]);
+ ASSERT_EQ(1, results["facet_counts"][0]["counts"].size());
+ ASSERT_EQ(2, results["facet_counts"][0]["counts"][0]["count"].get<size_t>());
+ ASSERT_EQ("Foo", results["facet_counts"][0]["counts"][0]["value"].get<std::string>());
+}
+
TEST_F(CollectionFacetingTest, FacetParseTest){
std::vector<field> fields = {
field("score", field_types::INT32, true),