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 foundational support for document update.

kishorenc377769294f82
diff --git a/include/collection.h b/include/collection.h
index f459c2a5..5faad469 100644
--- a/include/collection.h
+++ b/include/collection.h
@@ -92,6 +92,11 @@ struct override_t {
}
};
+struct doc_seq_id_t {
+ uint32_t seq_id;
+ bool is_new;
+};
+
class Collection {
private:
@@ -217,11 +222,11 @@ public:
std::string get_default_sorting_field();
- Option<uint32_t> to_doc(const std::string & json_str, nlohmann::json & document);
+ Option<doc_seq_id_t> to_doc(const std::string & json_str, nlohmann::json & document, bool upsert);
nlohmann::json get_summary_json();
- Option<nlohmann::json> add(const std::string & json_str);
+ Option<nlohmann::json> add(const std::string & json_str, const bool upsert=false);
nlohmann::json add_many(std::vector<std::string>& json_lines);
@@ -263,7 +268,7 @@ public:
Option<bool> get_document_from_store(const std::string & seq_id_key, nlohmann::json & document);
- Option<uint32_t> index_in_memory(const nlohmann::json & document, uint32_t seq_id);
+ Option<uint32_t> index_in_memory(const nlohmann::json & document, uint32_t seq_id, bool is_update);
size_t par_index_in_memory(std::vector<std::vector<index_record>> & iter_batch, std::vector<size_t>& indexed_counts);
diff --git a/include/index.h b/include/index.h
index 7ded995c..75f8c5dd 100644
--- a/include/index.h
+++ b/include/index.h
@@ -248,12 +248,13 @@ public:
static int64_t get_points_from_doc(const nlohmann::json &document, const std::string & default_sorting_field);
Option<uint32_t> index_in_memory(const nlohmann::json & document, uint32_t seq_id,
- const std::string & default_sorting_field);
+ const std::string & default_sorting_field, bool is_update);
static Option<uint32_t> validate_index_in_memory(const nlohmann::json &document, uint32_t seq_id,
const std::string & default_sorting_field,
const std::unordered_map<std::string, field> & search_schema,
- const std::map<std::string, field> & facet_schema);
+ const std::map<std::string, field> & facet_schema,
+ bool is_update);
static size_t batch_memory_index(Index *index,
std::vector<index_record> & iter_batch,
diff --git a/src/collection.cpp b/src/collection.cpp
index 680e9ba8..99d77ff2 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -98,34 +98,55 @@ void Collection::increment_next_seq_id_field() {
next_seq_id++;
}
-Option<uint32_t> Collection::to_doc(const std::string & json_str, nlohmann::json & document) {
+Option<doc_seq_id_t> Collection::to_doc(const std::string & json_str, nlohmann::json & document, bool upsert) {
try {
document = nlohmann::json::parse(json_str);
} catch(const std::exception& e) {
LOG(ERROR) << "JSON error: " << e.what();
- return Option<uint32_t>(400, std::string("Bad JSON: ") + e.what());
+ return Option<doc_seq_id_t>(400, std::string("Bad JSON: ") + e.what());
}
if(!document.is_object()) {
- return Option<uint32_t>(400, "Bad JSON: not a properly formed document.");
+ return Option<doc_seq_id_t>(400, "Bad JSON: not a properly formed document.");
}
if(document.count("id") == 0) {
+ if(upsert) {
+ return Option<doc_seq_id_t>(400, "For update, the `id` key must be present.");
+ }
uint32_t seq_id = get_next_seq_id();
document["id"] = std::to_string(seq_id);
- return Option<uint32_t>(seq_id);
+ return Option<doc_seq_id_t>(doc_seq_id_t{seq_id, true});
} else {
if(!document["id"].is_string()) {
- return Option<uint32_t>(400, "Document's `id` field should be a string.");
+ return Option<doc_seq_id_t>(400, "Document's `id` field should be a string.");
}
const std::string& doc_id = document["id"];
+
+ if(upsert) {
+ // try to get the corresponding sequence id from disk if present
+ std::string seq_id_str;
+ StoreStatus seq_id_status = store->get(get_doc_id_key(doc_id), seq_id_str);
+
+ if(seq_id_status == StoreStatus::ERROR) {
+ return Option<doc_seq_id_t>(500, "Error fetching the sequence key for document with id: " + doc_id);
+ }
+
+ if(seq_id_status == StoreStatus::FOUND) {
+ uint32_t seq_id = (uint32_t) std::stoul(seq_id_str);
+ return Option<doc_seq_id_t>(doc_seq_id_t{seq_id, false});
+ }
+
+ // if key is not found, we will fallback to the "insert" workflow
+ }
+
if(doc_exists(doc_id)) {
- return Option<uint32_t>(409, std::string("A document with id ") + doc_id + " already exists.");
+ return Option<doc_seq_id_t>(409, std::string("A document with id ") + doc_id + " already exists.");
}
uint32_t seq_id = get_next_seq_id();
- return Option<uint32_t>(seq_id);
+ return Option<doc_seq_id_t>(doc_seq_id_t{seq_id, true});
}
}
@@ -153,9 +174,9 @@ nlohmann::json Collection::get_summary_json() {
return json_response;
}
-Option<nlohmann::json> Collection::add(const std::string & json_str) {
+Option<nlohmann::json> Collection::add(const std::string & json_str, const bool upsert) {
nlohmann::json document;
- Option<uint32_t> doc_seq_id_op = to_doc(json_str, document);
+ Option<doc_seq_id_t> doc_seq_id_op = to_doc(json_str, document, upsert);
if(!doc_seq_id_op.ok()) {
return Option<nlohmann::json>(doc_seq_id_op.code(), doc_seq_id_op.error());
@@ -165,28 +186,68 @@ Option<nlohmann::json> Collection::add(const std::string & json_str) {
return Option<nlohmann::json>(403, "Max memory ratio exceeded.");
}*/
- const uint32_t seq_id = doc_seq_id_op.get();
+ const bool is_update = !doc_seq_id_op.get().is_new;
+ const uint32_t seq_id = doc_seq_id_op.get().seq_id;
const std::string seq_id_str = std::to_string(seq_id);
- const Option<uint32_t> & index_memory_op = index_in_memory(document, seq_id);
+ if(is_update) {
+ // fetch original doc from store, create new doc and write it back
+ nlohmann::json old_doc;
+ get_document_from_store(get_seq_id_key(seq_id), old_doc);
- if(!index_memory_op.ok()) {
- return Option<nlohmann::json>(index_memory_op.code(), index_memory_op.error());
- }
+ nlohmann::json new_doc;
- const std::string& serialized_json = document.dump(-1, ' ', false, nlohmann::detail::error_handler_t::ignore);
+ for(const auto& it: old_doc.items()) {
+ new_doc[it.key()] = it.value();
+ }
- rocksdb::WriteBatch batch;
- batch.Put(get_doc_id_key(document["id"]), seq_id_str);
- batch.Put(get_seq_id_key(seq_id), serialized_json);
- bool write_ok = store->batch_write(batch);
+ nlohmann::json changed_doc; // to identify changed fields
- if(!write_ok) {
- remove_document(document, seq_id, false); // remove from in-memory store too
- return Option<nlohmann::json>(500, "Could not write to on-disk storage.");
- }
+ for(const auto& it: document.items()) {
+ new_doc[it.key()] = it.value();
+ if(old_doc.count(it.key()) != 0) {
+ changed_doc[it.key()] = old_doc[it.key()];
+ }
+ }
- return Option<nlohmann::json>(document);
+ remove_document(changed_doc, seq_id, false);
+
+ const Option<uint32_t> & index_memory_op = index_in_memory(document, seq_id, is_update);
+ if(!index_memory_op.ok()) {
+ return Option<nlohmann::json>(index_memory_op.code(), index_memory_op.error());
+ }
+
+ const std::string& serialized_json = new_doc.dump(-1, ' ', false, nlohmann::detail::error_handler_t::ignore);
+ bool write_ok = store->insert(get_seq_id_key(seq_id), serialized_json);
+
+ if(!write_ok) {
+ // we will attempt to reindex the old doc on a best-effort basis
+ remove_document(new_doc, seq_id, false);
+ index_in_memory(old_doc, seq_id, false);
+ return Option<nlohmann::json>(500, "Could not write to on-disk storage.");
+ }
+
+ return Option<nlohmann::json>(new_doc);
+
+ } else {
+ const Option<uint32_t> & index_memory_op = index_in_memory(document, seq_id, is_update);
+ if(!index_memory_op.ok()) {
+ return Option<nlohmann::json>(index_memory_op.code(), index_memory_op.error());
+ }
+
+ const std::string& serialized_json = document.dump(-1, ' ', false, nlohmann::detail::error_handler_t::ignore);
+ rocksdb::WriteBatch batch;
+ batch.Put(get_doc_id_key(document["id"]), seq_id_str);
+ batch.Put(get_seq_id_key(seq_id), serialized_json);
+ bool write_ok = store->batch_write(batch);
+
+ if(!write_ok) {
+ remove_document(document, seq_id, false);
+ return Option<nlohmann::json>(500, "Could not write to on-disk storage.");
+ }
+
+ return Option<nlohmann::json>(document);
+ }
}
nlohmann::json Collection::add_many(std::vector<std::string>& json_lines) {
@@ -205,9 +266,9 @@ nlohmann::json Collection::add_many(std::vector<std::string>& json_lines) {
for(size_t i=0; i < json_lines.size(); i++) {
const std::string & json_line = json_lines[i];
nlohmann::json document;
- Option<uint32_t> doc_seq_id_op = to_doc(json_line, document);
+ Option<doc_seq_id_t> doc_seq_id_op = to_doc(json_line, document, false);
- const uint32_t seq_id = doc_seq_id_op.ok() ? doc_seq_id_op.get() : 0;
+ const uint32_t seq_id = doc_seq_id_op.ok() ? doc_seq_id_op.get().seq_id : 0;
index_record record(i, seq_id, document, CREATE);
// NOTE: we overwrite the input json_lines with result to avoid memory pressure
@@ -291,16 +352,16 @@ void Collection::batch_index(std::vector<std::vector<index_record>> &index_batch
}
}
-Option<uint32_t> Collection::index_in_memory(const nlohmann::json &document, uint32_t seq_id) {
+Option<uint32_t> Collection::index_in_memory(const nlohmann::json &document, uint32_t seq_id, bool is_update) {
Option<uint32_t> validation_op = Index::validate_index_in_memory(document, seq_id, default_sorting_field,
- search_schema, facet_schema);
+ search_schema, facet_schema, is_update);
if(!validation_op.ok()) {
return validation_op;
}
Index* index = indices[seq_id % num_memory_shards];
- index->index_in_memory(document, seq_id, default_sorting_field);
+ index->index_in_memory(document, seq_id, default_sorting_field, is_update);
num_documents += 1;
return Option<>(200);
@@ -1317,6 +1378,10 @@ void Collection::highlight_result(const field &search_field,
if(match.offsets[i].offset != MAX_DISPLACEMENT) {
size_t token_index = (size_t)(match.offsets[i].offset);
token_indices.push_back(token_index);
+ if(token_index >= tokens.size()) {
+ LOG(ERROR) << "Highlight token index " << token_index << " is greater than length of store field.";
+ continue;
+ }
std::string token = tokens[token_index];
string_utils.unicode_normalize(token);
token_hits.insert(token);
@@ -1402,7 +1467,7 @@ Option<nlohmann::json> Collection::get(const std::string & id) {
return Option<nlohmann::json>(500, "Error while fetching the document.");
}
- uint32_t seq_id = (uint32_t) std::stol(seq_id_str);
+ uint32_t seq_id = (uint32_t) std::stoul(seq_id_str);
std::string parsed_document;
StoreStatus doc_status = store->get(get_seq_id_key(seq_id), parsed_document);
@@ -1451,7 +1516,7 @@ Option<std::string> Collection::remove(const std::string & id, const bool remove
return Option<std::string>(500, "Error while fetching the document.");
}
- uint32_t seq_id = (uint32_t) std::stol(seq_id_str);
+ uint32_t seq_id = (uint32_t) std::stoul(seq_id_str);
std::string parsed_document;
StoreStatus doc_status = store->get(get_seq_id_key(seq_id), parsed_document);
diff --git a/src/core_api.cpp b/src/core_api.cpp
index f2b2f9c9..460bfc70 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -1044,7 +1044,7 @@ bool get_key(http_req &req, http_res &res) {
AuthManager &auth_manager = collectionManager.getAuthManager();
const std::string& key_id_str = req.params["id"];
- uint32_t key_id = (uint32_t) std::stol(key_id_str);
+ uint32_t key_id = (uint32_t) std::stoul(key_id_str);
const Option<api_key_t>& key_op = auth_manager.get_key(key_id);
@@ -1066,7 +1066,7 @@ bool del_key(http_req &req, http_res &res) {
AuthManager &auth_manager = collectionManager.getAuthManager();
const std::string& key_id_str = req.params["id"];
- uint32_t key_id = (uint32_t) std::stol(key_id_str);
+ uint32_t key_id = (uint32_t) std::stoul(key_id_str);
const Option<api_key_t> &del_op = auth_manager.remove_key(key_id);
diff --git a/src/index.cpp b/src/index.cpp
index 9b494c56..c3d423cc 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -85,8 +85,15 @@ int64_t Index::float_to_in64_t(float f) {
}
Option<uint32_t> Index::index_in_memory(const nlohmann::json &document, uint32_t seq_id,
- const std::string & default_sorting_field) {
- int64_t points = get_points_from_doc(document, default_sorting_field);
+ const std::string & default_sorting_field, bool is_update) {
+
+ int64_t points = 0;
+
+ if(is_update && document.count(default_sorting_field) == 0) {
+ points = sort_index[default_sorting_field]->at(seq_id);
+ } else {
+ points = get_points_from_doc(document, default_sorting_field);
+ }
std::unordered_map<std::string, size_t> facet_to_id;
size_t i_facet = 0;
@@ -104,7 +111,7 @@ Option<uint32_t> Index::index_in_memory(const nlohmann::json &document, uint32_t
for(const std::pair<std::string, field> & field_pair: search_schema) {
const std::string & field_name = field_pair.first;
- if(field_pair.second.optional && document.count(field_name) == 0) {
+ if((field_pair.second.optional || is_update) && document.count(field_name) == 0) {
continue;
}
@@ -212,17 +219,22 @@ Option<uint32_t> Index::index_in_memory(const nlohmann::json &document, uint32_t
Option<uint32_t> Index::validate_index_in_memory(const nlohmann::json &document, uint32_t seq_id,
const std::string & default_sorting_field,
const std::unordered_map<std::string, field> & search_schema,
- const std::map<std::string, field> & facet_schema) {
- if(document.count(default_sorting_field) == 0) {
+ const std::map<std::string, field> & facet_schema,
+ bool is_update) {
+
+ bool has_default_sort_field = (document.count(default_sorting_field) != 0);
+
+ if(!has_default_sort_field && !is_update) {
return Option<>(400, "Field `" + default_sorting_field + "` has been declared as a default sorting field, "
"but is not found in the document.");
}
- if(!document[default_sorting_field].is_number_integer() && !document[default_sorting_field].is_number_float()) {
+ if(has_default_sort_field &&
+ !document[default_sorting_field].is_number_integer() && !document[default_sorting_field].is_number_float()) {
return Option<>(400, "Default sorting field `" + default_sorting_field + "` must be a single valued numerical field.");
}
- if(search_schema.at(default_sorting_field).is_single_float() &&
+ if(has_default_sort_field && search_schema.at(default_sorting_field).is_single_float() &&
document[default_sorting_field].get<float>() > std::numeric_limits<float>::max()) {
return Option<>(400, "Default sorting field `" + default_sorting_field + "` exceeds maximum value of a float.");
}
@@ -230,7 +242,7 @@ Option<uint32_t> Index::validate_index_in_memory(const nlohmann::json &document,
for(const std::pair<std::string, field> & field_pair: search_schema) {
const std::string & field_name = field_pair.first;
- if(field_pair.second.optional && document.count(field_name) == 0) {
+ if((field_pair.second.optional || is_update) && document.count(field_name) == 0) {
continue;
}
@@ -325,14 +337,14 @@ size_t Index::batch_memory_index(Index *index, std::vector<index_record> & iter_
if(index_rec.operation == CREATE) {
Option<uint32_t> validation_op = validate_index_in_memory(index_rec.document, index_rec.seq_id,
default_sorting_field,
- search_schema, facet_schema);
+ search_schema, facet_schema, false);
if(!validation_op.ok()) {
index_rec.index_failure(validation_op.code(), validation_op.error());
continue;
}
- Option<uint32_t> index_mem_op = index->index_in_memory(index_rec.document, index_rec.seq_id, default_sorting_field);
+ Option<uint32_t> index_mem_op = index->index_in_memory(index_rec.document, index_rec.seq_id, default_sorting_field, false);
if(!index_mem_op.ok()) {
index_rec.index_failure(index_mem_op.code(), index_mem_op.error());
continue;
@@ -1803,73 +1815,75 @@ void Index::remove_and_shift_offset_index(sorted_array &offset_index, const uint
}
Option<uint32_t> Index::remove(const uint32_t seq_id, const nlohmann::json & document) {
+ std::unordered_map<std::string, size_t> facet_to_index;
+ get_facet_to_index(facet_to_index);
+
for(const auto& it: document.items()) {
const std::string& field_name = it.key();
- const auto& name_field = search_schema.find(field_name);
-
- if(name_field == search_schema.end()) {
+ const auto& search_field = search_schema.find(field_name);
+ if(search_field == search_schema.end()) {
continue;
}
// Go through all the field names and find the keys+values so that they can be removed from in-memory index
std::vector<std::string> tokens;
- if(name_field->second.type == field_types::STRING) {
- StringUtils::split(document[name_field->first], tokens, " ");
- } else if(name_field->second.type == field_types::STRING_ARRAY) {
- std::vector<std::string> values = document[name_field->first].get<std::vector<std::string>>();
+ if(search_field->second.type == field_types::STRING) {
+ StringUtils::split(document[search_field->first], tokens, " ");
+ } else if(search_field->second.type == field_types::STRING_ARRAY) {
+ std::vector<std::string> values = document[search_field->first].get<std::vector<std::string>>();
for(const std::string & value: values) {
StringUtils::split(value, tokens, " ");
}
- } else if(name_field->second.type == field_types::INT32) {
+ } else if(search_field->second.type == field_types::INT32) {
const int KEY_LEN = 8;
unsigned char key[KEY_LEN];
- int32_t value = document[name_field->first].get<int32_t>();
+ int32_t value = document[search_field->first].get<int32_t>();
encode_int32(value, key);
tokens.emplace_back((char*)key, KEY_LEN);
- } else if(name_field->second.type == field_types::INT32_ARRAY) {
- std::vector<int32_t> values = document[name_field->first].get<std::vector<int32_t>>();
+ } else if(search_field->second.type == field_types::INT32_ARRAY) {
+ std::vector<int32_t> values = document[search_field->first].get<std::vector<int32_t>>();
for(const int32_t value: values) {
const int KEY_LEN = 8;
unsigned char key[KEY_LEN];
encode_int32(value, key);
tokens.emplace_back((char*)key, KEY_LEN);
}
- } else if(name_field->second.type == field_types::INT64) {
+ } else if(search_field->second.type == field_types::INT64) {
const int KEY_LEN = 8;
unsigned char key[KEY_LEN];
- int64_t value = document[name_field->first].get<int64_t>();
+ int64_t value = document[search_field->first].get<int64_t>();
encode_int64(value, key);
tokens.emplace_back((char*)key, KEY_LEN);
- } else if(name_field->second.type == field_types::INT64_ARRAY) {
- std::vector<int64_t> values = document[name_field->first].get<std::vector<int64_t>>();
+ } else if(search_field->second.type == field_types::INT64_ARRAY) {
+ std::vector<int64_t> values = document[search_field->first].get<std::vector<int64_t>>();
for(const int64_t value: values) {
const int KEY_LEN = 8;
unsigned char key[KEY_LEN];
encode_int64(value, key);
tokens.emplace_back((char*)key, KEY_LEN);
}
- } else if(name_field->second.type == field_types::FLOAT) {
+ } else if(search_field->second.type == field_types::FLOAT) {
const int KEY_LEN = 8;
unsigned char key[KEY_LEN];
- int64_t value = document[name_field->first].get<int64_t>();
+ int64_t value = document[search_field->first].get<int64_t>();
encode_float(value, key);
tokens.emplace_back((char*)key, KEY_LEN);
- } else if(name_field->second.type == field_types::FLOAT_ARRAY) {
- std::vector<float> values = document[name_field->first].get<std::vector<float>>();
+ } else if(search_field->second.type == field_types::FLOAT_ARRAY) {
+ std::vector<float> values = document[search_field->first].get<std::vector<float>>();
for(const float value: values) {
const int KEY_LEN = 8;
unsigned char key[KEY_LEN];
encode_float(value, key);
tokens.emplace_back((char*)key, KEY_LEN);
}
- } else if(name_field->second.type == field_types::BOOL) {
+ } else if(search_field->second.type == field_types::BOOL) {
const int KEY_LEN = 1;
unsigned char key[KEY_LEN];
- bool value = document[name_field->first].get<bool>();
+ bool value = document[search_field->first].get<bool>();
key[0] = value ? '1' : '0';
tokens.emplace_back((char*)key, KEY_LEN);
- } else if(name_field->second.type == field_types::BOOL_ARRAY) {
- std::vector<bool> values = document[name_field->first].get<std::vector<bool>>();
+ } else if(search_field->second.type == field_types::BOOL_ARRAY) {
+ std::vector<bool> values = document[search_field->first].get<std::vector<bool>>();
for(const bool value: values) {
const int KEY_LEN = 1;
unsigned char key[KEY_LEN];
@@ -1882,7 +1896,7 @@ Option<uint32_t> Index::remove(const uint32_t seq_id, const nlohmann::json & doc
const unsigned char *key;
int key_len;
- if(name_field->second.type == field_types::STRING_ARRAY || name_field->second.type == field_types::STRING) {
+ if(search_field->second.type == field_types::STRING_ARRAY || search_field->second.type == field_types::STRING) {
string_utils.unicode_normalize(token);
key = (const unsigned char *) token.c_str();
key_len = (int) (token.length() + 1);
@@ -1891,7 +1905,7 @@ Option<uint32_t> Index::remove(const uint32_t seq_id, const nlohmann::json & doc
key_len = (int) (token.length());
}
- art_leaf* leaf = (art_leaf *) art_search(search_index.at(name_field->first), key, key_len);
+ art_leaf* leaf = (art_leaf *) art_search(search_index.at(search_field->first), key, key_len);
if(leaf != nullptr) {
uint32_t seq_id_values[1] = {seq_id};
uint32_t doc_index = leaf->values->ids.indexOf(seq_id);
@@ -1919,19 +1933,23 @@ Option<uint32_t> Index::remove(const uint32_t seq_id, const nlohmann::json & doc
LOG(INFO) << "----";*/
if(leaf->values->ids.getLength() == 0) {
- art_values* values = (art_values*) art_delete(search_index.at(name_field->first), key, key_len);
+ art_values* values = (art_values*) art_delete(search_index.at(search_field->first), key, key_len);
delete values;
}
}
}
- }
- // remove facets if any
- facet_index_v2.erase(seq_id);
+ // remove facets
+ if(facet_to_index.count(field_name) != 0 && facet_index_v2.count(seq_id) != 0) {
+ size_t facet_index = facet_to_index[field_name];
+ std::vector<std::vector<uint64_t>>& facet_values = facet_index_v2[seq_id];
+ facet_values[facet_index].clear();
+ }
- // remove sort index if any
- for(auto & field_doc_value_map: sort_index) {
- field_doc_value_map.second->erase(seq_id);
+ // remove sort field
+ if(sort_index.count(field_name) != 0) {
+ sort_index[field_name]->erase(seq_id);
+ }
}
return Option<uint32_t>(seq_id);
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index dea95760..897d3b45 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -2213,6 +2213,98 @@ TEST_F(CollectionTest, SearchHighlightShouldFollowThreshold) {
collectionManager.drop_collection("coll1");
}
+TEST_F(CollectionTest, UpdateDocument) {
+ Collection *coll1;
+
+ std::vector<field> fields = {field("title", field_types::STRING, true),
+ field("tags", field_types::STRING_ARRAY, true),
+ field("points", field_types::INT32, false)};
+
+ std::vector<sort_by> sort_fields = {sort_by("points", "DESC")};
+
+ coll1 = collectionManager.get_collection("coll1");
+ if (coll1 == nullptr) {
+ coll1 = collectionManager.create_collection("coll1", 1, fields, "points").get();
+ }
+
+ nlohmann::json doc;
+ doc["id"] = "100";
+ doc["title"] = "The quick brown fox jumped over the lazy dog and ran straight to the forest to sleep.";
+ doc["tags"] = {"NEWS", "LAZY"};
+ doc["points"] = 25;
+
+ auto add_op = coll1->add(doc.dump());
+ ASSERT_TRUE(add_op.ok());
+
+ auto res = coll1->search("lazy", {"title"}, "", {}, sort_fields, 0, 10, 1,
+ token_ordering::FREQUENCY, true, 10, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 5, "title").get();
+
+ ASSERT_EQ(1, res["hits"].size());
+ ASSERT_STREQ("The quick brown fox jumped over the lazy dog and ran straight to the forest to sleep.",
+ res["hits"][0]["document"]["title"].get<std::string>().c_str());
+
+ // try changing the title and searching for an older token
+ doc["title"] = "The quick brown fox.";
+ add_op = coll1->add(doc.dump(), true);
+ ASSERT_TRUE(add_op.ok());
+
+ res = coll1->search("lazy", {"title"}, "", {}, sort_fields, 0, 10, 1,
+ token_ordering::FREQUENCY, true, 10, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 5, "title").get();
+
+ ASSERT_EQ(0, res["hits"].size());
+
+ res = coll1->search("quick", {"title"}, "", {}, sort_fields, 0, 10, 1,
+ token_ordering::FREQUENCY, true, 10, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 5, "title").get();
+
+ ASSERT_EQ(1, res["hits"].size());
+ ASSERT_STREQ("The quick brown fox.", res["hits"][0]["document"]["title"].get<std::string>().c_str());
+
+ // try to change tags without `id`
+ nlohmann::json doc2;
+ doc2["tags"] = {"SENTENCE"};
+ add_op = coll1->add(doc2.dump(), true);
+ ASSERT_FALSE(add_op.ok());
+ ASSERT_STREQ("For update, the `id` key must be present.", add_op.error().c_str());
+
+ // now change tags with id
+ doc2["id"] = "100";
+ add_op = coll1->add(doc2.dump(), true);
+ ASSERT_TRUE(add_op.ok());
+
+ // check for old tag
+ res = coll1->search("NEWS", {"tags"}, "", {}, sort_fields, 0, 10, 1,
+ token_ordering::FREQUENCY, true, 10, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 5, "title").get();
+
+ ASSERT_EQ(0, res["hits"].size());
+
+ // now check for new tag and also try faceting on that field
+ res = coll1->search("SENTENCE", {"tags"}, "", {"tags"}, sort_fields, 0, 10, 1,
+ token_ordering::FREQUENCY, true, 10, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 5, "title").get();
+
+ ASSERT_EQ(1, res["hits"].size());
+ ASSERT_STREQ("SENTENCE", res["facet_counts"][0]["counts"][0]["value"].get<std::string>().c_str());
+
+ // try changing points
+ nlohmann::json doc3;
+ doc3["points"] = 99;
+ doc3["id"] = "100";
+
+ add_op = coll1->add(doc3.dump(), true);
+ ASSERT_TRUE(add_op.ok());
+
+ res = coll1->search("*", {"tags"}, "points: > 90", {}, sort_fields, 0, 10, 1,
+ token_ordering::FREQUENCY, true, 10, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 5, "title").get();
+
+ ASSERT_EQ(1, res["hits"].size());
+ ASSERT_EQ(99, res["hits"][0]["document"]["points"].get<size_t>());
+}
+
TEST_F(CollectionTest, SearchHighlightFieldFully) {
Collection *coll1;