Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic on duplicate field names fixes #1601 #1647

Merged
merged 1 commit into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ pub mod tests {
fn test_indexedfield_not_in_documents() -> crate::Result<()> {
let mut schema_builder = Schema::builder();
let text_field = schema_builder.add_text_field("text", TEXT);
let absent_field = schema_builder.add_text_field("text", TEXT);
let absent_field = schema_builder.add_text_field("absent_text", TEXT);
let schema = schema_builder.build();
let index = Index::create_in_ram(schema);
let mut index_writer = index.writer_for_tests()?;
Expand Down Expand Up @@ -1001,7 +1001,7 @@ pub mod tests {
let fast_field_signed = schema_builder.add_i64_field("signed", FAST);
let fast_field_float = schema_builder.add_f64_field("float", FAST);
let text_field = schema_builder.add_text_field("text", TEXT);
let stored_int_field = schema_builder.add_u64_field("text", STORED);
let stored_int_field = schema_builder.add_u64_field("stored_int", STORED);
let schema = schema_builder.build();

let index = Index::create_in_ram(schema);
Expand Down
2 changes: 1 addition & 1 deletion src/query/set_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ mod tests {
pub fn test_term_set_query() -> crate::Result<()> {
let mut schema_builder = Schema::builder();
let field1 = schema_builder.add_text_field("field1", TEXT);
let field2 = schema_builder.add_text_field("field1", TEXT);
let field2 = schema_builder.add_text_field("field2", TEXT);
let schema = schema_builder.build();
let index = Index::create_in_ram(schema);
{
Expand Down
60 changes: 17 additions & 43 deletions src/schema/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,9 @@ impl SchemaBuilder {
/// Adds a new u64 field.
/// Returns the associated field handle
///
/// # Caution
/// # Panics
///
/// Appending two fields with the same name
/// will result in the shadowing of the first
/// by the second one.
/// The first field will get a field id
/// but only the second one will be indexed
/// Panics when field already exists.
pub fn add_u64_field<T: Into<NumericOptions>>(
&mut self,
field_name_str: &str,
Expand All @@ -66,13 +62,9 @@ impl SchemaBuilder {
/// Adds a new i64 field.
/// Returns the associated field handle
///
/// # Caution
/// # Panics
///
/// Appending two fields with the same name
/// will result in the shadowing of the first
/// by the second one.
/// The first field will get a field id
/// but only the second one will be indexed
/// Panics when field already exists.
pub fn add_i64_field<T: Into<NumericOptions>>(
&mut self,
field_name_str: &str,
Expand All @@ -86,13 +78,9 @@ impl SchemaBuilder {
/// Adds a new f64 field.
/// Returns the associated field handle
///
/// # Caution
/// # Panics
///
/// Appending two fields with the same name
/// will result in the shadowing of the first
/// by the second one.
/// The first field will get a field id
/// but only the second one will be indexed
/// Panics when field already exists.
pub fn add_f64_field<T: Into<NumericOptions>>(
&mut self,
field_name_str: &str,
Expand All @@ -106,13 +94,9 @@ impl SchemaBuilder {
/// Adds a new bool field.
/// Returns the associated field handle
///
/// # Caution
/// # Panics
///
/// Appending two fields with the same name
/// will result in the shadowing of the first
/// by the second one.
/// The first field will get a field id
/// but only the second one will be indexed
/// Panics when field already exists.
pub fn add_bool_field<T: Into<NumericOptions>>(
&mut self,
field_name_str: &str,
Expand All @@ -128,13 +112,9 @@ impl SchemaBuilder {
/// Internally, Tantivy simply stores dates as i64 UTC timestamps,
/// while the user supplies DateTime values for convenience.
///
/// # Caution
/// # Panics
///
/// Appending two fields with the same name
/// will result in the shadowing of the first
/// by the second one.
/// The first field will get a field id
/// but only the second one will be indexed
/// Panics when field already exists.
pub fn add_date_field<T: Into<DateOptions>>(
&mut self,
field_name_str: &str,
Expand All @@ -148,13 +128,9 @@ impl SchemaBuilder {
/// Adds a ip field.
/// Returns the associated field handle.
///
/// # Caution
/// # Panics
///
/// Appending two fields with the same name
/// will result in the shadowing of the first
/// by the second one.
/// The first field will get a field id
/// but only the second one will be indexed
/// Panics when field already exists.
pub fn add_ip_addr_field<T: Into<IpAddrOptions>>(
&mut self,
field_name_str: &str,
Expand All @@ -168,13 +144,9 @@ impl SchemaBuilder {
/// Adds a new text field.
/// Returns the associated field handle
///
/// # Caution
/// # Panics
///
/// Appending two fields with the same name
/// will result in the shadowing of the first
/// by the second one.
/// The first field will get a field id
/// but only the second one will be indexed
/// Panics when field already exists.
pub fn add_text_field<T: Into<TextOptions>>(
&mut self,
field_name_str: &str,
Expand Down Expand Up @@ -228,8 +200,10 @@ impl SchemaBuilder {
pub fn add_field(&mut self, field_entry: FieldEntry) -> Field {
let field = Field::from_field_id(self.fields.len() as u32);
let field_name = field_entry.name().to_string();
if let Some(_previous_value) = self.fields_map.insert(field_name, field) {
panic!("Field already exists in schema {}", field_entry.name());
};
self.fields.push(field_entry);
self.fields_map.insert(field_name, field);
field
}

Expand Down