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

Support operators in multi-valued numerical filter.

kishorenc8c51e3f9201a
diff --git a/include/field.h b/include/field.h
index 279d9e1f..ea11ed4c 100644
--- a/include/field.h
+++ b/include/field.h
@@ -100,31 +100,45 @@ struct field {
struct filter {
std::string field_name;
std::vector<std::string> values;
- NUM_COMPARATOR compare_operator;
+ std::vector<NUM_COMPARATOR> comparators;
- static Option<NUM_COMPARATOR> extract_num_comparator(const std::string & comp_and_value) {
- if(StringUtils::is_integer(comp_and_value)) {
- return Option<NUM_COMPARATOR>(EQUALS);
+ static Option<NUM_COMPARATOR> extract_num_comparator(std::string & comp_and_value) {
+ auto num_comparator = EQUALS;
+
+ if(StringUtils::is_integer(comp_and_value) || StringUtils::is_float(comp_and_value)) {
+ num_comparator = EQUALS;
}
// the ordering is important - we have to compare 2-letter operators first
- if(comp_and_value.compare(0, 2, "<=") == 0) {
- return Option<NUM_COMPARATOR>(LESS_THAN_EQUALS);
+ else if(comp_and_value.compare(0, 2, "<=") == 0) {
+ num_comparator = LESS_THAN_EQUALS;
+ }
+
+ else if(comp_and_value.compare(0, 2, ">=") == 0) {
+ num_comparator = GREATER_THAN_EQUALS;
}
- if(comp_and_value.compare(0, 2, ">=") == 0) {
- return Option<NUM_COMPARATOR>(GREATER_THAN_EQUALS);
+ else if(comp_and_value.compare(0, 1, "<") == 0) {
+ num_comparator = LESS_THAN;
}
- if(comp_and_value.compare(0, 1, "<") == 0) {
- return Option<NUM_COMPARATOR>(LESS_THAN);
+ else if(comp_and_value.compare(0, 1, ">") == 0) {
+ num_comparator = GREATER_THAN;
}
- if(comp_and_value.compare(0, 1, ">") == 0) {
- return Option<NUM_COMPARATOR>(GREATER_THAN);
+ else {
+ return Option<NUM_COMPARATOR>(400, "Numerical field has an invalid comparator.");
}
- return Option<NUM_COMPARATOR>(400, "Numerical field has an invalid comparator.");
+ if(num_comparator == LESS_THAN || num_comparator == GREATER_THAN) {
+ comp_and_value = comp_and_value.substr(1);
+ } else if(num_comparator == LESS_THAN_EQUALS || num_comparator == GREATER_THAN_EQUALS) {
+ comp_and_value = comp_and_value.substr(2);
+ }
+
+ comp_and_value = StringUtils::trim(comp_and_value);
+
+ return Option<NUM_COMPARATOR>(num_comparator);
}
};
diff --git a/src/collection.cpp b/src/collection.cpp
index e618386b..a7836dea 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -247,7 +247,7 @@ nlohmann::json Collection::add_many(std::vector<std::string>& json_lines, nlohma
const size_t index_batch_size = 1000;
size_t num_indexed = 0;
- bool exceeds_memory_limit = false;
+ //bool exceeds_memory_limit = false;
for(size_t i=0; i < json_lines.size(); i++) {
const std::string & json_line = json_lines[i];
@@ -1706,7 +1706,14 @@ Option<bool> Collection::parse_filter_query(const std::string& simple_filter_que
std::vector<std::string> filter_values;
StringUtils::split(raw_value.substr(1, raw_value.size() - 2), filter_values, ",");
- for(const std::string & filter_value: filter_values) {
+ f = {field_name, {}, {}};
+
+ for(std::string & filter_value: filter_values) {
+ Option<NUM_COMPARATOR> op_comparator = filter::extract_num_comparator(filter_value);
+ if(!op_comparator.ok()) {
+ return Option<bool>(400, "Error with filter field `" + _field.name + "`: " + op_comparator.error());
+ }
+
if(_field.is_int32()) {
if(!StringUtils::is_int32_t(filter_value)) {
return Option<bool>(400, "Error with filter field `" + _field.name + "`: Not an int32.");
@@ -1724,47 +1731,36 @@ Option<bool> Collection::parse_filter_query(const std::string& simple_filter_que
return Option<bool>(400, "Error with filter field `" + _field.name + "`: Not a float.");
}
}
+
+ f.values.push_back(filter_value);
+ f.comparators.push_back(op_comparator.get());
}
- f = {field_name, filter_values, EQUALS};
} else {
Option<NUM_COMPARATOR> op_comparator = filter::extract_num_comparator(raw_value);
if(!op_comparator.ok()) {
return Option<bool>(400, "Error with filter field `" + _field.name + "`: " + op_comparator.error());
}
- // extract numerical value
- std::string filter_value;
- if(op_comparator.get() == LESS_THAN || op_comparator.get() == GREATER_THAN) {
- filter_value = raw_value.substr(1);
- } else if(op_comparator.get() == LESS_THAN_EQUALS || op_comparator.get() == GREATER_THAN_EQUALS) {
- filter_value = raw_value.substr(2);
- } else {
- // EQUALS
- filter_value = raw_value;
- }
-
- filter_value = StringUtils::trim(filter_value);
-
if(_field.is_int32()) {
- if(!StringUtils::is_int32_t(filter_value)) {
+ if(!StringUtils::is_int32_t(raw_value)) {
return Option<bool>(400, "Error with filter field `" + _field.name + "`: Not an int32.");
}
}
else if(_field.is_int64()) {
- if(!StringUtils::is_int64_t(filter_value)) {
+ if(!StringUtils::is_int64_t(raw_value)) {
return Option<bool>(400, "Error with filter field `" + _field.name + "`: Not an int64.");
}
}
else if(_field.is_float()) {
- if(!StringUtils::is_float(filter_value)) {
+ if(!StringUtils::is_float(raw_value)) {
return Option<bool>(400, "Error with filter field `" + _field.name + "`: Not a float.");
}
}
- f = {field_name, {filter_value}, op_comparator.get()};
+ f = {field_name, {raw_value}, {op_comparator.get()}};
}
} else if(_field.is_bool()) {
if(raw_value[0] == '[' && raw_value[raw_value.size() - 1] == ']') {
@@ -1780,13 +1776,13 @@ Option<bool> Collection::parse_filter_query(const std::string& simple_filter_que
filter_value = (filter_value == "true") ? "1" : "0";
}
- f = {field_name, filter_values, EQUALS};
+ f = {field_name, filter_values, {EQUALS}};
} else {
if(raw_value != "true" && raw_value != "false") {
return Option<bool>(400, "Value of filter field `" + _field.name + "` must be `true` or `false`.");
}
std::string bool_value = (raw_value == "true") ? "1" : "0";
- f = {field_name, {bool_value}, EQUALS};
+ f = {field_name, {bool_value}, {EQUALS}};
}
} else if(_field.is_string()) {
@@ -1808,9 +1804,9 @@ Option<bool> Collection::parse_filter_query(const std::string& simple_filter_que
if(raw_value[filter_value_index] == '[' && raw_value[raw_value.size() - 1] == ']') {
std::vector<std::string> filter_values;
StringUtils::split(raw_value.substr(filter_value_index+1, raw_value.size() - filter_value_index - 2), filter_values, ",");
- f = {field_name, filter_values, str_comparator};
+ f = {field_name, filter_values, {str_comparator}};
} else {
- f = {field_name, {raw_value.substr(filter_value_index)}, str_comparator};
+ f = {field_name, {raw_value.substr(filter_value_index)}, {str_comparator}};
}
} else {
return Option<bool>(400, "Error with filter field `" + _field.name +
@@ -1831,7 +1827,7 @@ Option<bool> Collection::parse_pinned_hits(const std::string& pinned_hits_str,
for(const std::string & pinned_hits_part: pinned_hits_strs) {
std::vector<std::string> expression_parts;
- size_t index = pinned_hits_part.size() - 1;
+ int64_t index = pinned_hits_part.size() - 1;
while(index >= 0 && pinned_hits_part[index] != ':') {
index--;
}
diff --git a/src/index.cpp b/src/index.cpp
index 989e9c1c..b28656a2 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -948,11 +948,11 @@ void Index::search_candidates(const uint8_t & field_id, uint32_t* filter_ids, si
result_ids = excluded_result_ids;
}
- LOG(INFO) << "n: " << n;
+ /*LOG(INFO) << "n: " << n;
for(size_t i=0; i < query_suggestion.size(); i++) {
LOG(INFO) << "i: " << i << " - " << query_suggestion[i]->key << ", ids: "
<< query_suggestion[i]->values->ids.getLength();
- }
+ }*/
if(filter_ids != nullptr) {
// intersect once again with filter ids
@@ -1016,14 +1016,18 @@ Option<uint32_t> Index::do_filtering(uint32_t** filter_ids_out, const std::vecto
std::vector<const art_leaf*> leaves;
std::vector<uint32_t> ids;
+ size_t value_index = 0;
for(const std::string & filter_value: a_filter.values) {
if(f.type == field_types::INT32 || f.type == field_types::INT32_ARRAY) {
+ // check for comparator again
int32_t value = (int32_t) std::stoi(filter_value);
- art_int32_search(t, value, a_filter.compare_operator, leaves);
+ art_int32_search(t, value, a_filter.comparators[value_index], leaves);
} else { // int64
int64_t value = (int64_t) std::stol(filter_value);
- art_int64_search(t, value, a_filter.compare_operator, leaves);
+ art_int64_search(t, value, a_filter.comparators[value_index], leaves);
}
+
+ value_index++;
}
result_ids = collate_leaf_ids(leaves, result_ids_len);
@@ -1032,9 +1036,11 @@ Option<uint32_t> Index::do_filtering(uint32_t** filter_ids_out, const std::vecto
std::vector<const art_leaf*> leaves;
std::vector<uint32_t> ids;
+ size_t value_index = 0;
for(const std::string & filter_value: a_filter.values) {
float value = (float) std::atof(filter_value.c_str());
- art_float_search(t, value, a_filter.compare_operator, leaves);
+ art_float_search(t, value, a_filter.comparators[value_index], leaves);
+ value_index++;
}
result_ids = collate_leaf_ids(leaves, result_ids_len);
@@ -1094,7 +1100,7 @@ Option<uint32_t> Index::do_filtering(uint32_t** filter_ids_out, const std::vecto
}
}
- if(a_filter.compare_operator == EQUALS && f.is_facet()) {
+ if(a_filter.comparators[0] == EQUALS && f.is_facet()) {
// need to do exact match (unlike CONTAINS) by using the facet index
// field being a facet is already enforced upstream
uint32_t* exact_strt_ids = new uint32_t[strt_ids_size];
@@ -1541,11 +1547,11 @@ void Index::search_field(const uint8_t & field_id,
while(token_index < search_tokens.size()) {
// For each token, look up the generated cost for this iteration and search using that cost
- std::string token = search_tokens[token_index];
+ const std::string& token = search_tokens[token_index];
const std::string token_cost_hash = token + std::to_string(costs[token_index]);
std::vector<art_leaf*> leaves;
- LOG(INFO) << "\nSearching for field: " << field << ", token:" << token << " - cost: " << costs[token_index];
+ //LOG(INFO) << "\nSearching for field: " << field << ", token:" << token << " - cost: " << costs[token_index];
if(token_cost_cache.count(token_cost_hash) != 0) {
leaves = token_cost_cache[token_cost_hash];
diff --git a/src/typesense_server_utils.cpp b/src/typesense_server_utils.cpp
index 5c9ffe29..0aec1eeb 100644
--- a/src/typesense_server_utils.cpp
+++ b/src/typesense_server_utils.cpp
@@ -184,11 +184,9 @@ Option<std::string> fetch_nodes_config(const std::string& path_to_nodes) {
}
bool is_private_ip(uint32_t ip) {
- uint8_t b1, b2, b3, b4;
+ uint8_t b1, b2;
b1 = (uint8_t) (ip >> 24);
b2 = (uint8_t) ((ip >> 16) & 0x0ff);
- b3 = (uint8_t) ((ip >> 8) & 0x0ff);
- b4 = (uint8_t) (ip & 0x0ff);
// 10.x.y.z
if (b1 == 10) {
diff --git a/test/collection_filtering_test.cpp b/test/collection_filtering_test.cpp
index 3bec1bf3..61362579 100644
--- a/test/collection_filtering_test.cpp
+++ b/test/collection_filtering_test.cpp
@@ -285,7 +285,7 @@ TEST_F(CollectionFilteringTest, FilterAndQueryFieldRestrictions) {
result_op = coll_mul_fields->search("captain", query_fields, "points: 100.34", facets, sort_fields, 0, 10, 1,
FREQUENCY, false);
ASSERT_EQ(false, result_op.ok());
- ASSERT_STREQ("Error with filter field `points`: Numerical field has an invalid comparator.", result_op.error().c_str());
+ ASSERT_STREQ("Error with filter field `points`: Not an int32.", result_op.error().c_str());
// bad filter value type - less than float on an integer field
result_op = coll_mul_fields->search("captain", query_fields, "points: <100.0", facets, sort_fields, 0, 10, 1,
@@ -614,3 +614,65 @@ TEST_F(CollectionFilteringTest, FilterOnFloatFields) {
collectionManager.drop_collection("coll_array_fields");
}
+
+TEST_F(CollectionFilteringTest, ComparatorsOnMultiValuedNumericalField) {
+ Collection *coll_array_fields;
+
+ std::ifstream infile(std::string(ROOT_DIR) + "test/numeric_array_documents.jsonl");
+ std::vector<field> fields = {
+ field("name", field_types::STRING, false),
+ field("age", field_types::INT32, false),
+ field("top_3", field_types::FLOAT_ARRAY, false),
+ field("rating", field_types::FLOAT, false)
+ };
+
+ std::vector<sort_by> sort_fields_desc = {sort_by("rating", "DESC")};
+
+ coll_array_fields = collectionManager.get_collection("coll_array_fields");
+ if (coll_array_fields == nullptr) {
+ coll_array_fields = collectionManager.create_collection("coll_array_fields", 4, fields, "age").get();
+ }
+
+ std::string json_line;
+
+ while (std::getline(infile, json_line)) {
+ auto add_op = coll_array_fields->add(json_line);
+ ASSERT_TRUE(add_op.ok());
+ }
+
+ infile.close();
+
+ query_fields = {"name"};
+ std::vector<std::string> facets;
+ nlohmann::json results = coll_array_fields->search("Jeremy", query_fields, "age: [24, >32]",
+ facets, sort_fields_desc, 0, 10, 1,FREQUENCY, false).get();
+
+ ASSERT_EQ(3, results["hits"].size());
+
+ std::vector<std::string> ids = {"1", "0", "3"};
+
+ 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());
+ }
+
+ // with <= and >=
+
+ results = coll_array_fields->search("Jeremy", query_fields, "age: [<=24, >=44]",
+ facets, sort_fields_desc, 0, 10, 1,FREQUENCY, false).get();
+
+ ASSERT_EQ(4, results["hits"].size());
+
+ ids = {"1", "2", "0", "3"};
+
+ 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());
+ }
+
+ collectionManager.drop_collection("coll_array_fields");
+}
\ No newline at end of file