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

Give highest priority to field order while ranking.

kishorenc590aed7f0e79
diff --git a/include/match_score.h b/include/match_score.h
index 8e172167..3b1ac48a 100644
--- a/include/match_score.h
+++ b/include/match_score.h
@@ -48,20 +48,25 @@ struct Match {
// Explicit construction of match score
static inline uint64_t get_match_score(const uint32_t words_present, const uint32_t total_cost, const uint8_t distance,
const uint8_t field_id) {
+ uint64_t match_score = (
+ (int64_t(field_id) << 24) |
+ (int64_t(words_present) << 16) |
+ (int64_t(255 - total_cost) << 8) |
+ (int64_t(distance))
+ );
- uint64_t match_score = ((int64_t) (words_present) << 24) |
- ((int64_t) (255 - total_cost) << 16) |
- ((int64_t) (distance) << 8) |
- ((int64_t) (field_id));
return match_score;
}
// Construct a single match score from individual components (for multi-field sort)
inline uint64_t get_match_score(const uint32_t total_cost, const uint8_t field_id) const {
- uint64_t match_score = ((int64_t) (words_present) << 24) |
- ((int64_t) (255 - total_cost) << 16) |
- ((int64_t) (distance) << 8) |
- ((int64_t) (field_id));
+ uint64_t match_score = (
+ (int64_t(field_id) << 24) |
+ (int64_t(words_present) << 16) |
+ (int64_t(255 - total_cost) << 8) |
+ (int64_t(distance))
+ );
+
return match_score;
}
diff --git a/src/index.cpp b/src/index.cpp
index 07b23b7f..8d7d0305 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -1373,6 +1373,8 @@ void Index::search(Option<uint32_t> & outcome,
}
}
+ std::vector<Topster*> ftopsters;
+
if(!q_include_tokens.empty() && q_include_tokens[0] == "*") {
const uint8_t field_id = (uint8_t)(FIELD_LIMIT_NUM - 0);
const std::string & field = search_fields[0];
@@ -1435,7 +1437,6 @@ void Index::search(Option<uint32_t> & outcome,
filter_ids = nullptr;
} else {
spp::sparse_hash_map<uint64_t, std::vector<KV*>> topster_ids;
- std::vector<Topster*> ftopsters;
// non-wildcard
for(size_t i = 0; i < num_search_fields; i++) {
@@ -1485,7 +1486,7 @@ void Index::search(Option<uint32_t> & outcome,
const auto& kvs = key_kvs.second;
const uint64_t seq_id = key_kvs.first;
- //LOG(INFO) << "DOC ID: " << seq_id;
+ // LOG(INFO) << "DOC ID: " << seq_id << ", score: " << kvs[0]->scores[kvs[0]->match_score_index];
/*if(seq_id == 12 || seq_id == 15) {
LOG(INFO) << "here";
@@ -1556,10 +1557,6 @@ void Index::search(Option<uint32_t> & outcome,
//LOG(INFO) << "kvs[0].key: " << kvs[0]->key;
topster->add(kvs[0]);
}
-
- for(Topster* ftopster: ftopsters) {
- delete ftopster;
- }
}
//LOG(INFO) << "topster size: " << topster->size;
@@ -1574,6 +1571,10 @@ void Index::search(Option<uint32_t> & outcome,
delete [] filter_ids;
delete [] all_result_ids;
+ for(Topster* ftopster: ftopsters) {
+ delete ftopster;
+ }
+
//long long int timeMillis = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - begin).count();
//!LOG(INFO) << "Time taken for result calc: " << timeMillis << "us";
diff --git a/test/collection_grouping_test.cpp b/test/collection_grouping_test.cpp
index 74132378..3ef5da56 100644
--- a/test/collection_grouping_test.cpp
+++ b/test/collection_grouping_test.cpp
@@ -301,14 +301,14 @@ TEST_F(CollectionGroupingTest, GroupingWithMultiFieldRelevance) {
ASSERT_STREQ("1", results["grouped_hits"][0]["hits"][0]["document"]["id"].get<std::string>().c_str());
ASSERT_STREQ("4", results["grouped_hits"][0]["hits"][1]["document"]["id"].get<std::string>().c_str());
- ASSERT_STREQ("rock", results["grouped_hits"][1]["group_key"][0].get<std::string>().c_str());
- ASSERT_EQ(1, results["grouped_hits"][1]["hits"].size());
- ASSERT_STREQ("5", results["grouped_hits"][1]["hits"][0]["document"]["id"].get<std::string>().c_str());
-
- ASSERT_STREQ("country", results["grouped_hits"][2]["group_key"][0].get<std::string>().c_str());
- ASSERT_EQ(2, results["grouped_hits"][2]["hits"].size());
- ASSERT_STREQ("8", results["grouped_hits"][2]["hits"][0]["document"]["id"].get<std::string>().c_str());
- ASSERT_STREQ("3", results["grouped_hits"][2]["hits"][1]["document"]["id"].get<std::string>().c_str());
+ ASSERT_STREQ("country", results["grouped_hits"][1]["group_key"][0].get<std::string>().c_str());
+ ASSERT_EQ(2, results["grouped_hits"][1]["hits"].size());
+ ASSERT_STREQ("8", results["grouped_hits"][1]["hits"][0]["document"]["id"].get<std::string>().c_str());
+ ASSERT_STREQ("3", results["grouped_hits"][1]["hits"][1]["document"]["id"].get<std::string>().c_str());
+
+ ASSERT_STREQ("rock", results["grouped_hits"][2]["group_key"][0].get<std::string>().c_str());
+ ASSERT_EQ(1, results["grouped_hits"][2]["hits"].size());
+ ASSERT_STREQ("5", results["grouped_hits"][2]["hits"][0]["document"]["id"].get<std::string>().c_str());
collectionManager.drop_collection("coll1");
}
diff --git a/test/collection_synonyms_test.cpp b/test/collection_synonyms_test.cpp
index 4b1a973d..9c597d6c 100644
--- a/test/collection_synonyms_test.cpp
+++ b/test/collection_synonyms_test.cpp
@@ -359,8 +359,8 @@ TEST_F(CollectionSynonymsTest, MultiWaySynonym) {
ASSERT_EQ(2, res["hits"].size());
ASSERT_EQ(2, res["found"].get<uint32_t>());
- ASSERT_STREQ("<mark>Samuel</mark> <mark>L.</mark> <mark>Jackson</mark>", res["hits"][0]["highlights"][0]["snippet"].get<std::string>().c_str());
- ASSERT_STREQ("<mark>Samuel</mark> <mark>L.</mark> <mark>Jackson</mark>", res["hits"][1]["highlights"][0]["snippet"].get<std::string>().c_str());
+ ASSERT_STREQ("<mark>Samuel</mark> L. <mark>Jackson</mark>", res["hits"][0]["highlights"][0]["snippet"].get<std::string>().c_str());
+ ASSERT_STREQ("<mark>Samuel</mark> L. <mark>Jackson</mark>", res["hits"][1]["highlights"][0]["snippet"].get<std::string>().c_str());
}
TEST_F(CollectionSynonymsTest, ExactMatchRankedHigherThanSynonymMatch) {
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index 7c412c3a..8e8fa060 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -2750,6 +2750,46 @@ TEST_F(CollectionTest, MultiFieldMatchRankingOnArray) {
collectionManager.drop_collection("coll1");
}
+TEST_F(CollectionTest, MultiFieldMatchRankingOnFieldOrder) {
+ Collection *coll1;
+
+ std::vector<field> fields = {field("title", field_types::STRING, false),
+ field("artist", field_types::STRING, false),
+ field("points", field_types::INT32, false),};
+
+ coll1 = collectionManager.get_collection("coll1");
+ if(coll1 == nullptr) {
+ coll1 = collectionManager.create_collection("coll1", 1, fields, "points").get();
+ }
+
+ std::vector<std::vector<std::string>> records = {
+ {"Toxic", "Britney Spears"},
+ {"Bad", "Michael Jackson"},
+ };
+
+ for(size_t i=0; i<records.size(); i++) {
+ nlohmann::json doc;
+
+ doc["id"] = std::to_string(i);
+ doc["title"] = records[i][0];
+ doc["artist"] = records[i][1];
+ doc["points"] = i;
+
+ ASSERT_TRUE(coll1->add(doc.dump()).ok());
+ }
+
+ auto results = coll1->search("michael jackson toxic",
+ {"title", "artist"}, "", {}, {}, 0, 3, 1, FREQUENCY, true, 5).get();
+
+ ASSERT_EQ(2, results["found"].get<size_t>());
+ ASSERT_EQ(2, results["hits"].size());
+
+ ASSERT_STREQ("0", results["hits"][0]["document"]["id"].get<std::string>().c_str());
+ ASSERT_STREQ("1", results["hits"][1]["document"]["id"].get<std::string>().c_str());
+
+ collectionManager.drop_collection("coll1");
+}
+
TEST_F(CollectionTest, HighlightWithAccentedCharacters) {
Collection *coll1;