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

Add total matching fields to match score.

Kishore Nallanaa3eddf9f4df
diff --git a/include/collection.h b/include/collection.h
index dbcd554a..61b0acc9 100644
--- a/include/collection.h
+++ b/include/collection.h
@@ -407,5 +407,11 @@ public:
std::vector<highlight_field_t>& highlight_items) const;
Option<bool> alter(nlohmann::json& alter_payload);
+
+ void
+ process_search_field_weights(const std::vector<std::string>& raw_search_fields,
+ std::vector<uint32_t>& query_by_weights,
+ std::vector<search_field_t>& weighted_search_fields,
+ std::vector<std::string>& reordered_search_fields) const;
};
diff --git a/include/index.h b/include/index.h
index 606bfbf2..896393c6 100644
--- a/include/index.h
+++ b/include/index.h
@@ -68,7 +68,6 @@ struct tok_candidates {
struct search_field_t {
std::string name;
- size_t priority;
size_t weight;
};
@@ -702,6 +701,9 @@ public:
// for limiting number of fields that can be searched on
enum {FIELD_LIMIT_NUM = 100};
+ // Values 0 to 15 are allowed
+ enum {FIELD_MAX_WEIGHT = 15};
+
enum {COMBINATION_MAX_LIMIT = 10000};
enum {COMBINATION_MIN_LIMIT = 10};
enum {MAX_CANDIDATES_DEFAULT = 4};
diff --git a/src/collection.cpp b/src/collection.cpp
index ebc671a4..b964013a 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -725,7 +725,8 @@ Option<bool> Collection::validate_and_standardize_sort_fields(const std::vector<
return Option<bool>(true);
}
-Option<nlohmann::json> Collection::search(const std::string & raw_query, const std::vector<std::string>& search_fields,
+Option<nlohmann::json> Collection::search(const std::string & raw_query,
+ const std::vector<std::string>& raw_search_fields,
const std::string & simple_filter_query, const std::vector<std::string>& facet_fields,
const std::vector<sort_by> & sort_fields, const std::vector<uint32_t>& num_typos,
const size_t per_page, const size_t page,
@@ -771,11 +772,11 @@ Option<nlohmann::json> Collection::search(const std::string & raw_query, const s
search_begin = std::chrono::high_resolution_clock::now();
search_cutoff = false;
- if(raw_query != "*" && search_fields.empty()) {
+ if(raw_query != "*" && raw_search_fields.empty()) {
return Option<nlohmann::json>(400, "No search fields specified for the query.");
}
- if(!search_fields.empty() && !query_by_weights.empty() && search_fields.size() != query_by_weights.size()) {
+ if(!raw_search_fields.empty() && !query_by_weights.empty() && raw_search_fields.size() != query_by_weights.size()) {
return Option<nlohmann::json>(400, "Number of weights in `query_by_weights` does not match "
"number of `query_by` fields.");
}
@@ -785,21 +786,21 @@ Option<nlohmann::json> Collection::search(const std::string & raw_query, const s
std::to_string(GROUP_LIMIT_MAX) + ".");
}
- if(!search_fields.empty() && search_fields.size() != num_typos.size()) {
+ if(!raw_search_fields.empty() && raw_search_fields.size() != num_typos.size()) {
if(num_typos.size() != 1) {
return Option<nlohmann::json>(400, "Number of weights in `num_typos` does not match "
"number of `query_by` fields.");
}
}
- if(!search_fields.empty() && search_fields.size() != prefixes.size()) {
+ if(!raw_search_fields.empty() && raw_search_fields.size() != prefixes.size()) {
if(prefixes.size() != 1) {
return Option<nlohmann::json>(400, "Number of prefix values in `prefix` does not match "
"number of `query_by` fields.");
}
}
- if(!search_fields.empty() && search_fields.size() != infixes.size()) {
+ if(!raw_search_fields.empty() && raw_search_fields.size() != infixes.size()) {
if(infixes.size() != 1) {
return Option<nlohmann::json>(400, "Number of infix values in `infix` does not match "
"number of `query_by` fields.");
@@ -810,32 +811,8 @@ Option<nlohmann::json> Collection::search(const std::string & raw_query, const s
group_limit = 0;
}
- // process weights for search fields
- std::vector<search_field_t> weighted_search_fields;
- size_t max_weight = 20;
-
- if(query_by_weights.empty()) {
- max_weight = search_fields.size();
- for(size_t i=1; i <= search_fields.size(); i++) {
- query_by_weights.push_back((max_weight - i) + 1);
- }
- } else {
- max_weight = *std::max_element(query_by_weights.begin(), query_by_weights.end());
- }
-
- for(size_t i=0; i < search_fields.size(); i++) {
- const auto& search_field = search_fields[i];
- // NOTE: we support zero-weight only for weighting and not priority since priority is used for typos, where
- // relative ordering is still useful.
- const auto weight = query_by_weights[i];
- const auto priority = (max_weight - query_by_weights[i]) + 1;
- weighted_search_fields.push_back({search_field, priority, weight});
- }
-
- std::vector<facet> facets;
-
// validate search fields
- for(const std::string & field_name: search_fields) {
+ for(const std::string & field_name: raw_search_fields) {
if(search_schema.count(field_name) == 0) {
std::string error = "Could not find a field named `" + field_name + "` in the schema.";
return Option<nlohmann::json>(404, error);
@@ -870,6 +847,15 @@ Option<nlohmann::json> Collection::search(const std::string & raw_query, const s
}
}
+ // process weights for search fields
+ std::vector<std::string> reordered_search_fields;
+ std::vector<search_field_t> weighted_search_fields;
+ process_search_field_weights(raw_search_fields, query_by_weights, weighted_search_fields, reordered_search_fields);
+
+ const std::vector<std::string>& search_fields = reordered_search_fields.empty() ? raw_search_fields
+ : reordered_search_fields;
+ std::vector<facet> facets;
+
const std::string doc_id_prefix = std::to_string(collection_id) + "_" + DOC_ID_PREFIX + "_";
std::vector<filter> filters;
Option<bool> parse_filter_op = filter::parse_filter_query(simple_filter_query, search_schema,
@@ -1555,6 +1541,75 @@ Option<nlohmann::json> Collection::search(const std::string & raw_query, const s
return Option<nlohmann::json>(result);
}
+void Collection::process_search_field_weights(const std::vector<std::string>& raw_search_fields,
+ std::vector<uint32_t>& query_by_weights,
+ std::vector<search_field_t>& weighted_search_fields,
+ std::vector<std::string>& reordered_search_fields) const {
+ const bool weights_given = !query_by_weights.empty();
+
+ // weights, if given, must be in desc order
+ bool weights_in_desc_order = true;
+ bool weights_undex_max = true;
+
+ for(size_t i=0; i < raw_search_fields.size(); i++) {
+ if(!weights_given) {
+ size_t weight = std::max<int>(0, (int(Index::FIELD_MAX_WEIGHT) - i));
+ query_by_weights.push_back(weight);
+ weighted_search_fields.push_back({raw_search_fields[i], weight});
+ } else {
+ // check if weights are already sorted
+ auto prev_weight = (i == 0) ? query_by_weights[0] : query_by_weights[i-1];
+ weights_in_desc_order = weights_in_desc_order && (query_by_weights[i] <= prev_weight);
+ weights_undex_max = weights_undex_max && (query_by_weights[i] <= Index::FIELD_MAX_WEIGHT);
+ }
+ }
+
+ if(weights_given && (!weights_in_desc_order || !weights_undex_max)) {
+ // ensure that search fields are sorted on their corresponding weight
+ std::vector<std::pair<size_t, size_t>> field_index_and_weights;
+
+ for(size_t i=0; i < raw_search_fields.size(); i++) {
+ field_index_and_weights.emplace_back(i, query_by_weights[i]);
+ }
+
+ std::sort(field_index_and_weights.begin(), field_index_and_weights.end(), [](const auto& a, const auto& b) {
+ return a.second > b.second;
+ });
+
+ for(size_t i = 0; i < field_index_and_weights.size(); i++) {
+ const auto& index_weight = field_index_and_weights[i];
+ reordered_search_fields.push_back(raw_search_fields[index_weight.first]);
+
+ // we have to also normalize weights to 0 to Index::FIELD_MAX_WEIGHT range.
+ if(i == 0) {
+ query_by_weights[i] = Index::FIELD_MAX_WEIGHT;
+ } else {
+ auto curr_weight = field_index_and_weights[i].second;
+ auto prev_weight = field_index_and_weights[i-1].second;
+
+ if(curr_weight == prev_weight) {
+ query_by_weights[i] = query_by_weights[i-1];
+ } else {
+ // bound to be lesser than prev_weight since weights have been sorted desc
+ uint32_t bounded_weight = std::max(0, int(query_by_weights[i-1]) - 1);
+ query_by_weights[i] = bounded_weight;
+ }
+ }
+ }
+ }
+
+ if(weighted_search_fields.empty()) {
+ const std::vector<std::string>& search_fields = reordered_search_fields.empty() ? raw_search_fields
+ : reordered_search_fields;
+
+ for(size_t i=0; i < search_fields.size(); i++) {
+ const auto& search_field = search_fields[i];
+ const auto weight = query_by_weights[i];
+ weighted_search_fields.push_back({search_field, weight});
+ }
+ }
+}
+
void Collection::process_highlight_fields(const std::vector<std::string>& search_fields,
const spp::sparse_hash_set<std::string>& include_fields,
const spp::sparse_hash_set<std::string>& exclude_fields,
diff --git a/src/index.cpp b/src/index.cpp
index 95772c48..d0ff568f 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -3129,6 +3129,7 @@ void Index::search_across_fields(const std::vector<token_t>& query_tokens,
}
int64_t max_field_match_score = 0, max_field_match_index = 0;
+ uint32_t num_matching_fields = 0;
for(size_t fi = 0; fi < field_to_tokens.size(); fi++) {
const std::vector<posting_list_t::iterator_t>& token_postings = field_to_tokens[fi];
@@ -3155,6 +3156,8 @@ void Index::search_across_fields(const std::vector<token_t>& query_tokens,
max_field_match_score = field_match_score;
max_field_match_index = fi;
}
+
+ num_matching_fields++;
}
uint64_t distinct_id = seq_id;
@@ -3173,19 +3176,27 @@ void Index::search_across_fields(const std::vector<token_t>& query_tokens,
if(syn_orig_num_tokens != -1) {
query_len = syn_orig_num_tokens;
}
+ query_len = std::min<size_t>(15, query_len);
// NOTE: `query_len` is total tokens matched across fields.
// Within a field, only a subset can match
- uint64_t aggregated_score = (int64_t(query_len) << 56) |
+ // [ query_len | max_field_score | max_field_weight | num_matching_fields]
+ // [ 4 | 48 | 8 | 3 ] (63 bits)
+
+ auto max_field_weight = std::min<size_t>(FIELD_MAX_WEIGHT, the_fields[max_field_match_index].weight);
+ num_matching_fields = std::min<size_t>(7, num_matching_fields);
+
+ uint64_t aggregated_score = (int64_t(query_len) << 59) |
(int64_t(max_field_match_score) << 8) |
- (int64_t(the_fields[max_field_match_index].weight) << 0);
+ (int64_t(max_field_weight) << 4) |
+ (int64_t(num_matching_fields) << 0);
/*LOG(INFO) << "seq_id: " << seq_id << ", query_tokens.size(): " << query_tokens.size()
<< ", syn_orig_num_tokens: " << syn_orig_num_tokens
<< ", max_field_match_score: " << max_field_match_score
<< ", max_field_match_index: " << max_field_match_index
- << ", field_weight: " << the_fields[max_field_match_index].weight
+ << ", field_weight: " << max_field_weight
<< ", aggregated_score: " << aggregated_score;*/
KV kv(0, searched_queries.size(), 0, seq_id, distinct_id, match_score_index, scores);
diff --git a/test/collection_override_test.cpp b/test/collection_override_test.cpp
index e7ba8bb3..27100308 100644
--- a/test/collection_override_test.cpp
+++ b/test/collection_override_test.cpp
@@ -1261,8 +1261,8 @@ TEST_F(CollectionOverrideTest, DynamicFilteringExactMatchBasics) {
{}, sort_fields, {2, 2, 2}, 10).get();
ASSERT_EQ(3, results["hits"].size());
- ASSERT_EQ("1", results["hits"][0]["document"]["id"].get<std::string>());
- ASSERT_EQ("0", results["hits"][1]["document"]["id"].get<std::string>());
+ ASSERT_EQ("0", results["hits"][0]["document"]["id"].get<std::string>());
+ ASSERT_EQ("1", results["hits"][1]["document"]["id"].get<std::string>());
ASSERT_EQ("2", results["hits"][2]["document"]["id"].get<std::string>());
// with override, results will be different
@@ -1891,8 +1891,8 @@ TEST_F(CollectionOverrideTest, DynamicFilteringWithSynonyms) {
ASSERT_EQ(2, results["hits"].size());
- ASSERT_EQ("1", results["hits"][0]["document"]["id"].get<std::string>());
- ASSERT_EQ("0", results["hits"][1]["document"]["id"].get<std::string>());
+ ASSERT_EQ("0", results["hits"][0]["document"]["id"].get<std::string>());
+ ASSERT_EQ("1", results["hits"][1]["document"]["id"].get<std::string>());
// keyword has no override, but synonym's override is used
results = coll1->search("exciting", {"name", "category", "brand"}, "",
diff --git a/test/collection_sorting_test.cpp b/test/collection_sorting_test.cpp
index 0091d165..e08f1b06 100644
--- a/test/collection_sorting_test.cpp
+++ b/test/collection_sorting_test.cpp
@@ -1729,10 +1729,10 @@ TEST_F(CollectionSortingTest, RepeatingTokenRanking) {
ASSERT_EQ("2", results["hits"][2]["document"]["id"].get<std::string>());
ASSERT_EQ("1", results["hits"][3]["document"]["id"].get<std::string>());
- ASSERT_EQ(144681433946980355, results["hits"][0]["text_match"].get<size_t>());
- ASSERT_EQ(144681433946914819, results["hits"][1]["text_match"].get<size_t>());
- ASSERT_EQ(144681433946914819, results["hits"][2]["text_match"].get<size_t>());
- ASSERT_EQ(144681433946914819, results["hits"][3]["text_match"].get<size_t>());
+ ASSERT_EQ(1153487750477971505, results["hits"][0]["text_match"].get<size_t>());
+ ASSERT_EQ(1153487750477905969, results["hits"][1]["text_match"].get<size_t>());
+ ASSERT_EQ(1153487750477905969, results["hits"][2]["text_match"].get<size_t>());
+ ASSERT_EQ(1153487750477905969, results["hits"][3]["text_match"].get<size_t>());
collectionManager.drop_collection("coll1");
}
diff --git a/test/collection_specific_more_test.cpp b/test/collection_specific_more_test.cpp
index d865f496..d41e811c 100644
--- a/test/collection_specific_more_test.cpp
+++ b/test/collection_specific_more_test.cpp
@@ -748,3 +748,195 @@ TEST_F(CollectionSpecificMoreTest, LongString) {
collectionManager.drop_collection("coll1");
}
+
+TEST_F(CollectionSpecificMoreTest, RelevanceConsiderAllFields) {
+ std::vector<field> fields = {field("f1", field_types::STRING, false),
+ field("f2", field_types::STRING, false),
+ field("f3", field_types::STRING, false),};
+
+ Collection* coll1 = collectionManager.create_collection("coll1", 1, fields).get();
+
+ nlohmann::json doc1;
+ doc1["f1"] = "alpha";
+ doc1["f2"] = "alpha";
+ doc1["f3"] = "alpha";
+
+ ASSERT_TRUE(coll1->add(doc1.dump()).ok());
+
+ doc1["f1"] = "alpha";
+ doc1["f2"] = "alpha";
+ doc1["f3"] = "beta";
+
+ ASSERT_TRUE(coll1->add(doc1.dump()).ok());
+
+ doc1["f1"] = "alpha";
+ doc1["f2"] = "beta";
+ doc1["f3"] = "gamma";
+
+ ASSERT_TRUE(coll1->add(doc1.dump()).ok());
+
+ auto results = coll1->search("alpha", {"f1", "f2", "f3"},
+ "", {}, {}, {2}, 10,
+ 1, FREQUENCY, {true},
+ 0, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "", 40, {}, {}, {}, 0,
+ "<mark>", "</mark>", {3, 2, 1}).get();
+
+ ASSERT_EQ(3, results["hits"].size());
+ ASSERT_EQ("0", results["hits"][0]["document"]["id"].get<std::string>());
+ ASSERT_EQ("1", results["hits"][1]["document"]["id"].get<std::string>());
+ ASSERT_EQ("2", results["hits"][2]["document"]["id"].get<std::string>());
+
+ collectionManager.drop_collection("coll1");
+}
+
+TEST_F(CollectionSpecificMoreTest, CrossFieldWeightIsNotAugmentated) {
+ std::vector<field> fields = {field("title", field_types::STRING, false),
+ field("type", field_types::STRING, false)};
+
+ Collection* coll1 = collectionManager.create_collection("coll1", 1, fields).get();
+
+ nlohmann::json doc1;
+ doc1["title"] = "Nike Shoerack";
+ doc1["type"] = "shoe_rack";
+
+ ASSERT_TRUE(coll1->add(doc1.dump()).ok());
+
+ doc1["title"] = "Nike Air Force 1";
+ doc1["type"] = "shoe";
+
+ ASSERT_TRUE(coll1->add(doc1.dump()).ok());
+
+ auto results = coll1->search("nike shoe", {"type", "title"},
+ "", {}, {}, {2}, 10,
+ 1, FREQUENCY, {true},
+ 0, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "", 40, {}, {}, {}, 0,
+ "<mark>", "</mark>", {5, 1}).get();
+
+ ASSERT_EQ(2, results["hits"].size());
+ ASSERT_EQ("0", results["hits"][0]["document"]["id"].get<std::string>());
+ ASSERT_EQ("1", results["hits"][1]["document"]["id"].get<std::string>());
+
+ collectionManager.drop_collection("coll1");
+}
+
+TEST_F(CollectionSpecificMoreTest, FieldWeightNormalization) {
+ std::vector<field> fields = {field("title", field_types::STRING, false),
+ field("brand", field_types::STRING, false),
+ field("type", field_types::STRING, false)};
+
+ Collection* coll1 = collectionManager.create_collection("coll1", 1, fields).get();
+
+ std::vector<std::string> raw_search_fields = {"title", "brand", "type"};
+ std::vector<uint32_t> query_by_weights = {110, 25, 55};
+ std::vector<search_field_t> weighted_search_fields;
+ std::vector<std::string> reordered_search_fields;
+
+ coll1->process_search_field_weights(raw_search_fields, query_by_weights, weighted_search_fields,
+ reordered_search_fields);
+
+ ASSERT_EQ(3, reordered_search_fields.size());
+ ASSERT_EQ(3, weighted_search_fields.size());
+
+ ASSERT_EQ("title", weighted_search_fields[0].name);
+ ASSERT_EQ("type", weighted_search_fields[1].name);
+ ASSERT_EQ("brand", weighted_search_fields[2].name);
+
+ ASSERT_EQ(15, weighted_search_fields[0].weight);
+ ASSERT_EQ(14, weighted_search_fields[1].weight);
+ ASSERT_EQ(13, weighted_search_fields[2].weight);
+
+ // same weights
+ weighted_search_fields.clear();
+ reordered_search_fields.clear();
+ query_by_weights = {15, 15, 15};
+
+ coll1->process_search_field_weights(raw_search_fields, query_by_weights, weighted_search_fields,
+ reordered_search_fields);
+
+ ASSERT_EQ("title", weighted_search_fields[0].name);
+ ASSERT_EQ("brand", weighted_search_fields[1].name);
+ ASSERT_EQ("type", weighted_search_fields[2].name);
+
+ ASSERT_EQ(15, weighted_search_fields[0].weight);
+ ASSERT_EQ(15, weighted_search_fields[1].weight);
+ ASSERT_EQ(15, weighted_search_fields[2].weight);
+
+ // same weights large
+ weighted_search_fields.clear();
+ reordered_search_fields.clear();
+ query_by_weights = {800, 800, 800};
+
+ coll1->process_search_field_weights(raw_search_fields, query_by_weights, weighted_search_fields,
+ reordered_search_fields);
+
+ ASSERT_EQ("title", weighted_search_fields[0].name);
+ ASSERT_EQ("brand", weighted_search_fields[1].name);
+ ASSERT_EQ("type", weighted_search_fields[2].name);
+
+ ASSERT_EQ(15, weighted_search_fields[0].weight);
+ ASSERT_EQ(15, weighted_search_fields[1].weight);
+ ASSERT_EQ(15, weighted_search_fields[2].weight);
+
+ // weights desc ordered but exceed max weight
+ weighted_search_fields.clear();
+ reordered_search_fields.clear();
+ query_by_weights = {603, 602, 601};
+
+ coll1->process_search_field_weights(raw_search_fields, query_by_weights, weighted_search_fields,
+ reordered_search_fields);
+
+ ASSERT_EQ("title", weighted_search_fields[0].name);
+ ASSERT_EQ("brand", weighted_search_fields[1].name);
+ ASSERT_EQ("type", weighted_search_fields[2].name);
+
+ ASSERT_EQ(15, weighted_search_fields[0].weight);
+ ASSERT_EQ(14, weighted_search_fields[1].weight);
+ ASSERT_EQ(13, weighted_search_fields[2].weight);
+
+ // number of fields > 15 (must cap least important fields to weight 0)
+ raw_search_fields.clear();
+ weighted_search_fields.clear();
+ reordered_search_fields.clear();
+ query_by_weights.clear();
+
+ for(size_t i = 0; i < 17; i++) {
+ raw_search_fields.push_back("field" + std::to_string(17 - i));
+ query_by_weights.push_back(17 - i);
+ }
+
+ coll1->process_search_field_weights(raw_search_fields, query_by_weights, weighted_search_fields,
+ reordered_search_fields);
+
+ ASSERT_EQ("field3", weighted_search_fields[14].name);
+ ASSERT_EQ("field2", weighted_search_fields[15].name);
+ ASSERT_EQ("field1", weighted_search_fields[16].name);
+
+ ASSERT_EQ(1, weighted_search_fields[14].weight);
+ ASSERT_EQ(0, weighted_search_fields[15].weight);
+ ASSERT_EQ(0, weighted_search_fields[16].weight);
+
+ // when weights are not given
+ raw_search_fields.clear();
+ weighted_search_fields.clear();
+ reordered_search_fields.clear();
+ query_by_weights.clear();
+
+ for(size_t i = 0; i < 17; i++) {
+ raw_search_fields.push_back("field" + std::to_string(17 - i));
+ }
+
+ coll1->process_search_field_weights(raw_search_fields, query_by_weights, weighted_search_fields,
+ reordered_search_fields);
+
+ ASSERT_EQ("field3", weighted_search_fields[14].name);
+ ASSERT_EQ("field2", weighted_search_fields[15].name);
+ ASSERT_EQ("field1", weighted_search_fields[16].name);
+
+ ASSERT_EQ(1, weighted_search_fields[14].weight);
+ ASSERT_EQ(0, weighted_search_fields[15].weight);
+ ASSERT_EQ(0, weighted_search_fields[16].weight);
+
+ collectionManager.drop_collection("coll1");
+}
diff --git a/test/collection_specific_test.cpp b/test/collection_specific_test.cpp
index 37acffe2..91c257a0 100644
--- a/test/collection_specific_test.cpp
+++ b/test/collection_specific_test.cpp
@@ -1505,7 +1505,6 @@ TEST_F(CollectionSpecificTest, MultiFieldVerbatimMatchesShouldBeWeighted) {
}
TEST_F(CollectionSpecificTest, ZeroWeightedField) {
- // 2 matches on low weighted fields should not overpower a single match on high weighted field
std::vector<field> fields = {field("name", field_types::STRING, false),
field("category", field_types::STRING, false),
field("points", field_types::INT32, false),};
@@ -1527,16 +1526,16 @@ TEST_F(CollectionSpecificTest, ZeroWeightedField) {
ASSERT_TRUE(coll1->add(doc1.dump()).ok());
ASSERT_TRUE(coll1->add(doc2.dump()).ok());
- auto results = coll1->search("kids", {"name", "category"},
+ auto results = coll1->search("kids", {"category", "name"},
"", {}, {}, {0, 0}, 10,
1, FREQUENCY, {false, false},
2, spp::sparse_hash_set<std::string>(),
spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "", 10, {}, {}, {}, 0,
- "<mark>", "</mark>", {0, 1}).get();
+ "<mark>", "</mark>", {1, 0}).get();
ASSERT_EQ(2, results["hits"].size());
- ASSERT_EQ("1", results["hits"][0]["document"]["id"].get<std::string>());
- ASSERT_EQ("0", results["hits"][1]["document"]["id"].get<std::string>());
+ ASSERT_EQ("0", results["hits"][0]["document"]["id"].get<std::string>());
+ ASSERT_EQ("1", results["hits"][1]["document"]["id"].get<std::string>());
collectionManager.drop_collection("coll1");
}
@@ -2594,13 +2593,13 @@ TEST_F(CollectionSpecificTest, HandleLargeWeights) {
doc1["id"] = "0";
doc1["title"] = "foo same";
doc1["description"] = "bar same";
- doc1["points"] = 100;
+ doc1["points"] = 200;
nlohmann::json doc2;
doc2["id"] = "1";
doc2["title"] = "bar same";
doc2["description"] = "foo same";
- doc2["points"] = 200;
+ doc2["points"] = 100;
ASSERT_TRUE(coll1->add(doc1.dump()).ok());
ASSERT_TRUE(coll1->add(doc2.dump()).ok());
@@ -2615,17 +2614,19 @@ TEST_F(CollectionSpecificTest, HandleLargeWeights) {
ASSERT_EQ(2, results["hits"].size());
ASSERT_EQ("1", results["hits"][0]["document"]["id"].get<std::string>());
ASSERT_EQ("0", results["hits"][1]["document"]["id"].get<std::string>());
+ ASSERT_TRUE(results["hits"][0]["text_match"].get<size_t>() > results["hits"][1]["text_match"].get<size_t>());
results = coll1->search("foo same", {"title", "description"},
"", {}, {}, {2, 2}, 10,
1, FREQUENCY, {true, true},
10, spp::sparse_hash_set<std::string>(),
spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "title", 20, {}, {}, {}, 0,
- "<mark>", "</mark>", {1, 128}, 1000, true).get();
+ "<mark>", "</mark>", {128, 130}, 1000, true).get();
ASSERT_EQ(2, results["hits"].size());
ASSERT_EQ("1", results["hits"][0]["document"]["id"].get<std::string>());
ASSERT_EQ("0", results["hits"][1]["document"]["id"].get<std::string>());
+ ASSERT_TRUE(results["hits"][0]["text_match"].get<size_t>() > results["hits"][1]["text_match"].get<size_t>());
collectionManager.drop_collection("coll1");
}
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index e7ef8baa..fedb6a0f 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -3273,8 +3273,8 @@ TEST_F(CollectionTest, MultiFieldRelevance3) {
ASSERT_EQ(2, results["found"].get<size_t>());
ASSERT_EQ(2, results["hits"].size());
- ASSERT_STREQ("1", results["hits"][0]["document"]["id"].get<std::string>().c_str());
- ASSERT_STREQ("0", results["hits"][1]["document"]["id"].get<std::string>().c_str());
+ 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");
}
@@ -3953,8 +3953,8 @@ TEST_F(CollectionTest, FieldSpecificNumTypos) {
ASSERT_EQ(2, results["found"].get<size_t>());
ASSERT_EQ(2, results["hits"].size());
- ASSERT_STREQ("1", results["hits"][0]["document"]["id"].get<std::string>().c_str());
- ASSERT_STREQ("0", results["hits"][1]["document"]["id"].get<std::string>().c_str());
+ 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());
results = coll1->search("tayylor",
{"title", "artist"}, "", {}, {}, {0, 1}, 10, 1, FREQUENCY,
@@ -4091,8 +4091,8 @@ TEST_F(CollectionTest, FieldLevelPrefixConfiguration) {
ASSERT_EQ(2, results["found"].get<size_t>());
ASSERT_EQ(2, results["hits"].size());
- ASSERT_STREQ("1", results["hits"][0]["document"]["id"].get<std::string>().c_str());
- ASSERT_STREQ("0", results["hits"][1]["document"]["id"].get<std::string>().c_str());
+ 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");
}