caio.co/de/cantine

Add functionality to sort u64 fields as f64

Somewhat of a partial revert of b59efa1da
Id
369ac5b1cdc529b9a0ab4343514d648243889e43
Author
Caio
Commit time
2019-12-12T08:50:52+01:00

Modified crates/tique/src/top_collector/field.rs

@@ -26,7 +26,21

fast_field_custom_score_collector!(ordered_by_i64_fast_field, i64, i64);
fast_field_custom_score_collector!(ordered_by_u64_fast_field, u64, u64);
-fast_field_custom_score_collector!(ordered_by_f64_fast_field, f64, f64);
+
+pub fn ordered_by_f64_from_u64_fast_field<C>(
+ field: Field,
+ limit: usize,
+ condition_factory: C,
+) -> impl Collector<Fruit = CollectionResult<f64>>
+where
+ C: CollectConditionFactory<f64> + Sync,
+{
+ let scorer_for_segment = move |reader: &SegmentReader| {
+ let scorer = reader.fast_fields().u64(field).expect("Not a fast field");
+ move |doc_id| f64::from_bits(scorer.get(doc_id))
+ };
+ CustomScoreTopCollector::new(limit, condition_factory, scorer_for_segment)
+}

#[cfg(test)]
mod tests {
@@ -44,7 +58,8

let u64_field = sb.add_u64_field("u64", FAST);
let i64_field = sb.add_i64_field("i64", FAST);
- let f64_field = sb.add_f64_field("f64", FAST);
+
+ let f64_field = sb.add_u64_field("f64_from_u64", FAST);

let index = Index::create_in_ram(sb.build());
let mut writer = index.writer_with_num_threads(1, 50_000_000)?;
@@ -53,7 +68,9
let mut doc = Document::new();
doc.add_u64(u64_field, a);
doc.add_i64(i64_field, b);
- doc.add_f64(f64_field, c);
+
+ doc.add_u64(f64_field, c.to_bits());
+
writer.add_document(doc);
};

@@ -67,7 +84,7

let u64_collector = ordered_by_u64_fast_field(u64_field, 2, true);
let i64_collector = ordered_by_i64_fast_field(i64_field, 2, true);
- let f64_collector = ordered_by_f64_fast_field(f64_field, 2, true);
+ let f64_collector = ordered_by_f64_from_u64_fast_field(f64_field, 2, true);

let (top_u64, top_i64, top_f64) =
searcher.search(&AllQuery, &(u64_collector, i64_collector, f64_collector))?;

Modified crates/tique/src/top_collector/mod.rs

@@ -8,5 +8,7
ConditionalTopSegmentCollector, SearchMarker,
};
pub use custom_score::CustomScoreTopCollector;
-pub use field::{ordered_by_f64_fast_field, ordered_by_i64_fast_field, ordered_by_u64_fast_field};
+pub use field::{
+ ordered_by_f64_from_u64_fast_field, ordered_by_i64_fast_field, ordered_by_u64_fast_field,
+};
pub use topk::{Scored, TopK};