1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use query::Query;
use query::Weight;
use query::Scorer;
use core::SegmentReader;
use Result;
use DocSet;
use Score;
use DocId;
use std::any::Any;
use core::Searcher;

/// Query that matches all of the documents.
///
/// All of the document get the score 1f32.
#[derive(Debug)]
pub struct AllQuery;

impl Query for AllQuery {
    fn as_any(&self) -> &Any {
        self
    }

    fn weight(&self, _: &Searcher) -> Result<Box<Weight>> {
        Ok(box AllWeight)
    }
}

/// Weight associated to the `AllQuery` query.
pub struct AllWeight;

impl Weight for AllWeight {
    fn scorer<'a>(&'a self, reader: &'a SegmentReader) -> Result<Box<Scorer + 'a>> {
        Ok(box AllScorer {
            started: false,
            doc: 0u32,
            max_doc: reader.max_doc(),
        })
    }
}

/// Scorer associated to the `AllQuery` query.
pub struct AllScorer {
    started: bool,
    doc: DocId,
    max_doc: DocId,
}

impl DocSet for AllScorer {
    fn advance(&mut self) -> bool {
        if self.started {
            self.doc += 1u32;
        } else {
            self.started = true;
        }
        self.doc < self.max_doc
    }

    fn doc(&self) -> DocId {
        self.doc
    }

    fn size_hint(&self) -> u32 {
        self.max_doc
    }
}

impl Scorer for AllScorer {
    fn score(&self) -> Score {
        1f32
    }
}