Skip to content

Commit

Permalink
Fix persistence range_index and stem properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
kishorenc committed Jul 14, 2024
1 parent 99e1106 commit 917cca9
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 122 deletions.
123 changes: 1 addition & 122 deletions include/field.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,128 +339,7 @@ struct field {

static Option<bool> fields_to_json_fields(const std::vector<field> & fields,
const std::string & default_sorting_field,
nlohmann::json& fields_json) {
bool found_default_sorting_field = false;

// Check for duplicates in field names
std::map<std::string, std::vector<const field*>> unique_fields;

for(const field & field: fields) {
unique_fields[field.name].push_back(&field);

if(field.name == "id") {
continue;
}

nlohmann::json field_val;
field_val[fields::name] = field.name;
field_val[fields::type] = field.type;
field_val[fields::facet] = field.facet;
field_val[fields::optional] = field.optional;
field_val[fields::index] = field.index;
field_val[fields::sort] = field.sort;
field_val[fields::infix] = field.infix;

field_val[fields::locale] = field.locale;

field_val[fields::store] = field.store;

if(field.embed.count(fields::from) != 0) {
field_val[fields::embed] = field.embed;
}

field_val[fields::nested] = field.nested;
if(field.nested) {
field_val[fields::nested_array] = field.nested_array;
}

if(field.num_dim > 0) {
field_val[fields::num_dim] = field.num_dim;
field_val[fields::vec_dist] = field.vec_dist == ip ? "ip" : "cosine";
}

if (!field.reference.empty()) {
field_val[fields::reference] = field.reference;
}

fields_json.push_back(field_val);

if(!field.has_valid_type()) {
return Option<bool>(400, "Field `" + field.name +
"` has an invalid data type `" + field.type +
"`, see docs for supported data types.");
}

if(field.name == default_sorting_field && !field.is_sortable()) {
return Option<bool>(400, "Default sorting field `" + default_sorting_field +
"` is not a sortable type.");
}

if(field.name == default_sorting_field) {
if(field.optional) {
return Option<bool>(400, "Default sorting field `" + default_sorting_field +
"` cannot be an optional field.");
}

if(field.is_geopoint()) {
return Option<bool>(400, "Default sorting field cannot be of type geopoint.");
}

found_default_sorting_field = true;
}

if(field.is_dynamic() && !field.nested && !field.optional) {
return Option<bool>(400, "Field `" + field.name + "` must be an optional field.");
}

if(field.name == ".*" && !field.index) {
return Option<bool>(400, "Field `" + field.name + "` cannot be marked as non-indexable.");
}

if(!field.index && field.facet) {
return Option<bool>(400, "Field `" + field.name + "` cannot be a facet since "
"it's marked as non-indexable.");
}

if(!field.is_sort_field() && field.sort) {
return Option<bool>(400, "Field `" + field.name + "` cannot be a sortable field.");
}
}

if(!default_sorting_field.empty() && !found_default_sorting_field) {
return Option<bool>(400, "Default sorting field is defined as `" + default_sorting_field +
"` but is not found in the schema.");
}

// check for duplicate field names in schema
for(auto& fname_fields: unique_fields) {
if(fname_fields.second.size() > 1) {
// if there are more than 1 field with the same field name, then
// a) only 1 field can be of static type
// b) only 1 field can be of dynamic type
size_t num_static = 0;
size_t num_dynamic = 0;

for(const field* f: fname_fields.second) {
if(f->name == ".*" || f->is_dynamic()) {
num_dynamic++;
} else {
num_static++;
}
}

if(num_static != 0 && num_static > 1) {
return Option<bool>(400, "There are duplicate field names in the schema.");
}

if(num_dynamic != 0 && num_dynamic > 1) {
return Option<bool>(400, "There are duplicate field names in the schema.");
}
}
}

return Option<bool>(true);
}
nlohmann::json& fields_json);

