Skip to content

Commit 5201d05

Browse files
stuhoodmdashti
authored andcommitted
fix: Add support for bool to the fast field TermSet implementation (#70)
Missed in #69. The `TermSet` fast fields implementation cribbed from `RangeQuery`'s fast fields implementation: ... which also has this bug. Will fix upstream.
1 parent 1f12321 commit 5201d05

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

src/query/term_set_query/term_set_query.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,17 @@ impl TermSetQuery {
4545
return Err(crate::TantivyError::SchemaError(error_msg));
4646
}
4747

48-
let supported_for_ff = sorted_terms
49-
.get(0)
50-
.map(|term| match term.typ() {
51-
Type::U64 | Type::I64 | Type::F64 | Type::Bool | Type::Date | Type::IpAddr => {
52-
true
53-
}
54-
Type::Json | Type::Str => {
55-
// Explicitly not supported yet: see `term_set_query_fastfield.rs`.
56-
false
57-
}
58-
_ => false,
59-
})
60-
.unwrap_or(false);
48+
let supported_for_ff = match field_type.value_type() {
49+
Type::U64 | Type::I64 | Type::F64 | Type::Bool | Type::Date | Type::IpAddr => {
50+
// NOTE: Keep in sync with `FastFieldTermSetWeight::scorer`.
51+
true
52+
}
53+
Type::Json | Type::Str => {
54+
// Explicitly not supported yet: see `term_set_query_fastfield.rs`.
55+
false
56+
}
57+
_ => false,
58+
};
6159

6260
if field_type.is_fast() && supported_for_ff {
6361
sub_queries.push((

src/query/term_set_query/term_set_query_fastfield.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ impl Weight for FastFieldTermSetWeight {
102102
let docset = TermSetDocSet::new(ip_addr_column, values);
103103
Ok(Box::new(ConstScorer::new(docset, boost)))
104104
} else {
105-
// Numeric types
105+
// Numeric types.
106+
//
107+
// NOTE: Keep in sync with `TermSetQuery::specialized_weight`.
106108
let mut values = HashSet::new();
107109
for term in &self.terms {
108110
let value = term.value();
@@ -112,11 +114,13 @@ impl Weight for FastFieldTermSetWeight {
112114
val.to_u64()
113115
} else if let Some(val) = value.as_f64() {
114116
val.to_u64()
117+
} else if let Some(val) = value.as_bool() {
118+
val.to_u64()
115119
} else if let Some(val) = value.as_date() {
116120
val.to_u64()
117121
} else {
118122
return Err(crate::TantivyError::InvalidArgument(format!(
119-
"Expected term with u64, i64, f64 or date, but got {:?}",
123+
"Expected term with u64, i64, f64, bool, or date, but got {:?}",
120124
term
121125
)));
122126
};
@@ -129,6 +133,7 @@ impl Weight for FastFieldTermSetWeight {
129133
ColumnType::U64,
130134
ColumnType::I64,
131135
ColumnType::F64,
136+
ColumnType::Bool,
132137
ColumnType::DateTime,
133138
]),
134139
field_name,
@@ -229,6 +234,7 @@ mod tests {
229234
let text_field_fast = schema_builder.add_text_field("text_fast", STRING | FAST);
230235
let u64_field_fast = schema_builder.add_u64_field("u64_fast", FAST | INDEXED);
231236
let ip_field_fast = schema_builder.add_ip_addr_field("ip_fast", FAST | INDEXED);
237+
let bool_field_fast = schema_builder.add_bool_field("bool_fast", FAST | INDEXED);
232238
let schema = schema_builder.build();
233239
let index = Index::create_in_ram(schema);
234240
{
@@ -238,18 +244,21 @@ mod tests {
238244
text_field_fast => "doc1",
239245
u64_field_fast => 1u64,
240246
ip_field_fast => IpAddr::from_str("127.0.0.1").unwrap().into_ipv6_addr(),
247+
bool_field_fast => true,
241248
))?;
242249
index_writer.add_document(doc!(
243250
text_field => "doc2",
244251
text_field_fast => "doc2",
245252
u64_field_fast => 2u64,
246253
ip_field_fast => IpAddr::from_str("127.0.0.2").unwrap().into_ipv6_addr(),
254+
bool_field_fast => false,
247255
))?;
248256
index_writer.add_document(doc!(
249257
text_field => "doc3",
250258
text_field_fast => "doc3",
251259
u64_field_fast => 3u64,
252260
ip_field_fast => IpAddr::from_str("::ffff:127.0.0.3").unwrap().into_ipv6_addr(),
261+
bool_field_fast => true,
253262
))?;
254263
index_writer.commit()?;
255264
}
@@ -310,6 +319,27 @@ mod tests {
310319

311320
let count = searcher.search(&query, &Count)?;
312321
assert_eq!(count, 2);
322+
323+
Ok(())
324+
}
325+
326+
#[test]
327+
pub fn test_term_set_query_fast_field_bool() -> crate::Result<()> {
328+
let index = create_test_index()?;
329+
let reader = index.reader()?;
330+
let searcher = reader.searcher();
331+
let bool_field_fast = index.schema().get_field("bool_fast").unwrap();
332+
333+
let query = FastFieldTermSetQuery::new(vec![Term::from_field_bool(bool_field_fast, true)]);
334+
335+
let count = searcher.search(&query, &Count)?;
336+
assert_eq!(count, 2);
337+
338+
let query = FastFieldTermSetQuery::new(vec![Term::from_field_bool(bool_field_fast, false)]);
339+
340+
let count = searcher.search(&query, &Count)?;
341+
assert_eq!(count, 1);
342+
313343
Ok(())
314344
}
315345

@@ -362,6 +392,10 @@ mod tests {
362392
let count = searcher.search(&query, &Count)?;
363393
assert_eq!(count, 2);
364394

395+
let query = query_parser.parse_query("bool_fast: IN [true]")?;
396+
let count = searcher.search(&query, &Count)?;
397+
assert_eq!(count, 2);
398+
365399
Ok(())
366400
}
367401

0 commit comments

Comments
 (0)