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

Prioritize highlight zone matching over total count.

Kishore Nallan0710addecc83
diff --git a/src/index.cpp b/src/index.cpp
index 26a64855..7fb73c31 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -4160,8 +4160,8 @@ int64_t Index::score_results2(const std::vector<sort_by> & sort_fields, const ui
// might be available within the window used for proximity calculation (this_words_present)
uint64_t mod_match_score = (
- (int64_t(unique_words) << 32) |
- (int64_t(this_words_present) << 24) |
+ (int64_t(this_words_present) << 32) |
+ (int64_t(unique_words) << 24) |
(int64_t(typo_score) << 16) |
(int64_t(proximity) << 8) |
(int64_t(verbatim) << 0)
@@ -4172,11 +4172,13 @@ int64_t Index::score_results2(const std::vector<sort_by> & sort_fields, const ui
}
/*std::ostringstream os;
- os << name << ", total_cost: " << (255 - total_cost)
- << ", words_present: " << match.words_present
- << ", match_score: " << match_score
- << ", match.distance: " << match.distance
- << ", seq_id: " << seq_id << std::endl;
+ os << "seq_id: " << seq_id
+ << ", this_words_present: " << this_words_present
+ << ", unique_words: " << unique_words
+ << ", typo_score: " << typo_score
+ << ", proximity: " << proximity
+ << ", verbatim: " << verbatim
+ << std::endl;
LOG(INFO) << os.str();*/
}
}
diff --git a/test/collection_specific_more_test.cpp b/test/collection_specific_more_test.cpp
index 44f08453..ba8e1d1e 100644
--- a/test/collection_specific_more_test.cpp
+++ b/test/collection_specific_more_test.cpp
@@ -85,3 +85,85 @@ TEST_F(CollectionSpecificMoreTest, PrefixExpansionWhenExactMatchExists) {
ASSERT_EQ(2, results["hits"].size());
collectionManager.drop_collection("coll1");
}
+
+TEST_F(CollectionSpecificMoreTest, ArrayElementMatchShouldBeMoreImportantThanTotalMatch) {
+ std::vector<field> fields = {field("title", field_types::STRING, false),
+ field("author", field_types::STRING, false),
+ field("tags", field_types::STRING_ARRAY, false),};
+
+ Collection* coll1 = collectionManager.create_collection("coll1", 1, fields).get();
+
+ nlohmann::json doc1;
+ doc1["id"] = "0";
+ doc1["title"] = "Harry Potter and the Prisoner of Azkaban";
+ doc1["author"] = "Rowling";
+ doc1["tags"] = {"harry", ""};
+
+ nlohmann::json doc2;
+ doc2["id"] = "1";
+ doc2["title"] = "Fantastic beasts and where to find them";
+ doc2["author"] = "Rowling";
+ doc2["tags"] = {"harry", "potter", "prisoner", "azkaban", "beasts", "guide", "rowling"};
+
+ nlohmann::json doc3;
+ doc3["id"] = "2";
+ doc3["title"] = "Fantastic beasts and where to find them";
+ doc3["author"] = "Rowling";
+ doc3["tags"] = {"harry potter", "prisoner azkaban", "beasts", "guide", "rowling"};
+
+ ASSERT_TRUE(coll1->add(doc1.dump()).ok());
+ ASSERT_TRUE(coll1->add(doc2.dump()).ok());
+ ASSERT_TRUE(coll1->add(doc3.dump()).ok());
+
+ auto results = coll1->search("harry potter rowling prisoner azkaban", {"title", "author", "tags"},
+ "", {}, {}, {2}, 10,
+ 1, FREQUENCY, {true},
+ 1, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "title", 5, {}, {}, {}, 0,
+ "<mark>", "</mark>", {}, 1000, true).get();
+
+ ASSERT_EQ(3, results["hits"].size());
+ ASSERT_EQ("0", results["hits"][0]["document"]["id"].get<std::string>());
+ ASSERT_EQ("2", results["hits"][1]["document"]["id"].get<std::string>());
+ ASSERT_EQ("1", results["hits"][2]["document"]["id"].get<std::string>());
+}
+
+TEST_F(CollectionSpecificMoreTest, MatchedSegmentMoreImportantThanTotalMatches) {
+ std::vector<field> fields = {field("title", field_types::STRING, false),
+ field("author", field_types::STRING, false)};
+
+ Collection* coll1 = collectionManager.create_collection("coll1", 1, fields).get();
+
+ nlohmann::json doc1;
+ doc1["id"] = "0";
+ doc1["title"] = "One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve Thirteen Fourteen";
+ doc1["author"] = "Rowling";
+
+ nlohmann::json doc2;
+ doc2["id"] = "1";
+ doc2["title"] = "One Four Five Six Seven Eight Nine Ten Eleven Twelve Thirteen Fourteen Three Rowling";
+ doc2["author"] = "Two";
+
+ nlohmann::json doc3;
+ doc3["id"] = "2";
+ doc3["title"] = "One Three Four Five Six Seven Eight Nine Ten Eleven Twelve Thirteen Fourteen Two Rowling";
+ doc3["author"] = "Foo";
+
+ ASSERT_TRUE(coll1->add(doc1.dump()).ok());
+ ASSERT_TRUE(coll1->add(doc2.dump()).ok());
+ ASSERT_TRUE(coll1->add(doc3.dump()).ok());
+
+ auto results = coll1->search("one two three rowling", {"title", "author"},
+ "", {}, {}, {2}, 10,
+ 1, FREQUENCY, {true},
+ 1, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "title", 5, {}, {}, {}, 0,
+ "<mark>", "</mark>", {}, 1000, true).get();
+
+ LOG(INFO) << results;
+ ASSERT_EQ(3, results["hits"].size());
+
+ ASSERT_EQ("0", results["hits"][0]["document"]["id"].get<std::string>());
+ ASSERT_EQ("2", results["hits"][1]["document"]["id"].get<std::string>());
+ ASSERT_EQ("1", results["hits"][2]["document"]["id"].get<std::string>());
+}