static Option<bool> json_field_to_field(bool enable_nested_fields, nlohmann::json& field_json,
std::vector<field>& the_fields,
Expand Down
126 changes: 126 additions & 0 deletions src/field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -761,3 +761,129 @@ Option<bool> field::validate_and_init_embed_field(const tsl::htrie_map<char, fie

return Option<bool>(true);
}

Option<bool> field::fields_to_json_fields(const std::vector<field>& fields, const string& default_sorting_field,
nlohmann::json& fields_json) {
bool found_default_sorting_field = false;

// Check for duplicates in field names
std::map<std::string, std::vector<const field*>> unique_fields;

for(const field & field: fields) {
unique_fields[field.name].push_back(&field);

if(field.name == "id") {
continue;
}

nlohmann::json field_val;
field_val[fields::name] = field.name;
field_val[fields::type] = field.type;
field_val[fields::facet] = field.facet;
field_val[fields::optional] = field.optional;
field_val[fields::index] = field.index;
field_val[fields::sort] = field.sort;
field_val[fields::infix] = field.infix;

field_val[fields::locale] = field.locale;

field_val[fields::store] = field.store;
field_val[fields::stem] = field.stem;
field_val[fields::range_index] = field.range_index;

if(field.embed.count(fields::from) != 0) {
field_val[fields::embed] = field.embed;
}

field_val[fields::nested] = field.nested;
if(field.nested) {
field_val[fields::nested_array] = field.nested_array;
}

if(field.num_dim > 0) {
field_val[fields::num_dim] = field.num_dim;
field_val[fields::vec_dist] = field.vec_dist == ip ? "ip" : "cosine";
}

if (!field.reference.empty()) {
field_val[fields::reference] = field.reference;
}

fields_json.push_back(field_val);

if(!field.has_valid_type()) {
return Option<bool>(400, "Field `" + field.name +
"` has an invalid data type `" + field.type +
"`, see docs for supported data types.");
}

if(field.name == default_sorting_field && !field.is_sortable()) {
return Option<bool>(400, "Default sorting field `" + default_sorting_field +
"` is not a sortable type.");
}

if(field.name == default_sorting_field) {
if(field.optional) {
return Option<bool>(400, "Default sorting field `" + default_sorting_field +
"` cannot be an optional field.");
}

if(field.is_geopoint()) {
return Option<bool>(400, "Default sorting field cannot be of type geopoint.");
}

found_default_sorting_field = true;
}

if(field.is_dynamic() && !field.nested && !field.optional) {
return Option<bool>(400, "Field `" + field.name + "` must be an optional field.");
}

if(field.name == ".*" && !field.index) {
return Option<bool>(400, "Field `" + field.name + "` cannot be marked as non-indexable.");
}

if(!field.index && field.facet) {
return Option<bool>(400, "Field `" + field.name + "` cannot be a facet since "
"it's marked as non-indexable.");
}

if(!field.is_sort_field() && field.sort) {
return Option<bool>(400, "Field `" + field.name + "` cannot be a sortable field.");
}
}

if(!default_sorting_field.empty() && !found_default_sorting_field) {
return Option<bool>(400, "Default sorting field is defined as `" + default_sorting_field +
"` but is not found in the schema.");
}

// check for duplicate field names in schema
for(auto& fname_fields: unique_fields) {
if(fname_fields.second.size() > 1) {
// if there are more than 1 field with the same field name, then
// a) only 1 field can be of static type
// b) only 1 field can be of dynamic type
size_t num_static = 0;
size_t num_dynamic = 0;

for(const field* f: fname_fields.second) {
if(f->name == ".*" || f->is_dynamic()) {
num_dynamic++;
} else {
num_static++;
}
}

if(num_static != 0 && num_static > 1) {
return Option<bool>(400, "There are duplicate field names in the schema.");
}

if(num_dynamic != 0 && num_dynamic > 1) {
return Option<bool>(400, "There are duplicate field names in the schema.");
}
}
}

return Option<bool>(true);
}

0 comments on commit 917cca9

Please sign in to comment.