Add support for booleans in schemas and docs (#105)
parent
ab2f435d9e
commit
9f932aeebe
|
@ -6,7 +6,8 @@ use pyo3::{
|
||||||
basic::CompareOp,
|
basic::CompareOp,
|
||||||
prelude::*,
|
prelude::*,
|
||||||
types::{
|
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>() {
|
if let Ok(s) = any.extract::<String>() {
|
||||||
return Ok(Value::Str(s));
|
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>() {
|
if let Ok(num) = any.extract::<i64>() {
|
||||||
return Ok(Value::I64(num));
|
return Ok(Value::I64(num));
|
||||||
}
|
}
|
||||||
|
@ -78,6 +82,10 @@ pub(crate) fn extract_value_for_type(
|
||||||
any.extract::<i64>()
|
any.extract::<i64>()
|
||||||
.map_err(to_pyerr_for_type("I64", field_name, any))?,
|
.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(
|
tv::schema::Type::F64 => Value::F64(
|
||||||
any.extract::<f64>()
|
any.extract::<f64>()
|
||||||
.map_err(to_pyerr_for_type("F64", field_name, any))?,
|
.map_err(to_pyerr_for_type("F64", field_name, any))?,
|
||||||
|
@ -382,6 +390,15 @@ impl Document {
|
||||||
self.add_value(field_name, value);
|
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.
|
/// Add a date value to the document.
|
||||||
///
|
///
|
||||||
/// Args:
|
/// Args:
|
||||||
|
|
|
@ -98,15 +98,10 @@ impl SchemaBuilder {
|
||||||
/// content of the field can be later restored from a Searcher.
|
/// content of the field can be later restored from a Searcher.
|
||||||
/// Defaults to False.
|
/// Defaults to False.
|
||||||
/// indexed (bool, optional): If true sets the field to be indexed.
|
/// indexed (bool, optional): If true sets the field to be indexed.
|
||||||
/// fast (str, optional): Set the u64 options as a single-valued fast
|
/// fast (str, optional): Set the numeric options as a fast field. A
|
||||||
/// field. Fast fields are designed for random access. Access time
|
/// fast field is a column-oriented fashion storage for tantivy.
|
||||||
/// are similar to a random lookup in an array. If more than one
|
/// It is designed for the fast random access of some document
|
||||||
/// value is associated to a fast field, only the last one is kept.
|
/// fields given a document id.
|
||||||
/// 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.
|
|
||||||
///
|
///
|
||||||
/// Returns the associated field handle.
|
/// Returns the associated field handle.
|
||||||
/// Raises a ValueError if there was an error with the field creation.
|
/// Raises a ValueError if there was an error with the field creation.
|
||||||
|
@ -132,6 +127,21 @@ impl SchemaBuilder {
|
||||||
Ok(self.clone())
|
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))]
|
#[pyo3(signature = (name, stored = false, indexed = false, fast = false))]
|
||||||
fn add_float_field(
|
fn add_float_field(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
@ -162,15 +172,10 @@ impl SchemaBuilder {
|
||||||
/// content of the field can be later restored from a Searcher.
|
/// content of the field can be later restored from a Searcher.
|
||||||
/// Defaults to False.
|
/// Defaults to False.
|
||||||
/// indexed (bool, optional): If true sets the field to be indexed.
|
/// indexed (bool, optional): If true sets the field to be indexed.
|
||||||
/// fast (str, optional): Set the u64 options as a single-valued fast
|
/// fast (str, optional): Set the numeric options as a fast field. A
|
||||||
/// field. Fast fields are designed for random access. Access time
|
/// fast field is a column-oriented fashion storage for tantivy.
|
||||||
/// are similar to a random lookup in an array. If more than one
|
/// It is designed for the fast random access of some document
|
||||||
/// value is associated to a fast field, only the last one is kept.
|
/// fields given a document id.
|
||||||
/// 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.
|
|
||||||
///
|
///
|
||||||
/// Returns the associated field handle.
|
/// Returns the associated field handle.
|
||||||
/// Raises a ValueError if there was an error with the field creation.
|
/// Raises a ValueError if there was an error with the field creation.
|
||||||
|
@ -196,6 +201,43 @@ impl SchemaBuilder {
|
||||||
Ok(self.clone())
|
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.
|
/// Add a new date field to the schema.
|
||||||
///
|
///
|
||||||
/// Args:
|
/// Args:
|
||||||
|
@ -204,15 +246,10 @@ impl SchemaBuilder {
|
||||||
/// content of the field can be later restored from a Searcher.
|
/// content of the field can be later restored from a Searcher.
|
||||||
/// Defaults to False.
|
/// Defaults to False.
|
||||||
/// indexed (bool, optional): If true sets the field to be indexed.
|
/// indexed (bool, optional): If true sets the field to be indexed.
|
||||||
/// fast (str, optional): Set the u64 options as a single-valued fast
|
/// fast (str, optional): Set the date options as a fast field. A fast
|
||||||
/// field. Fast fields are designed for random access. Access time
|
/// field is a column-oriented fashion storage for tantivy. It is
|
||||||
/// are similar to a random lookup in an array. If more than one
|
/// designed for the fast random access of some document fields
|
||||||
/// value is associated to a fast field, only the last one is kept.
|
/// given a document id.
|
||||||
/// 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.
|
|
||||||
///
|
///
|
||||||
/// Returns the associated field handle.
|
/// Returns the associated field handle.
|
||||||
/// Raises a ValueError if there was an error with the field creation.
|
/// Raises a ValueError if there was an error with the field creation.
|
||||||
|
|
|
@ -20,6 +20,7 @@ def schema_numeric_fields():
|
||||||
SchemaBuilder()
|
SchemaBuilder()
|
||||||
.add_integer_field("id", stored=True, indexed=True)
|
.add_integer_field("id", stored=True, indexed=True)
|
||||||
.add_float_field("rating", 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)
|
.add_text_field("body", stored=True)
|
||||||
.build()
|
.build()
|
||||||
)
|
)
|
||||||
|
@ -86,6 +87,7 @@ def create_index_with_numeric_fields(dir=None):
|
||||||
doc = Document()
|
doc = Document()
|
||||||
doc.add_integer("id", 1)
|
doc.add_integer("id", 1)
|
||||||
doc.add_float("rating", 3.5)
|
doc.add_float("rating", 3.5)
|
||||||
|
doc.add_boolean("is_good", True)
|
||||||
doc.add_text(
|
doc.add_text(
|
||||||
"body",
|
"body",
|
||||||
(
|
(
|
||||||
|
@ -99,6 +101,7 @@ def create_index_with_numeric_fields(dir=None):
|
||||||
{
|
{
|
||||||
"id": 2,
|
"id": 2,
|
||||||
"rating": 4.5,
|
"rating": 4.5,
|
||||||
|
"is_good": False,
|
||||||
"body": (
|
"body": (
|
||||||
"A few miles south of Soledad, the Salinas River drops "
|
"A few miles south of Soledad, the Salinas River drops "
|
||||||
"in close to the hillside bank and runs deep and "
|
"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 "
|
"sycamores with mottled, white, recumbent limbs and "
|
||||||
"branches that arch over the pool"
|
"branches that arch over the pool"
|
||||||
),
|
),
|
||||||
}
|
},
|
||||||
)
|
)
|
||||||
writer.add_document(doc)
|
writer.add_document(doc)
|
||||||
writer.commit()
|
writer.commit()
|
||||||
|
|
Loading…
Reference in New Issue