Fix edge case for facet counts with empty strings in array.

This commit is contained in:
Kishore Nallan 2022-12-21 14:39:06 +05:30
parent 61beb7f317
commit a10cf167ca
3 changed files with 37 additions and 6 deletions

View File

@ -1658,7 +1658,7 @@ Option<nlohmann::json> Collection::search(const std::string & raw_query,
facet_result["field_name"] = a_facet.field_name;
facet_result["counts"] = nlohmann::json::array();
std::vector<std::pair<int64_t, facet_count_t>> facet_hash_counts;
std::vector<std::pair<uint64_t, facet_count_t>> facet_hash_counts;
for (const auto & kv : a_facet.result_map) {
facet_hash_counts.emplace_back(kv);
}

View File

@ -1170,16 +1170,14 @@ void Index::tokenize_string_array_with_facets(const std::vector<std::string>& st
}
}
//LOG(INFO) << "Str: " << str << ", last_token: " << last_token;
if(is_facet) {
facet_hashes.push_back(facet_hash);
}
if(token_set.empty()) {
continue;
}
if(is_facet) {
facet_hashes.push_back(facet_hash);
}
for(auto& the_token: token_set) {
// repeat last element to indicate end of offsets for this array index
token_to_offsets[the_token].push_back(token_to_offsets[the_token].back());

View File

@ -979,3 +979,36 @@ TEST_F(CollectionFacetingTest, FacetByNestedIntField) {
ASSERT_EQ(2, results["facet_counts"][0]["counts"][0]["count"].get<size_t>());
ASSERT_EQ("2000", results["facet_counts"][0]["counts"][0]["value"].get<std::string>());
}
TEST_F(CollectionFacetingTest, FacetOnArrayFieldWithSpecialChars) {
std::vector<field> fields = {
field("tags", field_types::STRING_ARRAY, true),
field("points", field_types::INT32, true),
};
Collection* coll1 = collectionManager.create_collection("coll1", 1, fields).get();
nlohmann::json doc;
doc["tags"] = {"gamma"};
doc["points"] = 10;
ASSERT_TRUE(coll1->add(doc.dump()).ok());
doc["tags"] = {"alpha", "| . |", "beta", "gamma"};
doc["points"] = 10;
ASSERT_TRUE(coll1->add(doc.dump()).ok());
auto results = coll1->search("*", {},
"", {"tags"}, {}, {2}, 10, 1, FREQUENCY, {true}, 1).get();
ASSERT_EQ(1, results["facet_counts"].size());
ASSERT_EQ(4, results["facet_counts"][0]["counts"].size());
for(size_t i = 0; i < results["facet_counts"][0]["counts"].size(); i++) {
auto fvalue = results["facet_counts"][0]["counts"][i]["value"].get<std::string>();
if(fvalue == "gamma") {
ASSERT_EQ(2, results["facet_counts"][0]["counts"][i]["count"].get<size_t>());
} else {
ASSERT_EQ(1, results["facet_counts"][0]["counts"][i]["count"].get<size_t>());
}
}
}