Skip to content

Commit 38332cf

Browse files
authored
Added float fields to schema with tests (quickwit-oss#36)
* Added float fields to schema with tests * Fixed typo
1 parent 5a913b9 commit 38332cf

File tree

3 files changed

+109
-5
lines changed

3 files changed

+109
-5
lines changed

src/document.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,15 @@ impl Document {
292292
add_value(self, field_name, value);
293293
}
294294

295+
/// Add a float value to the document.
296+
///
297+
/// Args:
298+
/// field_name (str): The field name for which we are adding the value.
299+
/// value (f64): The float that will be added to the document.
300+
fn add_float(&mut self, field_name: String, value: f64) {
301+
add_value(self, field_name, value);
302+
}
303+
295304
/// Add a date value to the document.
296305
///
297306
/// Args:

src/schemabuilder.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl SchemaBuilder {
119119
) -> PyResult<Self> {
120120
let builder = &mut self.builder;
121121

122-
let opts = SchemaBuilder::build_int_option(stored, indexed, fast)?;
122+
let opts = SchemaBuilder::build_numeric_option(stored, indexed, fast)?;
123123

124124
if let Some(builder) = builder.write().unwrap().as_mut() {
125125
builder.add_i64_field(name, opts);
@@ -131,6 +131,28 @@ impl SchemaBuilder {
131131
Ok(self.clone())
132132
}
133133

134+
#[args(stored = false, indexed = false)]
135+
fn add_float_field(
136+
&mut self,
137+
name: &str,
138+
stored: bool,
139+
indexed: bool,
140+
fast: Option<&str>,
141+
) -> PyResult<Self> {
142+
let builder = &mut self.builder;
143+
144+
let opts = SchemaBuilder::build_numeric_option(stored, indexed, fast)?;
145+
146+
if let Some(builder) = builder.write().unwrap().as_mut() {
147+
builder.add_f64_field(name, opts);
148+
} else {
149+
return Err(exceptions::PyValueError::new_err(
150+
"Schema builder object isn't valid anymore.",
151+
));
152+
}
153+
Ok(self.clone())
154+
}
155+
134156
/// Add a new unsigned integer field to the schema.
135157
///
136158
/// Args:
@@ -161,7 +183,7 @@ impl SchemaBuilder {
161183
) -> PyResult<Self> {
162184
let builder = &mut self.builder;
163185

164-
let opts = SchemaBuilder::build_int_option(stored, indexed, fast)?;
186+
let opts = SchemaBuilder::build_numeric_option(stored, indexed, fast)?;
165187

166188
if let Some(builder) = builder.write().unwrap().as_mut() {
167189
builder.add_u64_field(name, opts);
@@ -203,7 +225,7 @@ impl SchemaBuilder {
203225
) -> PyResult<Self> {
204226
let builder = &mut self.builder;
205227

206-
let opts = SchemaBuilder::build_int_option(stored, indexed, fast)?;
228+
let opts = SchemaBuilder::build_numeric_option(stored, indexed, fast)?;
207229

208230
if let Some(builder) = builder.write().unwrap().as_mut() {
209231
builder.add_date_field(name, opts);
@@ -319,7 +341,7 @@ impl SchemaBuilder {
319341
}
320342

321343
impl SchemaBuilder {
322-
fn build_int_option(
344+
fn build_numeric_option(
323345
stored: bool,
324346
indexed: bool,
325347
fast: Option<&str>,

tests/tantivy_test.py

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ def schema():
1212
.build()
1313
)
1414

15+
def schema_numeric_fields():
16+
return (
17+
SchemaBuilder()
18+
.add_integer_field("id", stored=True, indexed=True)
19+
.add_float_field("rating", stored=True, indexed=True)
20+
.add_text_field("body", stored=True)
21+
.build()
22+
)
1523

1624
def create_index(dir=None):
1725
# assume all tests will use the same documents for now
@@ -66,6 +74,46 @@ def create_index(dir=None):
6674
index.reload()
6775
return index
6876

77+
def create_index_with_numeric_fields(dir=None):
78+
index = Index(schema_numeric_fields(), dir)
79+
writer = index.writer()
80+
81+
doc = Document()
82+
doc.add_integer("id", 1)
83+
doc.add_float("rating", 3.5)
84+
doc.add_text(
85+
"body",
86+
(
87+
"He was an old man who fished alone in a skiff in"
88+
"the Gulf Stream and he had gone eighty-four days "
89+
"now without taking a fish."
90+
),
91+
)
92+
writer.add_document(doc)
93+
doc = Document.from_dict(
94+
{
95+
"id": 2,
96+
"rating": 4.5,
97+
"body": (
98+
"A few miles south of Soledad, the Salinas River drops "
99+
"in close to the hillside bank and runs deep and "
100+
"green. The water is warm too, for it has slipped "
101+
"twinkling over the yellow sands in the sunlight "
102+
"before reaching the narrow pool. On one side of the "
103+
"river the golden foothill slopes curve up to the "
104+
"strong and rocky Gabilan Mountains, but on the valley "
105+
"side the water is lined with trees—willows fresh and "
106+
"green with every spring, carrying in their lower leaf "
107+
"junctures the debris of the winter’s flooding; and "
108+
"sycamores with mottled, white, recumbent limbs and "
109+
"branches that arch over the pool"
110+
),
111+
}
112+
)
113+
writer.add_document(doc)
114+
writer.commit()
115+
index.reload()
116+
return index
69117

70118
@pytest.fixture()
71119
def dir_index(tmpdir):
@@ -77,6 +125,11 @@ def ram_index():
77125
return create_index()
78126

79127

128+
@pytest.fixture(scope="class")
129+
def ram_index_numeric_fields():
130+
return create_index_with_numeric_fields()
131+
132+
80133
class TestClass(object):
81134
def test_simple_search_in_dir(self, dir_index):
82135
_, index = dir_index
@@ -120,6 +173,25 @@ def test_and_query(self, ram_index):
120173

121174
assert len(result.hits) == 1
122175

176+
def test_and_query_numeric_fields(self, ram_index_numeric_fields):
177+
index = ram_index_numeric_fields
178+
searcher = index.searcher()
179+
180+
# 1 result
181+
float_query = index.parse_query("3.5", ["rating"])
182+
result = searcher.search(float_query)
183+
assert len(result.hits) == 1
184+
assert searcher.doc(result.hits[0][1])['rating'][0] == 3.5
185+
186+
integer_query = index.parse_query("1", ["id"])
187+
result = searcher.search(integer_query)
188+
assert len(result.hits) == 1
189+
190+
# 0 result
191+
integer_query = index.parse_query("10", ["id"])
192+
result = searcher.search(integer_query)
193+
assert len(result.hits) == 0
194+
123195
def test_and_query_parser_default_fields(self, ram_index):
124196
query = ram_index.parse_query("winter", default_field_names=["title"])
125197
assert repr(query) == """Query(TermQuery(Term(type=Str, field=0, "winter")))"""
@@ -279,8 +351,9 @@ def test_create_readers(self):
279351

280352

281353
class TestSearcher(object):
282-
def test_searcher_repr(self, ram_index):
354+
def test_searcher_repr(self, ram_index, ram_index_numeric_fields):
283355
assert repr(ram_index.searcher()) == "Searcher(num_docs=3, num_segments=1)"
356+
assert repr(ram_index_numeric_fields.searcher()) == "Searcher(num_docs=2, num_segments=1)"
284357

285358

286359
class TestDocument(object):

0 commit comments

Comments
 (0)