mirror of
https://github.com/typesense/typesense.git
synced 2025-05-22 14:55:26 +08:00
Abstract foo_sequence_id
field from user.
This commit is contained in:
parent
e16b4b7349
commit
b41db06b1a
@ -9,6 +9,7 @@
|
||||
#include <sparsepp.h>
|
||||
#include <tsl/htrie_map.h>
|
||||
#include "json.hpp"
|
||||
#include <regex>
|
||||
|
||||
namespace field_types {
|
||||
// first field value indexed will determine the type
|
||||
@ -278,11 +279,17 @@ struct field {
|
||||
const std::string & default_sorting_field,
|
||||
nlohmann::json& fields_json) {
|
||||
bool found_default_sorting_field = false;
|
||||
const std::regex sequence_id_pattern(".*_sequence_id$");
|
||||
|
||||
// Check for duplicates in field names
|
||||
std::map<std::string, std::vector<const field*>> unique_fields;
|
||||
|
||||
for(const field & field: fields) {
|
||||
if (std::regex_match(field.name, sequence_id_pattern)) {
|
||||
// Don't add foo_sequence_id field.
|
||||
continue;
|
||||
}
|
||||
|
||||
unique_fields[field.name].push_back(&field);
|
||||
|
||||
if(field.name == "id") {
|
||||
|
@ -80,6 +80,10 @@ Collection* CollectionManager::init_collection(const nlohmann::json & collection
|
||||
}
|
||||
|
||||
fields.push_back(f);
|
||||
|
||||
if (!f.reference.empty()) {
|
||||
fields.emplace_back(field(f.name + "_sequence_id", "string", false, f.optional, true));
|
||||
}
|
||||
}
|
||||
|
||||
std::string default_sorting_field = collection_meta[Collection::COLLECTION_DEFAULT_SORTING_FIELD_KEY].get<std::string>();
|
||||
|
@ -700,7 +700,7 @@ Option<bool> field::json_field_to_field(bool enable_nested_fields, nlohmann::jso
|
||||
if (!field_json[fields::reference].get<std::string>().empty()) {
|
||||
the_fields.emplace_back(
|
||||
field(field_json[fields::name].get<std::string>() + "_sequence_id", "string", false,
|
||||
false, true)
|
||||
field_json[fields::optional], true)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -92,10 +92,10 @@ TEST_F(CollectionJoinTest, SchemaReferenceField) {
|
||||
|
||||
ASSERT_EQ(schema.at("customer_name").reference, "");
|
||||
ASSERT_EQ(schema.at("product_id").reference, "Products.product_id");
|
||||
ASSERT_EQ(schema.count("product_id_sequence_id"), 1);
|
||||
|
||||
auto field = schema.at("product_id_sequence_id");
|
||||
ASSERT_TRUE(field.index);
|
||||
// Index a `foo_sequence_id` field for `foo` reference field.
|
||||
ASSERT_EQ(schema.count("product_id_sequence_id"), 1);
|
||||
ASSERT_TRUE(schema.at("product_id_sequence_id").index);
|
||||
|
||||
collectionManager.drop_collection("Customers");
|
||||
}
|
@ -221,7 +221,7 @@ TEST_F(CollectionManagerTest, CollectionCreation) {
|
||||
"locale":"",
|
||||
"name":"product_id",
|
||||
"nested":false,
|
||||
"optional":false,
|
||||
"optional":true,
|
||||
"sort":false,
|
||||
"type":"string",
|
||||
"reference":"Products.product_id"
|
||||
@ -351,7 +351,11 @@ TEST_F(CollectionManagerTest, RestoreRecordsOnRestart) {
|
||||
std::string json_line;
|
||||
|
||||
while (std::getline(infile, json_line)) {
|
||||
collection1->add(json_line);
|
||||
auto op = collection1->add(json_line);
|
||||
if (!op.ok()) {
|
||||
LOG(INFO) << op.error();
|
||||
}
|
||||
ASSERT_TRUE(op.ok());
|
||||
}
|
||||
|
||||
infile.close();
|
||||
@ -434,6 +438,7 @@ TEST_F(CollectionManagerTest, RestoreRecordsOnRestart) {
|
||||
ASSERT_EQ(4, results["hits"].size());
|
||||
|
||||
tsl::htrie_map<char, field> schema = collection1->get_schema();
|
||||
ASSERT_EQ(schema.count("product_id_sequence_id"), 1);
|
||||
|
||||
// recreate collection manager to ensure that it restores the records from the disk backed store
|
||||
collectionManager.dispose();
|
||||
@ -472,6 +477,7 @@ TEST_F(CollectionManagerTest, RestoreRecordsOnRestart) {
|
||||
ASSERT_TRUE(restored_schema.at("person").nested);
|
||||
ASSERT_EQ(2, restored_schema.at("person").nested_array);
|
||||
ASSERT_EQ(128, restored_schema.at("vec").num_dim);
|
||||
ASSERT_EQ(restored_schema.count("product_id_sequence_id"), 1);
|
||||
|
||||
ASSERT_TRUE(collection1->get_enable_nested_fields());
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user