Float fields used as sort field should accept integers.

This commit is contained in:
kishorenc 2020-02-20 06:34:31 +05:30
parent 9cbc90f2b7
commit 412efbb500
3 changed files with 19 additions and 4 deletions

View File

@ -130,11 +130,11 @@ Option<uint32_t> Index::index_in_memory(const nlohmann::json &document, uint32_t
field_pair.second.type == field_types::FLOAT || field_pair.second.type == field_types::BOOL) {
spp::sparse_hash_map<uint32_t, number_t> *doc_to_score = sort_index.at(field_pair.first);
if(document[field_pair.first].is_number_integer()) {
if(field_pair.second.is_integer() ) {
doc_to_score->emplace(seq_id, document[field_pair.first].get<int64_t>());
} else if(document[field_pair.first].is_number_float()) {
} else if(field_pair.second.is_float()) {
doc_to_score->emplace(seq_id, document[field_pair.first].get<float>());
} else if(document[field_pair.first].is_boolean()) {
} else if(field_pair.second.is_bool()) {
doc_to_score->emplace(seq_id, (int64_t) document[field_pair.first].get<bool>());
}
}

View File

@ -435,6 +435,21 @@ TEST_F(CollectionTest, WildcardQuery) {
ASSERT_TRUE(results_op.ok());
ASSERT_EQ(3, results["hits"].size());
ASSERT_EQ(25, results["found"].get<uint32_t>());
// wildcard query with no filters and ASC sort
std::vector<sort_by> sort_fields = { sort_by("points", "ASC") };
results = collection->search("*", query_fields, "", {}, sort_fields, 0, 3, 1, FREQUENCY, false).get();
ASSERT_EQ(3, results["hits"].size());
ASSERT_EQ(25, results["found"].get<uint32_t>());
std::vector<std::string> ids = {"21", "24", "17"};
for(size_t i = 0; i < results["hits"].size(); i++) {
nlohmann::json result = results["hits"].at(i);
std::string result_id = result["document"]["id"];
std::string id = ids.at(i);
ASSERT_STREQ(id.c_str(), result_id.c_str());
}
}
TEST_F(CollectionTest, PrefixSearching) {

View File

@ -3,5 +3,5 @@
{"title": "Jeremy Howard", "score": 7.812, "average": 0.001 }
{"title": "Jeremy Howard", "score": 0.0, "average": 11.533 }
{"title": "Jeremy Howard", "score": -9.999, "average": -11.38 }
{"title": "Jeremy Howard", "score": -9.999, "average": 19.38 }
{"title": "Jeremy Howard", "score": -9.999, "average": 300 }
{"title": "Jeremy Howard", "score": -9.999, "average": -21.38 }