Swap `visit(score, doc)` with `visit(doc, score)`
Aha! I made it backwards to make it easier to output consistently. The consistency part makes sense, but driving a container with score before the item being contained was too confusing.
- Id
- 2f0ab387b00c3a299a2d0c25ca13d20eb49dbc0d
- Author
- Caio
- Commit time
- 2020-02-15T18:09:09+01:00
Modified tique/src/topterms.rs
while let Some((bytes, terminfo)) = termstream.next() {
if let Ok(text) = str::from_utf8(bytes) {
- let term = Term::from_field_text(field, text);
-
let mut postings =
inverted_index.read_postings_from_terminfo(terminfo, IndexRecordOption::WithFreqs);
if postings.skip_next(doc_id) == SkipResult::Reached {
+ let term = Term::from_field_text(field, text);
consumer(term, postings.term_freq());
}
}
}
pub fn into_sorted_vec(self) -> Vec<(Term, f32)> {
- self.0
- .into_sorted_vec()
- .into_iter()
- // FIXME What was I thinking when I made the whole topk api backwards?
- .map(|(score, term)| (term, score))
- .collect()
+ self.0.into_sorted_vec()
}
pub fn into_query(self) -> BooleanQuery {
self.0
.into_vec()
.into_iter()
- .map(|(_score, term)| term)
+ .map(|(term, _score)| term)
.collect(),
)
}
fn visit(&mut self, term: Term, score: f32) {
- self.0.visit(score, term);
+ self.0.visit(term, score);
}
// TODO into_boosted_query, using the scaled tf/idf scores scaled with
Modified tique/src/conditional_collector/top_collector.rs
.check(self.segment_id, doc, score, K::ASCENDING)
{
self.visited += 1;
- self.topk.visit(score, doc);
+ self.topk.visit(doc, score);
}
}
.topk
.into_vec()
.into_iter()
- .map(|(score, doc)| (score, DocAddress(segment_id, doc)))
+ .map(|(doc, score)| (score, DocAddress(segment_id, doc)))
.collect();
CollectionResult {
visited += item.visited;
for (score, doc) in item.items {
- topk.visit(score, doc);
+ topk.visit(doc, score);
}
}
CollectionResult {
total,
visited,
- items: topk.into_sorted_vec(),
+ items: topk
+ .into_sorted_vec()
+ .into_iter()
+ .map(|(doc, score)| (score, doc))
+ .collect(),
}
}
}
collector.collect(id, score);
}
- assert_eq!(wanted, collector.into_topk().into_sorted_vec());
+ assert_eq!(
+ wanted,
+ collector
+ .into_topk()
+ .into_sorted_vec()
+ .into_iter()
+ .map(|(id, score)| (score, id))
+ .collect::<Vec<_>>()
+ );
}
#[test]
Modified tique/src/conditional_collector/topk.rs
pub trait TopK<T, D> {
const ASCENDING: bool;
- fn visit(&mut self, score: T, doc: D);
- fn into_sorted_vec(self) -> Vec<(T, D)>;
- fn into_vec(self) -> Vec<(T, D)>;
+ fn visit(&mut self, doc: D, score: T);
+ fn into_sorted_vec(self) -> Vec<(D, T)>;
+ fn into_vec(self) -> Vec<(D, T)>;
}
pub trait TopKProvider<T: PartialOrd, D: Ord> {
}
}
- fn visit(&mut self, score: T, doc: D) {
+ fn visit(&mut self, doc: D, score: T) {
let scored = Scored {
score,
doc: Reverse(doc),
}
}
- fn into_sorted_vec(self) -> Vec<(T, D)> {
+ fn into_sorted_vec(self) -> Vec<(D, T)> {
self.heap
.into_sorted_vec()
.into_iter()
- .map(|s| (s.score, s.doc.0))
+ .map(|s| (s.doc.0, s.score))
.collect()
}
- fn into_vec(self) -> Vec<(T, D)> {
+ fn into_vec(self) -> Vec<(D, T)> {
self.heap
.into_vec()
.into_iter()
- .map(|s| (s.score, s.doc.0))
+ .map(|s| (s.doc.0, s.score))
.collect()
}
}
}
}
- fn visit(&mut self, score: T, doc: D) {
+ fn visit(&mut self, doc: D, score: T) {
let scored = Reverse(Scored { score, doc });
if self.heap.len() < self.limit {
self.heap.push(scored);
}
}
- fn into_sorted_vec(self) -> Vec<(T, D)> {
+ fn into_sorted_vec(self) -> Vec<(D, T)> {
self.heap
.into_sorted_vec()
.into_iter()
- .map(|s| (s.0.score, s.0.doc))
+ .map(|s| (s.0.doc, s.0.score))
.collect()
}
- fn into_vec(self) -> Vec<(T, D)> {
+ fn into_vec(self) -> Vec<(D, T)> {
self.heap
.into_vec()
.into_iter()
- .map(|s| (s.0.score, s.0.doc))
+ .map(|s| (s.0.doc, s.0.score))
.collect()
}
}
impl<T: PartialOrd, D: Ord> TopK<T, D> for AscendingTopK<T, D> {
const ASCENDING: bool = true;
- fn visit(&mut self, score: T, doc: D) {
- AscendingTopK::visit(self, score, doc);
+ fn visit(&mut self, doc: D, score: T) {
+ AscendingTopK::visit(self, doc, score);
}
- fn into_sorted_vec(self) -> Vec<(T, D)> {
+ fn into_sorted_vec(self) -> Vec<(D, T)> {
AscendingTopK::into_sorted_vec(self)
}
- fn into_vec(self) -> Vec<(T, D)> {
+ fn into_vec(self) -> Vec<(D, T)> {
AscendingTopK::into_vec(self)
}
}
impl<T: PartialOrd, D: Ord> TopK<T, D> for DescendingTopK<T, D> {
const ASCENDING: bool = false;
- fn visit(&mut self, score: T, doc: D) {
- DescendingTopK::visit(self, score, doc);
+ fn visit(&mut self, doc: D, score: T) {
+ DescendingTopK::visit(self, doc, score);
}
- fn into_sorted_vec(self) -> Vec<(T, D)> {
+ fn into_sorted_vec(self) -> Vec<(D, T)> {
DescendingTopK::into_sorted_vec(self)
}
- fn into_vec(self) -> Vec<(T, D)> {
+ fn into_vec(self) -> Vec<(D, T)> {
DescendingTopK::into_vec(self)
}
}
K: TopK<S, D>,
{
for (score, id) in input {
- topk.visit(score, id);
+ topk.visit(id, score);
}
- assert_eq!(wanted, topk.into_sorted_vec());
+ assert_eq!(
+ wanted,
+ topk.into_sorted_vec()
+ .into_iter()
+ .map(|(doc, score)| (score, doc))
+ .collect::<Vec<_>>()
+ );
}
#[test]