Add box_clone() and downcast::Any to Query (#303)

This commit is contained in:
Jason Wolfe
2018-05-18 09:53:11 +09:00
committed by Paul Masurel
parent c9459f74e8
commit 72acad0921
6 changed files with 49 additions and 5 deletions

View File

@@ -9,7 +9,7 @@ use Score;
/// Query that matches all of the documents.
///
/// All of the document get the score 1f32.
#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct AllQuery;
impl Query for AllQuery {

View File

@@ -23,6 +23,16 @@ pub struct BooleanQuery {
subqueries: Vec<(Occur, Box<Query>)>,
}
impl Clone for BooleanQuery {
fn clone(&self) -> Self {
self.subqueries
.iter()
.map(|(x, y)| (x.clone(), y.box_clone()))
.collect::<Vec<_>>()
.into()
}
}
impl From<Vec<(Occur, Box<Query>)>> for BooleanQuery {
fn from(subqueries: Vec<(Occur, Box<Query>)>) -> BooleanQuery {
BooleanQuery { subqueries }
@@ -55,4 +65,8 @@ impl BooleanQuery {
.collect();
BooleanQuery::from(occur_term_queries)
}
pub fn clauses(&self) -> &[(Occur, Box<Query>)] {
&self.subqueries[..]
}
}

View File

@@ -21,7 +21,7 @@ use Result;
/// Using a `PhraseQuery` on a field requires positions
/// to be indexed for this field.
///
#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct PhraseQuery {
field: Field,
phrase_terms: Vec<Term>,
@@ -47,6 +47,14 @@ impl PhraseQuery {
phrase_terms: terms,
}
}
pub fn field(&self) -> Field {
self.field
}
pub fn phrase_terms(&self) -> &[Term] {
&self.phrase_terms[..]
}
}
impl Query for PhraseQuery {

View File

@@ -1,6 +1,7 @@
use super::Weight;
use collector::Collector;
use core::searcher::Searcher;
use downcast;
use std::fmt;
use Result;
use SegmentLocalId;
@@ -38,7 +39,7 @@ use SegmentLocalId;
///
/// When implementing a new type of `Query`, it is normal to implement a
/// dedicated `Query`, `Weight` and `Scorer`.
pub trait Query: fmt::Debug {
pub trait Query: QueryClone + downcast::Any + fmt::Debug {
/// Create the weight associated to a query.
///
/// If scoring is not required, setting `scoring_enabled` to `false`
@@ -77,3 +78,20 @@ pub trait Query: fmt::Debug {
Ok(())
}
}
pub trait QueryClone {
fn box_clone(&self) -> Box<Query>;
}
impl<T> QueryClone for T
where T: 'static + Query + Clone
{
fn box_clone(&self) -> Box<Query> {
Box::new(self.clone())
}
}
#[allow(missing_docs)]
mod downcast_impl {
downcast!(super::Query);
}

View File

@@ -80,7 +80,7 @@ fn map_bound<TFrom, Transform: Fn(TFrom) -> Vec<u8>>(
/// # run().unwrap()
/// # }
/// ```
#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct RangeQuery {
field: Field,
value_type: Type,

View File

@@ -16,7 +16,7 @@ use Term;
/// * `idf` - inverse document frequency.
/// * `term_freq` - number of occurrences of the term in the field
/// * `field norm` - number of tokens in the field.
#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct TermQuery {
term: Term,
index_record_option: IndexRecordOption,
@@ -31,6 +31,10 @@ impl TermQuery {
}
}
pub fn term(&self) -> &Term {
&self.term
}
/// Returns a weight object.
///
/// While `.weight(...)` returns a boxed trait object,