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 secondary/tertiary order string sorting.

Kishore Nallanfbc646a56d02
diff --git a/src/index.cpp b/src/index.cpp
index 8f50e983..4b3c9bdb 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -3620,7 +3620,7 @@ void Index::score_results(const std::vector<sort_by> & sort_fields, const uint16
scores[1] = seq_id;
} else if(field_values[1] == &geo_sentinel_value) {
scores[1] = geopoint_distances[1];
- } else if(field_values[0] == &str_sentinel_value) {
+ } else if(field_values[1] == &str_sentinel_value) {
scores[1] = str_sort_index.at(sort_fields[1].name)->rank(seq_id);
} else {
auto it = field_values[1]->find(seq_id);
@@ -3640,7 +3640,7 @@ void Index::score_results(const std::vector<sort_by> & sort_fields, const uint16
scores[2] = seq_id;
} else if(field_values[2] == &geo_sentinel_value) {
scores[2] = geopoint_distances[2];
- } else if(field_values[0] == &str_sentinel_value) {
+ } else if(field_values[2] == &str_sentinel_value) {
scores[2] = str_sort_index.at(sort_fields[2].name)->rank(seq_id);
} else {
auto it = field_values[2]->find(seq_id);
diff --git a/test/collection_sorting_test.cpp b/test/collection_sorting_test.cpp
index 4f7bdf48..5687cba8 100644
--- a/test/collection_sorting_test.cpp
+++ b/test/collection_sorting_test.cpp
@@ -1179,6 +1179,75 @@ TEST_F(CollectionSortingTest, SortByTitle) {
collectionManager.drop_collection("coll1");
}
+TEST_F(CollectionSortingTest, SortByIntegerAndString) {
+ Collection* coll1;
+
+ std::vector<field> fields = {field("title", field_types::STRING, false, false, true, "", true),
+ field("points", field_types::INT32, false),};
+
+ coll1 = collectionManager.get_collection("coll1").get();
+ if (coll1 == nullptr) {
+ auto create_op = collectionManager.create_collection("coll1", 2, fields, "title");
+ ASSERT_TRUE(create_op.ok());
+ coll1 = create_op.get();
+ }
+
+ std::vector<std::vector<std::string>> records = {
+ {"abdde", "2"},
+ {"b", "2"},
+ {"b", "1"},
+ {"a", "1"},
+ {"c", "1"},
+ {"dd", "4"},
+ {"bab", "3"},
+ {"baa", "3"},
+ {"bcma", "3"},
+ {"cdma", "3"},
+ {"c", "5"},
+ {"x", "6"},
+ };
+
+ for (size_t i = 0; i < records.size(); i++) {
+ nlohmann::json doc;
+
+ doc["id"] = std::to_string(i);
+ doc["title"] = records[i][0];
+ doc["points"] = std::stoi(records[i][1]);
+
+ ASSERT_TRUE(coll1->add(doc.dump()).ok());
+ }
+
+ std::vector<sort_by> sort_fields = {
+ sort_by("points", "ASC"),
+ sort_by("title", "ASC"),
+ };
+
+ auto results = coll1->search("*", {}, "", {}, sort_fields, {0}, 20, 1, FREQUENCY, {true}, 10).get();
+
+ ASSERT_EQ("a", results["hits"][0]["document"]["title"].get<std::string>());
+ ASSERT_EQ("b", results["hits"][1]["document"]["title"].get<std::string>());
+ ASSERT_EQ("c", results["hits"][2]["document"]["title"].get<std::string>());
+ ASSERT_EQ("abdde", results["hits"][3]["document"]["title"].get<std::string>());
+ ASSERT_EQ("b", results["hits"][4]["document"]["title"].get<std::string>());
+ ASSERT_EQ("baa", results["hits"][5]["document"]["title"].get<std::string>());
+
+ sort_fields = {
+ sort_by("_text_match", "DESC"),
+ sort_by("points", "ASC"),
+ sort_by("title", "ASC"),
+ };
+
+ results = coll1->search("b", {"title"}, "", {}, sort_fields, {0}, 20, 1, FREQUENCY, {true}, 10).get();
+
+ ASSERT_EQ("b", results["hits"][0]["document"]["title"].get<std::string>());
+ ASSERT_EQ("b", results["hits"][1]["document"]["title"].get<std::string>());
+ ASSERT_EQ("baa", results["hits"][2]["document"]["title"].get<std::string>());
+ ASSERT_EQ("bab", results["hits"][3]["document"]["title"].get<std::string>());
+ ASSERT_EQ("bcma", results["hits"][4]["document"]["title"].get<std::string>());
+
+ collectionManager.drop_collection("coll1");
+}
+
TEST_F(CollectionSortingTest, TextMatchBucketRanking) {
std::vector<field> fields = {field("title", field_types::STRING, false),
field("description", field_types::STRING, false),