From eca0d952e9efcca8e01b628864a88ac07be23d26 Mon Sep 17 00:00:00 2001 From: Kishore Nallan Date: Tue, 11 May 2021 18:20:54 +0530 Subject: [PATCH] Fix issue with 3-way sort fields. --- src/index.cpp | 2 +- test/collection_sorting_test.cpp | 42 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/index.cpp b/src/index.cpp index fa6db017..b68c243d 100644 --- a/src/index.cpp +++ b/src/index.cpp @@ -2125,7 +2125,7 @@ void Index::score_results(const std::vector & sort_fields, const uint16 } if(sort_fields.size() > 2) { - if(field_values[2] != TEXT_MATCH_SENTINEL) { + if(field_values[2] == TEXT_MATCH_SENTINEL) { scores[2] = int64_t(match_score); match_score_index = 2; } else if (field_values[2] == SEQ_ID_SENTINEL) { diff --git a/test/collection_sorting_test.cpp b/test/collection_sorting_test.cpp index 788c3208..604e90aa 100644 --- a/test/collection_sorting_test.cpp +++ b/test/collection_sorting_test.cpp @@ -405,6 +405,48 @@ TEST_F(CollectionSortingTest, ThreeSortFieldsLimit) { collectionManager.drop_collection("coll1"); } +TEST_F(CollectionSortingTest, ThreeSortFieldsTextMatchLast) { + Collection *coll1; + + std::vector fields = { field("title", field_types::STRING, false), + field("artist", field_types::STRING, false), + field("popularity", field_types::INT32, false), + field("points", field_types::INT32, false),}; + + coll1 = collectionManager.get_collection("coll1").get(); + if(coll1 == nullptr) { + coll1 = collectionManager.create_collection("coll1", 1, fields, "points").get(); + } + + std::vector> records = { + {"Coby Grant", "100"}, // text_match: 33684577 + {"Coby Prant", "84642"}, // text_match: 129377 + }; + + for(size_t i=0; iadd(doc.dump()).ok()); + } + + std::vector sort_fields = { sort_by("popularity", "DESC"), sort_by("points", "DESC"), sort_by(sort_field_const::text_match, "DESC") }; + + auto res = coll1->search("grant", + {"title","artist"}, "", {}, sort_fields, 1, 10, 1, FREQUENCY).get(); + + ASSERT_EQ(2, res["found"].get()); + ASSERT_STREQ("1", res["hits"][0]["document"]["id"].get().c_str()); + ASSERT_STREQ("0", res["hits"][1]["document"]["id"].get().c_str()); + + collectionManager.drop_collection("coll1"); +} + TEST_F(CollectionSortingTest, NegativeInt64Value) { Collection *coll1;