Add support for booleans in schemas and docs (#105)

master
Chris Tam 2023-08-15 18:39:28 -04:00 committed by GitHub
parent ab2f435d9e
commit 9f932aeebe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 86 additions and 29 deletions

View File

@ -6,7 +6,8 @@ use pyo3::{
basic::CompareOp,
prelude::*,
types::{
PyAny, PyDateAccess, PyDateTime, PyDict, PyList, PyTimeAccess, PyTuple,
PyAny, PyBool, PyDateAccess, PyDateTime, PyDict, PyList, PyTimeAccess,
PyTuple,
},
};
@ -26,6 +27,9 @@ pub(crate) fn extract_value(any: &PyAny) -> PyResult<Value> {
if let Ok(s) = any.extract::<String>() {
return Ok(Value::Str(s));
}
if any.is_exact_instance_of::<PyBool>() {
return Ok(Value::Bool(any.extract::<bool>()?));
}
if let Ok(num) = any.extract::<i64>() {
return Ok(Value::I64(num));
}
@ -78,6 +82,10 @@ pub(crate) fn extract_value_for_type(
any.extract::<i64>()
.map_err(to_pyerr_for_type("I64", field_name, any))?,
),
tv::schema::Type::Bool => Value::Bool(
any.extract::<bool>()
.map_err(to_pyerr_for_type("Bool", field_name, any))?,
),
tv::schema::Type::F64 => Value::F64(
any.extract::<f64>()
.map_err(to_pyerr_for_type("F64", field_name, any))?,
@ -382,6 +390,15 @@ impl Document {
self.add_value(field_name, value);
}
/// Add a boolean value to the document.
///
/// Args:
/// field_name (str): The field name for which we are adding the value.
/// value (bool): The boolean that will be added to the document.
fn add_boolean(&mut self, field_name: String, value: bool) {
self.add_value(field_name, value);
}
/// Add a date value to the document.
///
/// Args:

View File

@ -98,15 +98,10 @@ impl SchemaBuilder {
/// content of the field can be later restored from a Searcher.
/// Defaults to False.
/// indexed (bool, optional): If true sets the field to be indexed.
/// fast (str, optional): Set the u64 options as a single-valued fast
/// field. Fast fields are designed for random access. Access time
/// are similar to a random lookup in an array. If more than one
/// value is associated to a fast field, only the last one is kept.
/// Can be one of 'single' or 'multi'. If this is set to 'single,
/// the document must have exactly one value associated to the
/// document. If this is set to 'multi', the document can have any
/// number of values associated to the document. Defaults to None,
/// which disables this option.
/// fast (str, optional): Set the numeric options as a fast field. A
/// fast field is a column-oriented fashion storage for tantivy.
/// It is designed for the fast random access of some document
/// fields given a document id.
///
/// Returns the associated field handle.
/// Raises a ValueError if there was an error with the field creation.
@ -132,6 +127,21 @@ impl SchemaBuilder {
Ok(self.clone())
}
/// Add a new float field to the schema.
///
/// Args:
/// name (str): The name of the field.
/// stored (bool, optional): If true sets the field as stored, the
/// content of the field can be later restored from a Searcher.
/// Defaults to False.
/// indexed (bool, optional): If true sets the field to be indexed.
/// fast (str, optional): Set the numeric options as a fast field. A
/// fast field is a column-oriented fashion storage for tantivy.
/// It is designed for the fast random access of some document
/// fields given a document id.
///
/// Returns the associated field handle.
/// Raises a ValueError if there was an error with the field creation.
#[pyo3(signature = (name, stored = false, indexed = false, fast = false))]
fn add_float_field(
&mut self,
@ -162,15 +172,10 @@ impl SchemaBuilder {
/// content of the field can be later restored from a Searcher.
/// Defaults to False.
/// indexed (bool, optional): If true sets the field to be indexed.
/// fast (str, optional): Set the u64 options as a single-valued fast
/// field. Fast fields are designed for random access. Access time
/// are similar to a random lookup in an array. If more than one
/// value is associated to a fast field, only the last one is kept.
/// Can be one of 'single' or 'multi'. If this is set to 'single,
/// the document must have exactly one value associated to the
/// document. If this is set to 'multi', the document can have any
/// number of values associated to the document. Defaults to None,
/// which disables this option.
/// fast (str, optional): Set the numeric options as a fast field. A
/// fast field is a column-oriented fashion storage for tantivy.
/// It is designed for the fast random access of some document
/// fields given a document id.
///
/// Returns the associated field handle.
/// Raises a ValueError if there was an error with the field creation.
@ -196,6 +201,43 @@ impl SchemaBuilder {
Ok(self.clone())
}
/// Add a new boolean field to the schema.
///
/// Args:
/// name (str): The name of the field.
/// stored (bool, optional): If true sets the field as stored, the
/// content of the field can be later restored from a Searcher.
/// Defaults to False.
/// indexed (bool, optional): If true sets the field to be indexed.
/// fast (str, optional): Set the numeric options as a fast field. A
/// fast field is a column-oriented fashion storage for tantivy.
/// It is designed for the fast random access of some document
/// fields given a document id.
///
/// Returns the associated field handle.
/// Raises a ValueError if there was an error with the field creation.
#[pyo3(signature = (name, stored = false, indexed = false, fast = false))]
fn add_boolean_field(
&mut self,
name: &str,
stored: bool,
indexed: bool,
fast: bool,
) -> PyResult<Self> {
let builder = &mut self.builder;
let opts = SchemaBuilder::build_numeric_option(stored, indexed, fast)?;
if let Some(builder) = builder.write().unwrap().as_mut() {
builder.add_bool_field(name, opts);
} else {
return Err(exceptions::PyValueError::new_err(
"Schema builder object isn't valid anymore.",
));
}
Ok(self.clone())
}
/// Add a new date field to the schema.
///
/// Args:
@ -204,15 +246,10 @@ impl SchemaBuilder {
/// content of the field can be later restored from a Searcher.
/// Defaults to False.
/// indexed (bool, optional): If true sets the field to be indexed.
/// fast (str, optional): Set the u64 options as a single-valued fast
/// field. Fast fields are designed for random access. Access time
/// are similar to a random lookup in an array. If more than one
/// value is associated to a fast field, only the last one is kept.
/// Can be one of 'single' or 'multi'. If this is set to 'single',
/// the document must have exactly one value associated to the
/// document. If this is set to 'multi', the document can have any
/// number of values associated to the document. Defaults to None,
/// which disables this option.
/// fast (str, optional): Set the date options as a fast field. A fast
/// field is a column-oriented fashion storage for tantivy. It is
/// designed for the fast random access of some document fields
/// given a document id.
///
/// Returns the associated field handle.
/// Raises a ValueError if there was an error with the field creation.

View File

@ -20,6 +20,7 @@ def schema_numeric_fields():
SchemaBuilder()
.add_integer_field("id", stored=True, indexed=True)
.add_float_field("rating", stored=True, indexed=True)
.add_boolean_field("is_good", stored=True, indexed=True)
.add_text_field("body", stored=True)
.build()
)
@ -86,6 +87,7 @@ def create_index_with_numeric_fields(dir=None):
doc = Document()
doc.add_integer("id", 1)
doc.add_float("rating", 3.5)
doc.add_boolean("is_good", True)
doc.add_text(
"body",
(
@ -99,6 +101,7 @@ def create_index_with_numeric_fields(dir=None):
{
"id": 2,
"rating": 4.5,
"is_good": False,
"body": (
"A few miles south of Soledad, the Salinas River drops "
"in close to the hillside bank and runs deep and "
@ -113,7 +116,7 @@ def create_index_with_numeric_fields(dir=None):
"sycamores with mottled, white, recumbent limbs and "
"branches that arch over the pool"
),
}
},
)
writer.add_document(doc)
writer.commit()