diff --git a/include/topster.h b/include/topster.h index 5d43920d..e1af2112 100644 --- a/include/topster.h +++ b/include/topster.h @@ -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{}; diff --git a/src/collection.cpp b/src/collection.cpp index 337f47de..78832b9d 100644 --- a/src/collection.cpp +++ b/src/collection.cpp @@ -1382,7 +1382,7 @@ Option 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(); diff --git a/src/index.cpp b/src/index.cpp index 5f9d8eaf..009ce89a 100644 --- a/src/index.cpp +++ b/src/index.cpp @@ -3446,7 +3446,7 @@ void Index::compute_sort_scores(const std::vector& 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& 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); diff --git a/test/collection_sorting_test.cpp b/test/collection_sorting_test.cpp index 80755c93..43c3e393 100644 --- a/test/collection_sorting_test.cpp +++ b/test/collection_sorting_test.cpp @@ -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 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"( {