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 duplicate counts when faceting across multiple query fields.

kishorencfde1d268cf74
diff --git a/src/index.cpp b/src/index.cpp
index 7492e36c..e429417b 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -584,7 +584,7 @@ void Index::search_candidates(const uint8_t & field_id, uint32_t* filter_ids, si
uint32_t total_cost = 0;
uint32_t* result_ids = query_suggestion[0]->values->ids.uncompress();
- for(auto tc: token_candidates_vec) {
+ for(const auto& tc: token_candidates_vec) {
total_cost += tc.cost;
}
@@ -615,8 +615,6 @@ void Index::search_candidates(const uint8_t & field_id, uint32_t* filter_ids, si
delete [] *all_result_ids;
*all_result_ids = new_all_result_ids;
- do_facets(facets, filtered_result_ids, filtered_results_size);
-
// go through each matching document id and calculate match score
score_results(sort_fields, (uint16_t) searched_queries.size(), field_id, total_cost, topster, query_suggestion,
filtered_result_ids, filtered_results_size);
@@ -624,8 +622,6 @@ void Index::search_candidates(const uint8_t & field_id, uint32_t* filter_ids, si
delete[] filtered_result_ids;
delete[] result_ids;
} else {
- do_facets(facets, result_ids, result_size);
-
uint32_t* new_all_result_ids;
all_result_ids_len = ArrayUtils::or_scalar(*all_result_ids, all_result_ids_len, result_ids,
result_size, &new_all_result_ids);
@@ -932,6 +928,7 @@ void Index::search(Option<uint32_t> & outcome, std::string query, const std::vec
collate_curated_ids(query, field, field_id, included_ids, curated_topster, searched_queries);
}
}
+ do_facets(facets, all_result_ids, all_result_ids_len);
}
do_facets(facets, &included_ids[0], included_ids.size());
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index a684da5b..11fb8eaa 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -685,6 +685,7 @@ TEST_F(CollectionTest, MultipleFields) {
std::vector<field> fields = {
field("title", field_types::STRING, false),
field("starring", field_types::STRING, false),
+ field("starring_facet", field_types::STRING, true),
field("cast", field_types::STRING_ARRAY, false),
field("points", field_types::INT32, false)
};
@@ -774,8 +775,10 @@ TEST_F(CollectionTest, MultipleFields) {
ASSERT_STREQ(id.c_str(), result_id.c_str());
}
- // when a token exists in multiple fields of the same document, document should be returned only once
+ // when a token exists in multiple fields of the same document, document and facet should be returned only once
query_fields = {"starring", "title", "cast"};
+ facets = {"starring_facet"};
+
results = coll_mul_fields->search("myers", query_fields, "", facets, sort_fields, 0, 10, 1, FREQUENCY, false).get();
ASSERT_EQ(1, results["hits"].size());
ids = {"17"};
@@ -786,6 +789,11 @@ TEST_F(CollectionTest, MultipleFields) {
ASSERT_STREQ(id.c_str(), result_id.c_str());
}
+ ASSERT_EQ(1, results["facet_counts"].size());
+ ASSERT_STREQ("starring_facet", results["facet_counts"][0]["field_name"].get<std::string>().c_str());
+ size_t facet_count = results["facet_counts"][0]["counts"][0]["count"];
+ ASSERT_EQ(1, facet_count);
+
collectionManager.drop_collection("coll_mul_fields");
}
diff --git a/test/multi_field_documents.jsonl b/test/multi_field_documents.jsonl
index b052c93a..46d4607d 100644
--- a/test/multi_field_documents.jsonl
+++ b/test/multi_field_documents.jsonl
@@ -1,18 +1,18 @@
-{"title": "Wake Up, Ron Burgundy: The Lost Movie", "starring": "Will Ferrell", "points": 62, "cast": [ "Adam McKay", "Steve Carell", "Paul Rudd"] }
-{"title": "Anchorman 2: The Legend Continues", "starring": "Will Ferrell", "points": 63, "cast": ["Josh Lawson", "Chris Parnell"] }
-{"title": "There Will Be Blood", "starring": "Daniel Day-Lewis", "points": 81, "cast": ["Martin Stringer", "Jacob Stringer", "Harrison Taylor"] }
-{"title": "Good Will Hunting", "starring": "Robin Williams", "points": 83, "cast": ["Matt Damon", "Ben Affleck", "Minnie Driver"] }
-{"title": "The Adventures of Huck Finn", "starring": "Ron Perlman", "points": 58, "cast": ["Elijah Wood", "Courtney B. Vance", "Robbie Coltrane"] }
-{"title": "Percy Jackson: Sea of Monsters", "starring": "Ron Perlman", "points": 59, "cast": ["Logan Lerman", "Alexandra Daddario"] }
-{"title": "Captain America: The Winter Soldier", "starring": "Samuel L. Jackson", "points": 78, "cast": ["Chris Evans", "Scarlett Johansson"] }
-{"title": "Quantum Quest: A Cassini Space Odyssey", "starring": "Samuel L. Jackson", "points": 52, "cast": ["Chris Pine"] }
-{"title": "Scott Pilgrim vs. the World", "starring": "Michael Cera", "points": 75, "cast": ["Alison Pill"] }
-{"title": "Homeland Security", "starring": "Scott Glenn", "points": 43, "cast": ["Tom Skerritt"] }
-{"title": "The Paperboy", "starring": "Scott Glenn", "points": 58, "cast": ["Zac Efron", "Matthew McConaughey"] }
-{"title": "The Silence of the Lambs", "starring": "Scott Glenn", "points": 86, "cast": ["Jodie Foster"] }
-{"title": "Confessions of a Shopaholic", "starring": "Kristin Scott Thomas", "points": 59, "cast": ["Isla Fisher", "Hugh Dancy"] }
-{"title": "The Woman in the Fifth", "starring": "Kristin Scott Thomas", "points": 53, "cast": ["Ethan Hawke"] }
-{"title": "Odd Thomas", "starring": "Matthew Page", "points": 69, "cast": ["Anton Yelchin", "Ashley Sommers"] }
-{"title": "Suffering Man's Charity", "starring": "Henry Thomas", "points": 69, "cast": ["Alan Cumming", "Alison Guh", "David Boreanaz"] }
-{"title": "The Gospel According to St. Matthew", "starring": "Paola Tedesco", "points": 79, "cast": ["Enrique Irazoqui", "Margherita Caruso"] }
-{"title": "Halloween 5: The Revenge of Michael Myers", "starring": "Donald Myers", "points": 52, "cast": ["Danielle Harris", "Ellie Cornell"] }
\ No newline at end of file
+{"cast":["Adam McKay","Steve Carell","Paul Rudd"],"points":62,"starring":"Will Ferrell","starring_facet":"Will Ferrell","title":"Wake Up, Ron Burgundy: The Lost Movie"}
+{"cast":["Josh Lawson","Chris Parnell"],"points":63,"starring":"Will Ferrell","starring_facet":"Will Ferrell","title":"Anchorman 2: The Legend Continues"}
+{"cast":["Martin Stringer","Jacob Stringer","Harrison Taylor"],"points":81,"starring":"Daniel Day-Lewis","starring_facet":"Daniel Day-Lewis","title":"There Will Be Blood"}
+{"cast":["Matt Damon","Ben Affleck","Minnie Driver"],"points":83,"starring":"Robin Williams","starring_facet":"Robin Williams","title":"Good Will Hunting"}
+{"cast":["Elijah Wood","Courtney B. Vance","Robbie Coltrane"],"points":58,"starring":"Ron Perlman","starring_facet":"Ron Perlman","title":"The Adventures of Huck Finn"}
+{"cast":["Logan Lerman","Alexandra Daddario"],"points":59,"starring":"Ron Perlman","starring_facet":"Ron Perlman","title":"Percy Jackson: Sea of Monsters"}
+{"cast":["Chris Evans","Scarlett Johansson"],"points":78,"starring":"Samuel L. Jackson","starring_facet":"Samuel L. Jackson","title":"Captain America: The Winter Soldier"}
+{"cast":["Chris Pine"],"points":52,"starring":"Samuel L. Jackson","starring_facet":"Samuel L. Jackson","title":"Quantum Quest: A Cassini Space Odyssey"}
+{"cast":["Alison Pill"],"points":75,"starring":"Michael Cera","starring_facet":"Michael Cera","title":"Scott Pilgrim vs. the World"}
+{"cast":["Tom Skerritt"],"points":43,"starring":"Scott Glenn","starring_facet":"Scott Glenn","title":"Homeland Security"}
+{"cast":["Zac Efron","Matthew McConaughey"],"points":58,"starring":"Scott Glenn","starring_facet":"Scott Glenn","title":"The Paperboy"}
+{"cast":["Jodie Foster"],"points":86,"starring":"Scott Glenn","starring_facet":"Scott Glenn","title":"The Silence of the Lambs"}
+{"cast":["Isla Fisher","Hugh Dancy"],"points":59,"starring":"Kristin Scott Thomas","starring_facet":"Kristin Scott Thomas","title":"Confessions of a Shopaholic"}
+{"cast":["Ethan Hawke"],"points":53,"starring":"Kristin Scott Thomas","starring_facet":"Kristin Scott Thomas","title":"The Woman in the Fifth from Kristin"}
+{"cast":["Anton Yelchin","Ashley Sommers"],"points":69,"starring":"Matthew Page","starring_facet":"Matthew Page","title":"Odd Thomas"}
+{"cast":["Alan Cumming","Alison Guh","David Boreanaz"],"points":69,"starring":"Henry Thomas","starring_facet":"Henry Thomas","title":"Suffering Man's Charity"}
+{"cast":["Enrique Irazoqui","Margherita Caruso"],"points":79,"starring":"Paola Tedesco","starring_facet":"Paola Tedesco","title":"The Gospel According to St. Matthew"}
+{"cast":["Danielle Harris","Ellie Cornell"],"points":52,"starring":"Donald Myers","starring_facet":"Donald Myers","title":"Halloween 5: The Revenge of Michael Myers"}
\ No newline at end of file