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 repeated facet values in arrays during searching.

Kishore Nallana1e570900d1f
diff --git a/src/collection.cpp b/src/collection.cpp
index 41b3f926..41d5e9b1 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1732,13 +1732,6 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
if(!facet_query.query.empty()) {
// identify facet hash tokens
- for(const auto& the_facet: facets) {
- if(the_facet.field_name == facet_query.field_name) {
- //the_facet.hash_tokens
- break;
- }
- }
-
auto fq_field = search_schema.at(facet_query.field_name);
bool is_cyrillic = Tokenizer::is_cyrillic(fq_field.locale);
bool normalise = is_cyrillic ? false : true;
diff --git a/src/index.cpp b/src/index.cpp
index e4546983..82bcc5cb 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -1379,10 +1379,18 @@ void Index::do_facets(std::vector<facet> & facets, facet_query_t & facet_query,
RETURN_CIRCUIT_BREAKER
}
+ std::set<uint32_t> unique_facet_hashes;
+
for(size_t j = 0; j < facet_hashes.size(); j++) {
const auto& fhash = facet_hashes[j];
+ if(unique_facet_hashes.count(fhash) == 0) {
+ unique_facet_hashes.insert(fhash);
+ } else {
+ continue;
+ }
+
if(should_compute_stats) {
compute_facet_stats(a_facet, fhash, facet_field.type);
}
diff --git a/test/collection_faceting_test.cpp b/test/collection_faceting_test.cpp
index cadc95c8..a797532e 100644
--- a/test/collection_faceting_test.cpp
+++ b/test/collection_faceting_test.cpp
@@ -1109,7 +1109,7 @@ TEST_F(CollectionFacetingTest, FacetByArrayField) {
})"_json;
auto doc2 = R"({
- "data": ["Foo", "Foo"]
+ "data": ["Foo", "Foo", "Bazinga"]
})"_json;
ASSERT_TRUE(coll1->add(doc1.dump(), CREATE).ok());
@@ -1122,9 +1122,23 @@ TEST_F(CollectionFacetingTest, FacetByArrayField) {
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"].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>());
+
+ ASSERT_EQ(1, results["facet_counts"][0]["counts"][1]["count"].get<size_t>());
+ ASSERT_EQ("Bazinga", results["facet_counts"][0]["counts"][1]["value"].get<std::string>());
+
+ 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, "data:baz", 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(1, results["facet_counts"][0]["counts"][0]["count"].get<size_t>());
+ ASSERT_EQ("Bazinga", results["facet_counts"][0]["counts"][0]["value"].get<std::string>());
}
TEST_F(CollectionFacetingTest, FacetParseTest){