Handle match score not being part of sort fields.

In such a case, match score should not be returned in response.
This commit is contained in:
Kishore Nallan 2022-08-16 18:29:15 +05:30
parent 241a6e0476
commit c1aace925c
4 changed files with 36 additions and 5 deletions

View File

@ -8,7 +8,7 @@
struct KV {
uint8_t field_id{};
uint8_t match_score_index{};
int8_t match_score_index{};
uint16_t query_index{};
uint16_t array_index{};
uint32_t token_bits{};

View File

@ -1382,7 +1382,7 @@ Option<nlohmann::json> Collection::search(const std::string & raw_query,
if(field_order_kv->match_score_index == CURATED_RECORD_IDENTIFIER) {
wrapper_doc["curated"] = true;
} else {
} else if(field_order_kv->match_score_index >= 0) {
wrapper_doc["text_match"] = field_order_kv->scores[field_order_kv->match_score_index];
wrapper_doc["text_match_info"] = nlohmann::json::object();

View File

@ -3446,7 +3446,7 @@ void Index::compute_sort_scores(const std::vector<sort_by>& sort_fields, const i
filter_index = found_index;
}
scores[0] = int64_t(found);
scores[2] = int64_t(found);
} else {
auto it = field_values[2]->find(seq_id);
scores[2] = (it == field_values[2]->end()) ? default_score : it->second;
@ -3664,7 +3664,7 @@ void Index::do_infix_search(const size_t num_search_fields, const std::vector<se
0, match_score, seq_id, sort_order, false, false, false, 1, -1, {});
int64_t scores[3] = {0};
int64_t match_score_index = 0;
int64_t match_score_index = -1;
compute_sort_scores(sort_fields, sort_order, field_values, geopoint_indices, seq_id, filter_index,
100, scores, match_score_index);
@ -3995,7 +3995,7 @@ void Index::search_wildcard(const std::vector<filter>& filters,
match_score, seq_id, sort_order, false, false, false, 1, -1, plists);
int64_t scores[3] = {0};
int64_t match_score_index = 0;
int64_t match_score_index = -1;
compute_sort_scores(sort_fields, sort_order, field_values, geopoint_indices, seq_id, filter_index,
100, scores, match_score_index);

View File

@ -1737,6 +1737,37 @@ TEST_F(CollectionSortingTest, RepeatingTokenRanking) {
collectionManager.drop_collection("coll1");
}
TEST_F(CollectionSortingTest, SortingDoesNotHaveTextMatchComponent) {
// text_match_score field should not be present in response
std::vector<field> fields = {field("title", field_types::STRING, false),
field("points", field_types::INT32, false),};
Collection* coll1 = collectionManager.create_collection("coll1", 1, fields, "points").get();
nlohmann::json doc1;
doc1["id"] = "0";
doc1["title"] = "Test Title";
doc1["points"] = 100;
ASSERT_TRUE(coll1->add(doc1.dump()).ok());
sort_fields = {
sort_by("points", "DESC"),
sort_by("points", "DESC"),
sort_by("points", "DESC"),
};
auto results = coll1->search("test", {"title"}, "", {}, sort_fields, {2}, 10, 1, FREQUENCY, {true}).get();
ASSERT_EQ(1, results["hits"].size());
ASSERT_EQ(0, results["hits"][0].count("text_match"));
results = coll1->search("*", {}, "", {}, sort_fields, {2}, 10, 1, FREQUENCY, {true}).get();
ASSERT_EQ(1, results["hits"].size());
ASSERT_EQ(0, results["hits"][0].count("text_match"));
collectionManager.drop_collection("coll1");
}
TEST_F(CollectionSortingTest, IntegerFloatAndBoolShouldDefaultSortTrue) {
std::string coll_schema = R"(
{