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

Make the `sort_fields` take order of sorting.

Kishore Nallanf62247cd32f6
diff --git a/TODO.md b/TODO.md
index ccd3c991..d06b6fd4 100644
--- a/TODO.md
+++ b/TODO.md
@@ -30,6 +30,8 @@
- ~~Schema validation during insertion (missing fields + type errors)~~
- ~~Proper score field for ranking tokens~~
- ~~Throw errors when schema is broken~~
+- Desc/Asc ordering with tests
+- Found count is wrong
- Prevent string copy during indexing
- clean special chars before indexing
- Minimum results should be a variable instead of blindly going with max_results
diff --git a/include/collection.h b/include/collection.h
index d28b2d7a..d05a9888 100644
--- a/include/collection.h
+++ b/include/collection.h
@@ -23,7 +23,7 @@ private:
spp::sparse_hash_map<std::string, field> facet_schema;
- std::vector<std::string> rank_fields;
+ std::vector<sort_field> sort_fields;
Store* store;
@@ -31,7 +31,7 @@ private:
spp::sparse_hash_map<std::string, art_tree*> facet_index;
- spp::sparse_hash_map<std::string, spp::sparse_hash_map<uint32_t, int64_t>*> rank_index;
+ spp::sparse_hash_map<std::string, spp::sparse_hash_map<uint32_t, int64_t>*> sort_index;
std::string token_ordering_field;
@@ -51,12 +51,12 @@ private:
void do_facets(std::vector<facet> & facets, uint32_t* result_ids, size_t results_size);
void search_field(std::string & query, const std::string & field, uint32_t *filter_ids, size_t filter_ids_length,
- std::vector<facet> & facets, const std::vector<std::string> & rank_fields, const int num_typos,
+ std::vector<facet> & facets, const std::vector<sort_field> & sort_fields, const int num_typos,
const size_t num_results, Topster<100> &topster, size_t & num_found,
const token_ordering token_order = FREQUENCY, const bool prefix = false);
void search_candidates(uint32_t* filter_ids, size_t filter_ids_length, std::vector<facet> & facets,
- const std::vector<std::string> & rank_fields, int & token_rank,
+ const std::vector<sort_field> & sort_fields, int & token_rank,
std::vector<std::vector<art_leaf*>> & token_leaves, Topster<100> & topster,
size_t & total_results, size_t & num_found, const size_t & max_results);
@@ -82,7 +82,7 @@ public:
Collection(const std::string name, const uint32_t collection_id, const uint32_t next_seq_id, Store *store,
const std::vector<field> & search_fields, const std::vector<field> & facet_fields,
- const std::vector<std::string> & rank_fields, const std::string token_ordering_field);
+ const std::vector<sort_field> & sort_fields, const std::string token_ordering_field);
~Collection();
@@ -100,7 +100,7 @@ public:
std::vector<std::string> get_facet_fields();
- std::vector<std::string> get_rank_fields();
+ std::vector<sort_field> get_sort_fields();
spp::sparse_hash_map<std::string, field> get_schema();
@@ -110,12 +110,12 @@ public:
nlohmann::json search(std::string query, const std::vector<std::string> search_fields,
const std::string & simple_filter_query, const std::vector<std::string> & facet_fields,
- const std::vector<std::string> & rank_fields, const int num_typos,
+ const std::vector<sort_field> & sort_fields, const int num_typos,
const size_t num_results, const token_ordering token_order = FREQUENCY, const bool prefix = false);
void remove(std::string id);
- void score_results(const std::vector<std::string> & rank_fields, const int & token_rank, Topster<100> &topster,
+ void score_results(const std::vector<sort_field> & sort_fields, const int & token_rank, Topster<100> &topster,
const std::vector<art_leaf *> & query_suggestion, const uint32_t *result_ids,
const size_t result_size) const;
diff --git a/include/collection_manager.h b/include/collection_manager.h
index 700be3d7..2a05b803 100644
--- a/include/collection_manager.h
+++ b/include/collection_manager.h
@@ -22,7 +22,7 @@ private:
static constexpr const char* COLLECTION_ID_KEY = "id";
static constexpr const char* COLLECTION_SEARCH_FIELDS_KEY = "search_fields";
static constexpr const char* COLLECTION_FACET_FIELDS_KEY = "facet_fields";
- static constexpr const char* COLLECTION_RANK_FIELDS_KEY = "rank_fields";
+ static constexpr const char* COLLECTION_SORT_FIELDS_KEY = "sort_fields";
static constexpr const char* COLLECTION_TOKEN_ORDERING_FIELD_KEY = "token_ordering_field";
CollectionManager();
@@ -42,7 +42,7 @@ public:
Collection* create_collection(std::string name, const std::vector<field> & search_fields,
const std::vector<field> & facet_fields,
- const std::vector<std::string> & rank_fields,
+ const std::vector<sort_field> & sort_fields,
const std::string & token_ordering_field = "");
Collection* get_collection(std::string collection_name);
diff --git a/include/field.h b/include/field.h
index 90ee5b28..f3673501 100644
--- a/include/field.h
+++ b/include/field.h
@@ -64,6 +64,26 @@ struct filter {
}
};
+namespace sort_field_const {
+ static const std::string name = "name";
+ static const std::string order = "order";
+}
+
+struct sort_field {
+ std::string name;
+ std::string order;
+
+ sort_field(const std::string & name, const std::string & order): name(name), order(order) {
+
+ }
+
+ sort_field& operator=(sort_field other) {
+ name = other.name;
+ order = other.order;
+ return *this;
+ }
+};
+
struct facet {
const std::string field_name;
std::map<std::string, size_t> result_map;
diff --git a/src/api.cpp b/src/api.cpp
index ed73b969..2e16606e 100644
--- a/src/api.cpp
+++ b/src/api.cpp
@@ -4,7 +4,13 @@
#include "collection_manager.h"
void post_create_collection(http_req & req, http_res & res) {
- nlohmann::json req_json = nlohmann::json::parse(req.body);
+ nlohmann::json req_json;
+
+ try {
+ req_json = nlohmann::json::parse(req.body);
+ } catch(...) {
+ return res.send_400("Bad JSON.");
+ }
CollectionManager & collectionManager = CollectionManager::get_instance();
@@ -18,8 +24,8 @@ void post_create_collection(http_req & req, http_res & res) {
return res.send_400("Parameter `search_fields` is required.");
}
- if(req_json.count("rank_fields") == 0) {
- return res.send_400("Parameter `rank_fields` is required.");
+ if(req_json.count("sort_fields") == 0) {
+ return res.send_400("Parameter `sort_fields` is required.");
}
if(collectionManager.get_collection(req_json["name"]) != nullptr) {
@@ -68,23 +74,27 @@ void post_create_collection(http_req & req, http_res & res) {
}
}
- std::vector<std::string> rank_fields;
+ std::vector<sort_field> sort_fields;
- if(!req_json["rank_fields"].is_array() || req_json["rank_fields"].size() == 0) {
- return res.send_400("Wrong format for `rank_fields`. It should be an array like: "
- "[\"<field_name_1>, <field_name_2>\"]");
+ if(!req_json["sort_fields"].is_array() || req_json["sort_fields"].size() == 0) {
+ return res.send_400("Wrong format for `sort_fields`. It should be an array like: "
+ "[{\"name\": \"<field_name>\", \"order\": \"<ASC|DESC>\"}]");
}
- for(const nlohmann::json & rank_field: req_json["rank_fields"]) {
- if(!rank_field.is_string()) {
- return res.send_400("Wrong format for `rank_fields`. It should be an array like: "
- "[\"<field_name_1>, <field_name_2>\"]");
+ for(const nlohmann::json & sort_field_json: req_json["sort_fields"]) {
+ if(!sort_field_json.is_object() ||
+ sort_field_json.count(sort_field_const::name) == 0 || sort_field_json.count(sort_field_const::order) == 0 ||
+ !sort_field_json.at(sort_field_const::name).is_string() ||
+ !sort_field_json.at(sort_field_const::order).is_string()) {
+
+ return res.send_400("Wrong format for `sort_fields`. It should be an array like: "
+ "[{\"name\": \"<field_name>\", \"order\": \"<ASC|DESC>\"}]");
}
- rank_fields.push_back(rank_field.get<std::string>());
+ sort_fields.push_back(sort_field(sort_field_json["name"], sort_field_json["order"]));
}
- collectionManager.create_collection(req_json["name"], search_fields, facet_fields, rank_fields);
+ collectionManager.create_collection(req_json["name"], search_fields, facet_fields, sort_fields);
res.send_201(req.body);
}
@@ -124,7 +134,8 @@ void get_search(http_req & req, http_res & res) {
}
nlohmann::json result = collection->search(req.params["q"], search_fields, filter_str, { },
- {"points"}, std::stoi(req.params[NUM_TYPOS]), 100, token_order, false);
+ {sort_field("points", "DESC")}, std::stoi(req.params[NUM_TYPOS]), 100,
+ token_order, false);
const std::string & json_str = result.dump();
//std::cout << "JSON:" << json_str << std::endl;
struct rusage r_usage;
diff --git a/src/collection.cpp b/src/collection.cpp
index 80e63976..ddce35a2 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -9,9 +9,9 @@
Collection::Collection(const std::string name, const uint32_t collection_id, const uint32_t next_seq_id, Store *store,
const std::vector<field> &search_fields, const std::vector<field> & facet_fields,
- const std::vector<std::string> & rank_fields, const std::string token_ordering_field):
+ const std::vector<sort_field> & sort_fields, const std::string token_ordering_field):
name(name), collection_id(collection_id), next_seq_id(next_seq_id), store(store),
- rank_fields(rank_fields), token_ordering_field(token_ordering_field) {
+ sort_fields(sort_fields), token_ordering_field(token_ordering_field) {
for(const field& field: search_fields) {
art_tree *t = new art_tree;
@@ -27,9 +27,9 @@ Collection::Collection(const std::string name, const uint32_t collection_id, con
facet_schema.emplace(field.name, field);
}
- for(const std::string & rank_field: rank_fields) {
+ for(const sort_field & _sort_field: sort_fields) {
spp::sparse_hash_map<uint32_t, int64_t> * doc_to_score = new spp::sparse_hash_map<uint32_t, int64_t>();
- rank_index.emplace(rank_field, doc_to_score);
+ sort_index.emplace(_sort_field.name, doc_to_score);
}
}
@@ -46,7 +46,7 @@ Collection::~Collection() {
t = nullptr;
}
- for(std::pair<std::string, spp::sparse_hash_map<uint32_t, int64_t>*> name_map: rank_index) {
+ for(std::pair<std::string, spp::sparse_hash_map<uint32_t, int64_t>*> name_map: sort_index) {
delete name_map.second;
}
}
@@ -197,18 +197,18 @@ Option<uint32_t> Collection::index_in_memory(const nlohmann::json &document, uin
}
}
- for(const std::string & rank_field: rank_fields) {
- if(document.count(rank_field) == 0) {
- return Option<>(400, "Field `" + rank_field + "` has been declared as a rank field in the schema, "
+ for(const sort_field & _sort_field: sort_fields) {
+ if(document.count(_sort_field.name) == 0) {
+ return Option<>(400, "Field `" + _sort_field.name + "` has been declared as a sort field in the schema, "
"but is not found in the document.");
}
- if(!document[rank_field].is_number()) {
- return Option<>(400, "Rank field `" + rank_field + "` must be an integer.");
+ if(!document[_sort_field.name].is_number()) {
+ return Option<>(400, "Sort field `" + _sort_field.name + "` must be an integer.");
}
- spp::sparse_hash_map<uint32_t, int64_t> *doc_to_score = rank_index.at(rank_field);
- doc_to_score->emplace(seq_id, document[rank_fields[0]].get<int64_t>());
+ spp::sparse_hash_map<uint32_t, int64_t> *doc_to_score = sort_index.at(_sort_field.name);
+ doc_to_score->emplace(seq_id, document[_sort_field.name].get<int64_t>());
}
return Option<>(200);
@@ -357,7 +357,7 @@ void Collection::do_facets(std::vector<facet> & facets, uint32_t* result_ids, si
}
void Collection::search_candidates(uint32_t* filter_ids, size_t filter_ids_length, std::vector<facet> & facets,
- const std::vector<std::string> & rank_fields, int & token_rank,
+ const std::vector<sort_field> & sort_fields, int & token_rank,
std::vector<std::vector<art_leaf*>> & token_leaves, Topster<100> & topster,
size_t & total_results, size_t & num_found, const size_t & max_results) {
const size_t combination_limit = 10;
@@ -392,7 +392,7 @@ void Collection::search_candidates(uint32_t* filter_ids, size_t filter_ids_lengt
do_facets(facets, filtered_result_ids, filtered_results_size);
// go through each matching document id and calculate match score
- score_results(rank_fields, token_rank, topster, query_suggestion, filtered_result_ids, filtered_results_size);
+ score_results(sort_fields, token_rank, topster, query_suggestion, filtered_result_ids, filtered_results_size);
num_found += filtered_results_size;
delete[] filtered_result_ids;
@@ -400,7 +400,7 @@ void Collection::search_candidates(uint32_t* filter_ids, size_t filter_ids_lengt
} else {
do_facets(facets, result_ids, result_size);
- score_results(rank_fields, token_rank, topster, query_suggestion, result_ids, result_size);
+ score_results(sort_fields, token_rank, topster, query_suggestion, result_ids, result_size);
num_found += result_size;
delete[] result_ids;
}
@@ -558,7 +558,7 @@ Option<uint32_t> Collection::do_filtering(uint32_t** filter_ids_out, const std::
nlohmann::json Collection::search(std::string query, const std::vector<std::string> search_fields,
const std::string & simple_filter_query, const std::vector<std::string> & facet_fields,
- const std::vector<std::string> & rank_fields, const int num_typos,
+ const std::vector<sort_field> & sort_fields, const int num_typos,
const size_t num_results, const token_ordering token_order, const bool prefix) {
size_t num_found = 0;
nlohmann::json result = nlohmann::json::object();
@@ -587,10 +587,10 @@ nlohmann::json Collection::search(std::string query, const std::vector<std::stri
facets.push_back(facet(field_name));
}
- // validate rank fields
- for(const std::string & field_name: rank_fields) {
- if(rank_index.count(field_name) == 0) {
- result["error"] = "Could not find a rank field named `" + field_name + "` in the schema.";
+ // validate sort fields
+ for(const sort_field & _sort_field: sort_fields) {
+ if(sort_index.count(_sort_field.name) == 0) {
+ result["error"] = "Could not find a sort field named `" + _sort_field.name + "` in the schema.";
return result;
}
}
@@ -605,7 +605,7 @@ nlohmann::json Collection::search(std::string query, const std::vector<std::stri
const uint32_t filter_ids_length = op_filter_ids_length.get();
- // Order of `fields` are used to rank results
+ // Order of `fields` are used to sort results
auto begin = std::chrono::high_resolution_clock::now();
std::vector<std::pair<int, Topster<100>::KV>> field_order_kvs;
@@ -614,7 +614,7 @@ nlohmann::json Collection::search(std::string query, const std::vector<std::stri
const std::string & field = search_fields[i];
// proceed to query search only when no filters are provided or when filtering produces results
if(simple_filter_query.size() == 0 || filter_ids_length > 0) {
- search_field(query, field, filter_ids, filter_ids_length, facets, rank_fields, num_typos, num_results,
+ search_field(query, field, filter_ids, filter_ids_length, facets, sort_fields, num_typos, num_results,
topster, num_found, token_order, prefix);
topster.sort();
}
@@ -681,7 +681,7 @@ nlohmann::json Collection::search(std::string query, const std::vector<std::stri
5. Sort the docs based on some ranking criteria
*/
void Collection::search_field(std::string & query, const std::string & field, uint32_t *filter_ids, size_t filter_ids_length,
- std::vector<facet> & facets, const std::vector<std::string> & rank_fields, const int num_typos,
+ std::vector<facet> & facets, const std::vector<sort_field> & sort_fields, const int num_typos,
const size_t num_results, Topster<100> &topster, size_t & num_found,
const token_ordering token_order, const bool prefix) {
std::vector<std::string> tokens;
@@ -784,7 +784,7 @@ void Collection::search_field(std::string & query, const std::string & field, ui
if(token_leaves.size() != 0 && token_leaves.size() == tokens.size()) {
// If all tokens were found, go ahead and search for candidates with what we have so far
- search_candidates(filter_ids, filter_ids_length, facets, rank_fields, token_rank, token_leaves, topster,
+ search_candidates(filter_ids, filter_ids_length, facets, sort_fields, token_rank, token_leaves, topster,
total_results, num_found, max_results);
if (total_results >= max_results) {
@@ -818,7 +818,7 @@ void Collection::search_field(std::string & query, const std::string & field, ui
}
}
- return search_field(truncated_query, field, filter_ids, filter_ids_length, facets, rank_fields, num_typos,
+ return search_field(truncated_query, field, filter_ids, filter_ids_length, facets, sort_fields, num_typos,
num_results, topster, num_found, token_order, prefix);
}
}
@@ -834,7 +834,7 @@ void Collection::log_leaves(const int cost, const std::string &token, const std:
}
}
-void Collection::score_results(const std::vector<std::string> & rank_fields, const int & token_rank,
+void Collection::score_results(const std::vector<sort_field> & sort_fields, const int & token_rank,
Topster<100> & topster, const std::vector<art_leaf *> &query_suggestion,
const uint32_t *result_ids, const size_t result_size) const {
@@ -853,13 +853,13 @@ void Collection::score_results(const std::vector<std::string> & rank_fields, con
spp::sparse_hash_map<uint32_t, int64_t> * primary_rank_scores = nullptr;
spp::sparse_hash_map<uint32_t, int64_t> * secondary_rank_scores = nullptr;
- if(rank_fields.size() > 0) {
+ if(sort_fields.size() > 0) {
// assumed that rank field exists in the index - checked earlier in the chain
- primary_rank_scores = rank_index.at(rank_fields[0]);
+ primary_rank_scores = sort_index.at(sort_fields[0].name);
}
- if(rank_fields.size() > 1) {
- secondary_rank_scores = rank_index.at(rank_fields[1]);
+ if(sort_fields.size() > 1) {
+ secondary_rank_scores = sort_index.at(sort_fields[1].name);
}
for(auto i=0; i<result_size; i++) {
@@ -1059,8 +1059,8 @@ std::vector<std::string> Collection::get_facet_fields() {
return facet_fields_copy;
}
-std::vector<std::string> Collection::get_rank_fields() {
- return rank_fields;
+std::vector<sort_field> Collection::get_sort_fields() {
+ return sort_fields;
}
spp::sparse_hash_map<std::string, field> Collection::get_schema() {
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 387dfa4b..36f593d5 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -45,8 +45,13 @@ void CollectionManager::init(Store *store) {
store->get(Collection::get_next_seq_id_key(this_collection_name), collection_next_seq_id_str);
uint32_t collection_next_seq_id = (const uint32_t) std::stoi(collection_next_seq_id_str);
- std::vector<std::string> collection_rank_fields =
- collection_meta[COLLECTION_RANK_FIELDS_KEY].get<std::vector<std::string>>();
+
+ std::vector<sort_field> collection_sort_fields;
+ nlohmann::json sort_fields_map = collection_meta[COLLECTION_SORT_FIELDS_KEY];
+
+ for (nlohmann::json::iterator it = sort_fields_map.begin(); it != sort_fields_map.end(); ++it) {
+ collection_sort_fields.push_back({it.value()[sort_field_const::name], it.value()[sort_field_const::order]});
+ }
std::string token_ordering_field = collection_meta[COLLECTION_TOKEN_ORDERING_FIELD_KEY].get<std::string>();
@@ -56,7 +61,7 @@ void CollectionManager::init(Store *store) {
store,
search_fields,
facet_fields,
- collection_rank_fields,
+ collection_sort_fields,
token_ordering_field);
// Fetch records from the store and re-create memory index
@@ -82,7 +87,7 @@ void CollectionManager::init(Store *store) {
Collection* CollectionManager::create_collection(std::string name, const std::vector<field> & search_fields,
const std::vector<field> & facet_fields,
- const std::vector<std::string> & rank_fields,
+ const std::vector<sort_field> & sort_fields,
const std::string & token_ordering_field) {
if(store->contains(Collection::get_meta_key(name))) {
return nullptr;
@@ -91,7 +96,7 @@ Collection* CollectionManager::create_collection(std::string name, const std::ve
nlohmann::json collection_meta;
nlohmann::json search_fields_json = nlohmann::json::array();;
- for(const field& search_field: search_fields) {
+ for(const field & search_field: search_fields) {
nlohmann::json field_val;
field_val[fields::name] = search_field.name;
field_val[fields::type] = search_field.type;
@@ -99,22 +104,30 @@ Collection* CollectionManager::create_collection(std::string name, const std::ve
}
nlohmann::json facet_fields_json = nlohmann::json::array();;
- for(const field& facet_field: facet_fields) {
+ for(const field & facet_field: facet_fields) {
nlohmann::json field_val;
field_val[fields::name] = facet_field.name;
field_val[fields::type] = facet_field.type;
facet_fields_json.push_back(field_val);
}
+ nlohmann::json sort_fields_json = nlohmann::json::array();;
+ for(const sort_field & _sort_field: sort_fields) {
+ nlohmann::json sort_field_val;
+ sort_field_val[sort_field_const::name] = _sort_field.name;
+ sort_field_val[sort_field_const::order] = _sort_field.order;
+ sort_fields_json.push_back(sort_field_val);
+ }
+
collection_meta[COLLECTION_NAME_KEY] = name;
collection_meta[COLLECTION_ID_KEY] = next_collection_id;
collection_meta[COLLECTION_SEARCH_FIELDS_KEY] = search_fields_json;
collection_meta[COLLECTION_FACET_FIELDS_KEY] = facet_fields_json;
- collection_meta[COLLECTION_RANK_FIELDS_KEY] = rank_fields;
+ collection_meta[COLLECTION_SORT_FIELDS_KEY] = sort_fields_json;
collection_meta[COLLECTION_TOKEN_ORDERING_FIELD_KEY] = token_ordering_field;
Collection* new_collection = new Collection(name, next_collection_id, 0, store, search_fields, facet_fields,
- rank_fields, token_ordering_field);
+ sort_fields, token_ordering_field);
store->insert(Collection::get_meta_key(name), collection_meta.dump());
store->insert(Collection::get_next_seq_id_key(name), std::to_string(0));
diff --git a/src/main/benchmark.cpp b/src/main/benchmark.cpp
index 22919a37..d5585e7b 100644
--- a/src/main/benchmark.cpp
+++ b/src/main/benchmark.cpp
@@ -17,14 +17,14 @@ int main(int argc, char* argv[]) {
system("rm -rf /tmp/typesense-data && mkdir -p /tmp/typesense-data");
std::vector<field> fields_to_index = {field("title", field_types::STRING)};
- std::vector<std::string> rank_fields = {"points"};
+ std::vector<sort_field> sort_fields = { sort_field("points", "DESC")};
Store *store = new Store("/tmp/typesense-data");
CollectionManager & collectionManager = CollectionManager::get_instance();
collectionManager.init(store);
Collection *collection = collectionManager.get_collection("collection");
if(collection == nullptr) {
- collection = collectionManager.create_collection("collection", fields_to_index, {}, rank_fields);
+ collection = collectionManager.create_collection("collection", fields_to_index, {}, sort_fields);
}
std::ifstream infile("/Users/kishore/Downloads/hnstories_small.jsonl");
diff --git a/src/main/main.cpp b/src/main/main.cpp
index 636e8e78..a7fc0117 100644
--- a/src/main/main.cpp
+++ b/src/main/main.cpp
@@ -43,7 +43,7 @@ int main(int argc, char* argv[]) {
const std::string state_dir_path = "/tmp/typesense-data";
std::vector<field> fields_to_index = {field("title", field_types::STRING)};
- std::vector<std::string> rank_fields = {"points"};
+ std::vector<sort_field> sort_fields = { sort_field("points", "DESC")};
Store *store = new Store("/tmp/typesense-data");
CollectionManager & collectionManager = CollectionManager::get_instance();
@@ -51,7 +51,7 @@ int main(int argc, char* argv[]) {
Collection *collection = collectionManager.get_collection("collection");
if(collection == nullptr) {
- collection = collectionManager.create_collection("collection", fields_to_index, {}, rank_fields);
+ collection = collectionManager.create_collection("collection", fields_to_index, {}, sort_fields);
std::ifstream infile(std::string(ROOT_DIR)+"test/documents.jsonl");
//std::ifstream infile(argv[1]);
diff --git a/src/main/server.cpp b/src/main/server.cpp
index 2909d503..62f4d3b0 100644
--- a/src/main/server.cpp
+++ b/src/main/server.cpp
@@ -30,7 +30,7 @@ static h2o_globalconf_t config;
static h2o_context_t ctx;
static h2o_accept_ctx_t accept_ctx;
std::vector<field> search_fields = {field("title", field_types::STRING), field("points", field_types::INT32)};
-std::vector<std::string> rank_fields = {"points"};
+std::vector<sort_field> sort_fields = { sort_field("points", "DESC")};
Collection *collection;
static h2o_pathconf_t *register_handler(h2o_hostconf_t *hostconf, const char *path,
@@ -236,7 +236,7 @@ int main(int argc, char **argv) {
collection = collectionManager.get_collection("collection");
if(collection == nullptr) {
- collection = collectionManager.create_collection("collection", search_fields, {}, rank_fields);
+ collection = collectionManager.create_collection("collection", search_fields, {}, sort_fields);
//index_documents(std::string(ROOT_DIR)+"test/documents.jsonl");
if(argc > 1) {
index_documents(argv[1]);
diff --git a/test/collection_manager_test.cpp b/test/collection_manager_test.cpp
index 6d301322..f17000ad 100644
--- a/test/collection_manager_test.cpp
+++ b/test/collection_manager_test.cpp
@@ -12,7 +12,7 @@ protected:
Collection *collection1;
std::vector<field> search_fields;
std::vector<field> facet_fields;
- std::vector<std::string> rank_fields;
+ std::vector<sort_field> sort_fields;
void setupCollection() {
std::string state_dir_path = "/tmp/typesense_test/coll_manager_test_db";
@@ -24,10 +24,10 @@ protected:
search_fields = {field("title", field_types::STRING), field("starring", field_types::STRING)};
facet_fields = {field("starring", field_types::STRING)};
- rank_fields = {"points"};
+ sort_fields = { sort_field("points", "DESC") };
collection1 = collectionManager.create_collection("collection1", search_fields, facet_fields,
- rank_fields, "points");
+ sort_fields, "points");
}
virtual void SetUp() {
@@ -53,7 +53,7 @@ TEST_F(CollectionManagerTest, RestoreRecordsOnRestart) {
std::vector<std::string> search_fields = {"starring", "title"};
std::vector<std::string> facets;
- nlohmann::json results = collection1->search("thomas", search_fields, "", facets, rank_fields, 0, 10, FREQUENCY, false);
+ nlohmann::json results = collection1->search("thomas", search_fields, "", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(4, results["hits"].size());
spp::sparse_hash_map<std::string, field> schema = collection1->get_schema();
@@ -70,11 +70,12 @@ TEST_F(CollectionManagerTest, RestoreRecordsOnRestart) {
ASSERT_EQ(0, collection1->get_collection_id());
ASSERT_EQ(18, collection1->get_next_seq_id());
ASSERT_EQ(facet_fields_expected, collection1->get_facet_fields());
- ASSERT_EQ(rank_fields, collection1->get_rank_fields());
+ ASSERT_EQ(1, collection1->get_sort_fields().size());
+ ASSERT_EQ(sort_fields[0].name, collection1->get_sort_fields()[0].name);
ASSERT_EQ(schema.size(), collection1->get_schema().size());
ASSERT_EQ("points", collection1->get_token_ordering_field());
- results = collection1->search("thomas", search_fields, "", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = collection1->search("thomas", search_fields, "", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(4, results["hits"].size());
}
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index 659fd9c3..539d5d76 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -11,7 +11,7 @@ protected:
std::vector<std::string> query_fields;
Store *store;
CollectionManager & collectionManager = CollectionManager::get_instance();
- std::vector<std::string> rank_fields;
+ std::vector<sort_field> sort_fields;
std::vector<field> facet_fields;
void setupCollection() {
@@ -27,12 +27,12 @@ protected:
query_fields = {"title"};
facet_fields = { };
- rank_fields = {"points"};
+ sort_fields = { sort_field("points", "DESC") };
collection = collectionManager.get_collection("collection");
if(collection == nullptr) {
collection = collectionManager.create_collection("collection", search_fields, facet_fields,
- rank_fields, "points");
+ sort_fields, "points");
}
std::string json_line;
@@ -60,7 +60,7 @@ protected:
TEST_F(CollectionTest, ExactSearchShouldBeStable) {
std::vector<std::string> facets;
- nlohmann::json results = collection->search("the", query_fields, "", facets, rank_fields, 0, 10);
+ nlohmann::json results = collection->search("the", query_fields, "", facets, sort_fields, 0, 10);
ASSERT_EQ(7, results["hits"].size());
ASSERT_EQ(7, results["found"].get<int>());
@@ -77,7 +77,7 @@ TEST_F(CollectionTest, ExactSearchShouldBeStable) {
TEST_F(CollectionTest, ExactPhraseSearch) {
std::vector<std::string> facets;
- nlohmann::json results = collection->search("rocket launch", query_fields, "", facets, rank_fields, 0, 10);
+ nlohmann::json results = collection->search("rocket launch", query_fields, "", facets, sort_fields, 0, 10);
ASSERT_EQ(5, results["hits"].size());
/*
@@ -99,7 +99,7 @@ TEST_F(CollectionTest, ExactPhraseSearch) {
}
// Check pagination
- results = collection->search("rocket launch", query_fields, "", facets, rank_fields, 0, 3);
+ results = collection->search("rocket launch", query_fields, "", facets, sort_fields, 0, 3);
ASSERT_EQ(3, results["hits"].size());
for(size_t i = 0; i < 3; i++) {
nlohmann::json result = results["hits"].at(i);
@@ -112,7 +112,7 @@ TEST_F(CollectionTest, ExactPhraseSearch) {
TEST_F(CollectionTest, SkipUnindexedTokensDuringPhraseSearch) {
// Tokens that are not found in the index should be skipped
std::vector<std::string> facets;
- nlohmann::json results = collection->search("DoesNotExist from", query_fields, "", facets, rank_fields, 0, 10);
+ nlohmann::json results = collection->search("DoesNotExist from", query_fields, "", facets, sort_fields, 0, 10);
ASSERT_EQ(2, results["hits"].size());
std::vector<std::string> ids = {"2", "17"};
@@ -125,7 +125,7 @@ TEST_F(CollectionTest, SkipUnindexedTokensDuringPhraseSearch) {
}
// with non-zero cost
- results = collection->search("DoesNotExist from", query_fields, "", facets, rank_fields, 1, 10);
+ results = collection->search("DoesNotExist from", query_fields, "", facets, sort_fields, 1, 10);
ASSERT_EQ(2, results["hits"].size());
for(size_t i = 0; i < results["hits"].size(); i++) {
@@ -136,7 +136,7 @@ TEST_F(CollectionTest, SkipUnindexedTokensDuringPhraseSearch) {
}
// with 2 indexed words
- results = collection->search("from DoesNotExist insTruments", query_fields, "", facets, rank_fields, 1, 10);
+ results = collection->search("from DoesNotExist insTruments", query_fields, "", facets, sort_fields, 1, 10);
ASSERT_EQ(2, results["hits"].size());
ids = {"2", "17"};
@@ -148,17 +148,17 @@ TEST_F(CollectionTest, SkipUnindexedTokensDuringPhraseSearch) {
}
results.clear();
- results = collection->search("DoesNotExist1 DoesNotExist2", query_fields, "", facets, rank_fields, 0, 10);
+ results = collection->search("DoesNotExist1 DoesNotExist2", query_fields, "", facets, sort_fields, 0, 10);
ASSERT_EQ(0, results["hits"].size());
results.clear();
- results = collection->search("DoesNotExist1 DoesNotExist2", query_fields, "", facets, rank_fields, 2, 10);
+ results = collection->search("DoesNotExist1 DoesNotExist2", query_fields, "", facets, sort_fields, 2, 10);
ASSERT_EQ(0, results["hits"].size());
}
TEST_F(CollectionTest, PartialPhraseSearch) {
std::vector<std::string> facets;
- nlohmann::json results = collection->search("rocket research", query_fields, "", facets, rank_fields, 0, 10);
+ nlohmann::json results = collection->search("rocket research", query_fields, "", facets, sort_fields, 0, 10);
ASSERT_EQ(4, results["hits"].size());
std::vector<std::string> ids = {"1", "8", "16", "17"};
@@ -173,7 +173,7 @@ TEST_F(CollectionTest, PartialPhraseSearch) {
TEST_F(CollectionTest, QueryWithTypo) {
std::vector<std::string> facets;
- nlohmann::json results = collection->search("kind biologcal", query_fields, "", facets, rank_fields, 2, 3);
+ nlohmann::json results = collection->search("kind biologcal", query_fields, "", facets, sort_fields, 2, 3);
ASSERT_EQ(3, results["hits"].size());
std::vector<std::string> ids = {"19", "20", "21"};
@@ -186,7 +186,7 @@ TEST_F(CollectionTest, QueryWithTypo) {
}
results.clear();
- results = collection->search("fer thx", query_fields, "", facets, rank_fields, 1, 3);
+ results = collection->search("fer thx", query_fields, "", facets, sort_fields, 1, 3);
ids = {"1", "10", "13"};
ASSERT_EQ(3, results["hits"].size());
@@ -201,7 +201,7 @@ TEST_F(CollectionTest, QueryWithTypo) {
TEST_F(CollectionTest, TypoTokenRankedByScoreAndFrequency) {
std::vector<std::string> facets;
- nlohmann::json results = collection->search("loox", query_fields, "", facets, rank_fields, 1, 2, MAX_SCORE, false);
+ nlohmann::json results = collection->search("loox", query_fields, "", facets, sort_fields, 1, 2, MAX_SCORE, false);
ASSERT_EQ(2, results["hits"].size());
std::vector<std::string> ids = {"22", "23"};
@@ -212,7 +212,7 @@ TEST_F(CollectionTest, TypoTokenRankedByScoreAndFrequency) {
ASSERT_STREQ(id.c_str(), result_id.c_str());
}
- results = collection->search("loox", query_fields, "", facets, rank_fields, 1, 3, FREQUENCY, false);
+ results = collection->search("loox", query_fields, "", facets, sort_fields, 1, 3, FREQUENCY, false);
ASSERT_EQ(3, results["hits"].size());
ids = {"3", "12", "24"};
@@ -224,19 +224,19 @@ TEST_F(CollectionTest, TypoTokenRankedByScoreAndFrequency) {
}
// Check pagination
- results = collection->search("loox", query_fields, "", facets, rank_fields, 1, 1, FREQUENCY, false);
+ results = collection->search("loox", query_fields, "", facets, sort_fields, 1, 1, FREQUENCY, false);
ASSERT_EQ(3, results["found"].get<int>());
ASSERT_EQ(1, results["hits"].size());
std::string solo_id = results["hits"].at(0)["id"];
ASSERT_STREQ("3", solo_id.c_str());
- results = collection->search("loox", query_fields, "", facets, rank_fields, 1, 2, FREQUENCY, false);
+ results = collection->search("loox", query_fields, "", facets, sort_fields, 1, 2, FREQUENCY, false);
ASSERT_EQ(3, results["found"].get<int>());
ASSERT_EQ(2, results["hits"].size());
// Check total ordering
- results = collection->search("loox", query_fields, "", facets, rank_fields, 1, 10, FREQUENCY, false);
+ results = collection->search("loox", query_fields, "", facets, sort_fields, 1, 10, FREQUENCY, false);
ASSERT_EQ(5, results["hits"].size());
ids = {"3", "12", "24", "22", "23"};
@@ -247,7 +247,7 @@ TEST_F(CollectionTest, TypoTokenRankedByScoreAndFrequency) {
ASSERT_STREQ(id.c_str(), result_id.c_str());
}
- results = collection->search("loox", query_fields, "", facets, rank_fields, 1, 10, MAX_SCORE, false);
+ results = collection->search("loox", query_fields, "", facets, sort_fields, 1, 10, MAX_SCORE, false);
ASSERT_EQ(5, results["hits"].size());
ids = {"22", "23", "3", "12", "24"};
@@ -262,7 +262,7 @@ TEST_F(CollectionTest, TypoTokenRankedByScoreAndFrequency) {
TEST_F(CollectionTest, TextContainingAnActualTypo) {
// A line contains "ISX" but not "what" - need to ensure that correction to "ISS what" happens
std::vector<std::string> facets;
- nlohmann::json results = collection->search("ISX what", query_fields, "", facets, rank_fields, 1, 4, FREQUENCY, false);
+ nlohmann::json results = collection->search("ISX what", query_fields, "", facets, sort_fields, 1, 4, FREQUENCY, false);
ASSERT_EQ(4, results["hits"].size());
std::vector<std::string> ids = {"19", "6", "21", "8"};
@@ -275,7 +275,7 @@ TEST_F(CollectionTest, TextContainingAnActualTypo) {
}
// Record containing exact token match should appear first
- results = collection->search("ISX", query_fields, "", facets, rank_fields, 1, 10, FREQUENCY, false);
+ results = collection->search("ISX", query_fields, "", facets, sort_fields, 1, 10, FREQUENCY, false);
ASSERT_EQ(8, results["hits"].size());
ids = {"20", "19", "6", "3", "21", "4", "10", "8"};
@@ -290,7 +290,7 @@ TEST_F(CollectionTest, TextContainingAnActualTypo) {
TEST_F(CollectionTest, PrefixSearching) {
std::vector<std::string> facets;
- nlohmann::json results = collection->search("ex", query_fields, "", facets, rank_fields, 0, 10, FREQUENCY, true);
+ nlohmann::json results = collection->search("ex", query_fields, "", facets, sort_fields, 0, 10, FREQUENCY, true);
ASSERT_EQ(2, results["hits"].size());
std::vector<std::string> ids = {"12", "6"};
@@ -301,7 +301,7 @@ TEST_F(CollectionTest, PrefixSearching) {
ASSERT_STREQ(id.c_str(), result_id.c_str());
}
- results = collection->search("ex", query_fields, "", facets, rank_fields, 0, 10, MAX_SCORE, true);
+ results = collection->search("ex", query_fields, "", facets, sort_fields, 0, 10, MAX_SCORE, true);
ASSERT_EQ(2, results["hits"].size());
ids = {"6", "12"};
@@ -319,11 +319,11 @@ TEST_F(CollectionTest, MultipleFields) {
std::ifstream infile(std::string(ROOT_DIR)+"test/multi_field_documents.jsonl");
std::vector<field> fields = {field("title", field_types::STRING), field("starring", field_types::STRING),
field("cast", field_types::STRING_ARRAY)};
- std::vector<std::string> rank_fields = {"points"};
+ std::vector<sort_field> sort_fields = { sort_field("points", "DESC") };
coll_mul_fields = collectionManager.get_collection("coll_mul_fields");
if(coll_mul_fields == nullptr) {
- coll_mul_fields = collectionManager.create_collection("coll_mul_fields", fields, facet_fields, rank_fields);
+ coll_mul_fields = collectionManager.create_collection("coll_mul_fields", fields, facet_fields, sort_fields);
}
std::string json_line;
@@ -336,7 +336,7 @@ TEST_F(CollectionTest, MultipleFields) {
query_fields = {"title", "starring"};
std::vector<std::string> facets;
- nlohmann::json results = coll_mul_fields->search("Will", query_fields, "", facets, rank_fields, 0, 10, FREQUENCY, false);
+ nlohmann::json results = coll_mul_fields->search("Will", query_fields, "", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(4, results["hits"].size());
std::vector<std::string> ids = {"3", "2", "1", "0"};
@@ -351,7 +351,7 @@ TEST_F(CollectionTest, MultipleFields) {
// when "starring" takes higher priority than "title"
query_fields = {"starring", "title"};
- results = coll_mul_fields->search("thomas", query_fields, "", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = coll_mul_fields->search("thomas", query_fields, "", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(4, results["hits"].size());
ids = {"15", "14", "12", "13"};
@@ -364,11 +364,11 @@ TEST_F(CollectionTest, MultipleFields) {
}
query_fields = {"starring", "title", "cast"};
- results = coll_mul_fields->search("ben affleck", query_fields, "", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = coll_mul_fields->search("ben affleck", query_fields, "", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(1, results["hits"].size());
query_fields = {"cast"};
- results = coll_mul_fields->search("chris", query_fields, "", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = coll_mul_fields->search("chris", query_fields, "", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(3, results["hits"].size());
ids = {"6", "1", "7"};
@@ -380,7 +380,7 @@ TEST_F(CollectionTest, MultipleFields) {
}
query_fields = {"cast"};
- results = coll_mul_fields->search("chris pine", query_fields, "", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = coll_mul_fields->search("chris pine", query_fields, "", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(3, results["hits"].size());
ids = {"7", "6", "1"};
@@ -399,11 +399,11 @@ TEST_F(CollectionTest, FilterOnNumericFields) {
std::vector<field> fields = {field("name", field_types::STRING), field("age", field_types::INT32),
field("years", field_types::INT32_ARRAY),
field("timestamps", field_types::INT64_ARRAY)};
- std::vector<std::string> rank_fields = {"age"};
+ std::vector<sort_field> sort_fields = { sort_field("age", "DESC") };
coll_array_fields = collectionManager.get_collection("coll_array_fields");
if(coll_array_fields == nullptr) {
- coll_array_fields = collectionManager.create_collection("coll_array_fields", fields, facet_fields, rank_fields);
+ coll_array_fields = collectionManager.create_collection("coll_array_fields", fields, facet_fields, sort_fields);
}
std::string json_line;
@@ -417,7 +417,7 @@ TEST_F(CollectionTest, FilterOnNumericFields) {
// Plain search with no filters - results should be sorted by rank fields
query_fields = {"name"};
std::vector<std::string> facets;
- nlohmann::json results = coll_array_fields->search("Jeremy", query_fields, "", facets, rank_fields, 0, 10, FREQUENCY, false);
+ nlohmann::json results = coll_array_fields->search("Jeremy", query_fields, "", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(5, results["hits"].size());
std::vector<std::string> ids = {"3", "1", "4", "0", "2"};
@@ -430,7 +430,7 @@ TEST_F(CollectionTest, FilterOnNumericFields) {
}
// Searching on an int32 field
- results = coll_array_fields->search("Jeremy", query_fields, "age:>24", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = coll_array_fields->search("Jeremy", query_fields, "age:>24", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(3, results["hits"].size());
ids = {"3", "1", "4"};
@@ -442,14 +442,14 @@ TEST_F(CollectionTest, FilterOnNumericFields) {
ASSERT_STREQ(id.c_str(), result_id.c_str());
}
- results = coll_array_fields->search("Jeremy", query_fields, "age:>=24", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = coll_array_fields->search("Jeremy", query_fields, "age:>=24", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(4, results["hits"].size());
- results = coll_array_fields->search("Jeremy", query_fields, "age:24", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = coll_array_fields->search("Jeremy", query_fields, "age:24", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(1, results["hits"].size());
// Searching a number against an int32 array field
- results = coll_array_fields->search("Jeremy", query_fields, "years:>2002", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = coll_array_fields->search("Jeremy", query_fields, "years:>2002", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(3, results["hits"].size());
ids = {"1", "0", "2"};
@@ -460,7 +460,7 @@ TEST_F(CollectionTest, FilterOnNumericFields) {
ASSERT_STREQ(id.c_str(), result_id.c_str());
}
- results = coll_array_fields->search("Jeremy", query_fields, "years:<1989", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = coll_array_fields->search("Jeremy", query_fields, "years:<1989", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(1, results["hits"].size());
ids = {"3"};
@@ -472,7 +472,7 @@ TEST_F(CollectionTest, FilterOnNumericFields) {
}
// multiple filters
- results = coll_array_fields->search("Jeremy", query_fields, "years:<2005 && years:>1987", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = coll_array_fields->search("Jeremy", query_fields, "years:<2005 && years:>1987", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(1, results["hits"].size());
ids = {"4"};
@@ -484,7 +484,7 @@ TEST_F(CollectionTest, FilterOnNumericFields) {
}
// multiple search values (works like SQL's IN operator) against a single int field
- results = coll_array_fields->search("Jeremy", query_fields, "age:[21, 24, 63]", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = coll_array_fields->search("Jeremy", query_fields, "age:[21, 24, 63]", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(3, results["hits"].size());
ids = {"3", "0", "2"};
@@ -496,7 +496,7 @@ TEST_F(CollectionTest, FilterOnNumericFields) {
}
// multiple search values against an int32 array field - also use extra padding between symbols
- results = coll_array_fields->search("Jeremy", query_fields, "years : [ 2015, 1985 , 1999]", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = coll_array_fields->search("Jeremy", query_fields, "years : [ 2015, 1985 , 1999]", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(4, results["hits"].size());
ids = {"3", "1", "4", "0"};
@@ -508,7 +508,7 @@ TEST_F(CollectionTest, FilterOnNumericFields) {
}
// searching on an int64 array field - also ensure that padded space causes no issues
- results = coll_array_fields->search("Jeremy", query_fields, "timestamps : > 475205222", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = coll_array_fields->search("Jeremy", query_fields, "timestamps : > 475205222", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(4, results["hits"].size());
ids = {"1", "4", "0", "2"};
@@ -521,7 +521,7 @@ TEST_F(CollectionTest, FilterOnNumericFields) {
}
// when filters don't match any record, no results should be returned
- results = coll_array_fields->search("Jeremy", query_fields, "timestamps:<1", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = coll_array_fields->search("Jeremy", query_fields, "timestamps:<1", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(0, results["hits"].size());
collectionManager.drop_collection("coll_array_fields");
@@ -534,11 +534,11 @@ TEST_F(CollectionTest, FilterOnTextFields) {
std::vector<field> fields = {field("name", field_types::STRING), field("age", field_types::INT32),
field("years", field_types::INT32_ARRAY),
field("tags", field_types::STRING_ARRAY)};
- std::vector<std::string> rank_fields = {"age"};
+ std::vector<sort_field> sort_fields = { sort_field("age", "DESC") };
coll_array_fields = collectionManager.get_collection("coll_array_fields");
if(coll_array_fields == nullptr) {
- coll_array_fields = collectionManager.create_collection("coll_array_fields", fields, facet_fields, rank_fields);
+ coll_array_fields = collectionManager.create_collection("coll_array_fields", fields, facet_fields, sort_fields);
}
std::string json_line;
@@ -551,7 +551,7 @@ TEST_F(CollectionTest, FilterOnTextFields) {
query_fields = {"name"};
std::vector<std::string> facets;
- nlohmann::json results = coll_array_fields->search("Jeremy", query_fields, "tags: gold", facets, rank_fields, 0, 10, FREQUENCY, false);
+ nlohmann::json results = coll_array_fields->search("Jeremy", query_fields, "tags: gold", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(4, results["hits"].size());
std::vector<std::string> ids = {"1", "4", "0", "2"};
@@ -563,7 +563,7 @@ TEST_F(CollectionTest, FilterOnTextFields) {
ASSERT_STREQ(id.c_str(), result_id.c_str());
}
- results = coll_array_fields->search("Jeremy", query_fields, "tags : bronze", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = coll_array_fields->search("Jeremy", query_fields, "tags : bronze", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(2, results["hits"].size());
ids = {"4", "2"};
@@ -576,7 +576,7 @@ TEST_F(CollectionTest, FilterOnTextFields) {
}
// search with a list of tags, also testing extra padding of space
- results = coll_array_fields->search("Jeremy", query_fields, "tags: [bronze, silver]", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = coll_array_fields->search("Jeremy", query_fields, "tags: [bronze, silver]", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(4, results["hits"].size());
ids = {"3", "4", "0", "2"};
@@ -589,7 +589,7 @@ TEST_F(CollectionTest, FilterOnTextFields) {
}
// should be exact matches (no normalization or fuzzy searching should happen)
- results = coll_array_fields->search("Jeremy", query_fields, "tags: BRONZE", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = coll_array_fields->search("Jeremy", query_fields, "tags: BRONZE", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(0, results["hits"].size());
collectionManager.drop_collection("coll_array_fields");
@@ -604,11 +604,11 @@ TEST_F(CollectionTest, HandleBadlyFormedFilterQuery) {
field("years", field_types::INT32_ARRAY),
field("timestamps", field_types::INT64_ARRAY),
field("tags", field_types::STRING_ARRAY)};
- std::vector<std::string> rank_fields = {"age"};
+ std::vector<sort_field> sort_fields = { sort_field("age", "DESC") };
coll_array_fields = collectionManager.get_collection("coll_array_fields");
if(coll_array_fields == nullptr) {
- coll_array_fields = collectionManager.create_collection("coll_array_fields", fields, facet_fields, rank_fields);
+ coll_array_fields = collectionManager.create_collection("coll_array_fields", fields, facet_fields, sort_fields);
}
std::string json_line;
@@ -623,27 +623,27 @@ TEST_F(CollectionTest, HandleBadlyFormedFilterQuery) {
std::vector<std::string> facets;
// when filter field does not exist in the schema
- nlohmann::json results = coll_array_fields->search("Jeremy", query_fields, "tagzz: gold", facets, rank_fields, 0, 10, FREQUENCY, false);
+ nlohmann::json results = coll_array_fields->search("Jeremy", query_fields, "tagzz: gold", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(0, results["hits"].size());
// searching using a string for a numeric field
- results = coll_array_fields->search("Jeremy", query_fields, "age: abcdef", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = coll_array_fields->search("Jeremy", query_fields, "age: abcdef", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(0, results["hits"].size());
// searching using a string for a numeric array field
- results = coll_array_fields->search("Jeremy", query_fields, "timestamps: abcdef", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = coll_array_fields->search("Jeremy", query_fields, "timestamps: abcdef", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(0, results["hits"].size());
// malformed k:v syntax
- results = coll_array_fields->search("Jeremy", query_fields, "timestamps abcdef", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = coll_array_fields->search("Jeremy", query_fields, "timestamps abcdef", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(0, results["hits"].size());
// just empty spaces
- results = coll_array_fields->search("Jeremy", query_fields, " ", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = coll_array_fields->search("Jeremy", query_fields, " ", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(0, results["hits"].size());
// wrapping number with quotes
- results = coll_array_fields->search("Jeremy", query_fields, "age: '21'", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = coll_array_fields->search("Jeremy", query_fields, "age: '21'", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(0, results["hits"].size());
collectionManager.drop_collection("coll_array_fields");
@@ -658,11 +658,11 @@ TEST_F(CollectionTest, FacetCounts) {
field("timestamps", field_types::INT64_ARRAY),
field("tags", field_types::STRING_ARRAY)};
facet_fields = {field("tags", field_types::STRING_ARRAY), field("name", field_types::STRING)};
- std::vector<std::string> rank_fields = {"age"};
+ std::vector<sort_field> sort_fields = { sort_field("age", "DESC") };
coll_array_fields = collectionManager.get_collection("coll_array_fields");
if(coll_array_fields == nullptr) {
- coll_array_fields = collectionManager.create_collection("coll_array_fields", fields, facet_fields, rank_fields);
+ coll_array_fields = collectionManager.create_collection("coll_array_fields", fields, facet_fields, sort_fields);
}
std::string json_line;
@@ -677,7 +677,7 @@ TEST_F(CollectionTest, FacetCounts) {
std::vector<std::string> facets = {"tags"};
// single facet with no filters
- nlohmann::json results = coll_array_fields->search("Jeremy", query_fields, "", facets, rank_fields, 0, 10, FREQUENCY, false);
+ nlohmann::json results = coll_array_fields->search("Jeremy", query_fields, "", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(5, results["hits"].size());
ASSERT_EQ(1, results["facet_counts"].size());
@@ -697,7 +697,7 @@ TEST_F(CollectionTest, FacetCounts) {
facets.clear();
facets.push_back("tags");
facets.push_back("name");
- results = coll_array_fields->search("Jeremy", query_fields, "", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = coll_array_fields->search("Jeremy", query_fields, "", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(5, results["hits"].size());
ASSERT_EQ(2, results["facet_counts"].size());
@@ -712,7 +712,7 @@ TEST_F(CollectionTest, FacetCounts) {
// facet with filters
facets.clear();
facets.push_back("tags");
- results = coll_array_fields->search("Jeremy", query_fields, "age: >24", facets, rank_fields, 0, 10, FREQUENCY, false);
+ results = coll_array_fields->search("Jeremy", query_fields, "age: >24", facets, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(3, results["hits"].size());
ASSERT_EQ(1, results["facet_counts"].size());
@@ -739,11 +739,11 @@ TEST_F(CollectionTest, SearchingWithMissingFields) {
field("timestamps", field_types::INT64_ARRAY),
field("tags", field_types::STRING_ARRAY)};
facet_fields = {field("tags", field_types::STRING_ARRAY), field("name", field_types::STRING)};
- std::vector<std::string> rank_fields = {"age"};
+ std::vector<sort_field> sort_fields = { sort_field("age", "DESC") };
coll_array_fields = collectionManager.get_collection("coll_array_fields");
if(coll_array_fields == nullptr) {
- coll_array_fields = collectionManager.create_collection("coll_array_fields", fields, facet_fields, rank_fields);
+ coll_array_fields = collectionManager.create_collection("coll_array_fields", fields, facet_fields, sort_fields);
}
std::string json_line;
@@ -758,28 +758,28 @@ TEST_F(CollectionTest, SearchingWithMissingFields) {
std::vector<std::string> facets;
std::vector<std::string> query_fields_not_found = {"titlez"};
- nlohmann::json res = coll_array_fields->search("the", query_fields_not_found, "", facets, rank_fields, 0, 10);
+ nlohmann::json res = coll_array_fields->search("the", query_fields_not_found, "", facets, sort_fields, 0, 10);
ASSERT_EQ(0, res["hits"].size());
ASSERT_STREQ("Could not find a search field named `titlez` in the schema.",res["error"].get<std::string>().c_str());
// when a query field is an integer field
- res = coll_array_fields->search("the", {"age"}, "", facets, rank_fields, 0, 10);
+ res = coll_array_fields->search("the", {"age"}, "", facets, sort_fields, 0, 10);
ASSERT_EQ(0, res["hits"].size());
ASSERT_STREQ("Search field `age` should be a string or a string array.", res["error"].get<std::string>().c_str());
// when a facet field is not defined in the schema
- res = coll_array_fields->search("the", {"name"}, "", {"timestamps"}, rank_fields, 0, 10);
+ res = coll_array_fields->search("the", {"name"}, "", {"timestamps"}, sort_fields, 0, 10);
ASSERT_EQ(0, res["hits"].size());
ASSERT_STREQ("Could not find a facet field named `timestamps` in the schema.", res["error"].get<std::string>().c_str());
// when a rank field is not defined in the schema
- res = coll_array_fields->search("the", {"name"}, "", {}, {"timestamps"}, 0, 10);
+ res = coll_array_fields->search("the", {"name"}, "", {}, { sort_field("timestamps", "ASC") }, 0, 10);
ASSERT_EQ(0, res["hits"].size());
- ASSERT_STREQ("Could not find a rank field named `timestamps` in the schema.", res["error"].get<std::string>().c_str());
+ ASSERT_STREQ("Could not find a sort field named `timestamps` in the schema.", res["error"].get<std::string>().c_str());
- res = coll_array_fields->search("the", {"name"}, "", {}, {"_rank"}, 0, 10);
+ res = coll_array_fields->search("the", {"name"}, "", {}, { sort_field("_rank", "ASC") }, 0, 10);
ASSERT_EQ(0, res["hits"].size());
- ASSERT_STREQ("Could not find a rank field named `_rank` in the schema.", res["error"].get<std::string>().c_str());
+ ASSERT_STREQ("Could not find a sort field named `_rank` in the schema.", res["error"].get<std::string>().c_str());
collectionManager.drop_collection("coll_array_fields");
}
@@ -790,12 +790,12 @@ TEST_F(CollectionTest, IndexingWithBadData) {
std::vector<field> fields = {field("name", field_types::STRING)};
facet_fields = {field("tags", field_types::STRING_ARRAY)};
- std::vector<std::string> rank_fields = {"age", "average"};
+ std::vector<sort_field> sort_fields = { sort_field("age", "DESC"), sort_field("average", "DESC") };
sample_collection = collectionManager.get_collection("sample_collection");
if(sample_collection == nullptr) {
sample_collection = collectionManager.create_collection("sample_collection", fields, facet_fields,
- rank_fields, "age");
+ sort_fields, "age");
}
const Option<std::string> & search_fields_missing_op1 = sample_collection->add("{\"namezz\": \"foo\", \"age\": 29}");
@@ -814,10 +814,10 @@ TEST_F(CollectionTest, IndexingWithBadData) {
facet_fields_missing_op1.error().c_str());
const char *doc_str = "{\"name\": \"foo\", \"age\": 34, \"tags\": [\"red\", \"blue\"]}";
- const Option<std::string> & rank_fields_missing_op1 = sample_collection->add(doc_str);
- ASSERT_FALSE(rank_fields_missing_op1.ok());
- ASSERT_STREQ("Field `average` has been declared as a rank field in the schema, but is not found in the document.",
- rank_fields_missing_op1.error().c_str());
+ const Option<std::string> & sort_fields_missing_op1 = sample_collection->add(doc_str);
+ ASSERT_FALSE(sort_fields_missing_op1.ok());
+ ASSERT_STREQ("Field `average` has been declared as a sort field in the schema, but is not found in the document.",
+ sort_fields_missing_op1.error().c_str());
// Handle type errors
@@ -844,7 +844,7 @@ TEST_F(CollectionTest, IndexingWithBadData) {
doc_str = "{\"name\": \"foo\", \"age\": 34, \"tags\": [], \"average\": \"34\"}";
const Option<std::string> & bad_rank_field_op = sample_collection->add(doc_str);
ASSERT_FALSE(bad_rank_field_op.ok());
- ASSERT_STREQ("Rank field `average` must be an integer.", bad_rank_field_op.error().c_str());
+ ASSERT_STREQ("Sort field `average` must be an integer.", bad_rank_field_op.error().c_str());
collectionManager.drop_collection("sample_collection");
}
@@ -854,13 +854,13 @@ TEST_F(CollectionTest, EmptyIndexShouldNotCrash) {
std::vector<field> fields = {field("name", field_types::STRING)};
facet_fields = {field("tags", field_types::STRING_ARRAY)};
- std::vector<std::string> rank_fields = {"age", "average"};
+ std::vector<sort_field> sort_fields = { sort_field("age", "DESC"), sort_field("average", "DESC") };
empty_coll = collectionManager.get_collection("empty_coll");
if(empty_coll == nullptr) {
- empty_coll = collectionManager.create_collection("empty_coll", fields, facet_fields, rank_fields, "age");
+ empty_coll = collectionManager.create_collection("empty_coll", fields, facet_fields, sort_fields, "age");
}
- nlohmann::json results = empty_coll->search("a", {"name"}, "", {}, rank_fields, 0, 10, FREQUENCY, false);
+ nlohmann::json results = empty_coll->search("a", {"name"}, "", {}, sort_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(0, results["hits"].size());
}
\ No newline at end of file