Struct tantivy::query::QueryParser
source · [−]pub struct QueryParser { /* private fields */ }
Expand description
Tantivy’s Query parser
The language covered by the current parser is extremely simple.
-
simple terms: “e.g.:
Barack Obama
are simply tokenized using tantivy’sSimpleTokenizer
, hence becoming["barack", "obama"]
. The terms are then searched within the default terms of the query parser.e.g. If
body
andtitle
are default fields, our example terms are["title:barack", "body:barack", "title:obama", "body:obama"]
. By default, all tokenized and indexed fields are default fields.Multiple terms are handled as an
OR
: any document containing at least one of the term will go through the scoring.This behavior is slower, but is not a bad idea if the user is sorting by relevance : The user typically just scans through the first few documents in order of decreasing relevance and will stop when the documents are not relevant anymore.
Switching to a default of
AND
can be done by calling.set_conjunction_by_default()
. -
boolean operators
AND
,OR
.AND
takes precedence overOR
, so thata AND b OR c
is interpreted as(a AND b) OR c
. -
In addition to the boolean operators, the
-
,+
can help define. These operators are sufficient to express all queries using boolean operators. For instancex AND y OR z
can be written ((+x +y) z
). In addition, these operators can help define “required optional” queries.(+x y)
matches the same document set as simplyx
, buty
will help refining the score. -
negative terms: By prepending a term by a
-
, a term can be excluded from the search. This is useful for disambiguating a query. e.g.apple -fruit
-
must terms: By prepending a term by a
+
, a term can be made required for the search. -
phrase terms: Quoted terms become phrase searches on fields that have positions indexed. e.g.,
title:"Barack Obama"
will only find documents that have “barack” immediately followed by “obama”. -
range terms: Range searches can be done by specifying the start and end bound. These can be inclusive or exclusive. e.g.,
title:[a TO c}
will find all documents whose title contains a word lexicographically betweena
andc
(inclusive lower bound, exclusive upper bound). Inclusive bounds are[]
, exclusive are{}
. -
date values: The query parser supports rfc3339 formatted dates. For example
"2002-10-02T15:00:00.05Z"
orsome_date_field:[2002-10-02T15:00:00Z TO 2002-10-02T18:00:00Z}
-
all docs query: A plain
*
will match all documents in the index.
Parts of the queries can be boosted by appending ^boostfactor
.
For instance, "SRE"^2.0 OR devops^0.4
will boost documents containing SRE
instead of
devops. Negative boosts are not allowed.
It is also possible to define a boost for a some specific field, at the query parser level.
(See set_boost(...)
). Typically you may want to boost a title
field.
Implementations
sourceimpl QueryParser
impl QueryParser
sourcepub fn new(
schema: Schema,
default_fields: Vec<Field>,
tokenizer_manager: TokenizerManager
) -> QueryParser
pub fn new(
schema: Schema,
default_fields: Vec<Field>,
tokenizer_manager: TokenizerManager
) -> QueryParser
Creates a QueryParser
, given
- schema - index Schema
- default_fields - fields used to search if no field is specifically defined in the query.
sourcepub fn for_index(index: &Index, default_fields: Vec<Field>) -> QueryParser
pub fn for_index(index: &Index, default_fields: Vec<Field>) -> QueryParser
Creates a QueryParser
, given
- an index
- a set of default - fields used to search if no field is specifically defined in the query.
sourcepub fn set_conjunction_by_default(&mut self)
pub fn set_conjunction_by_default(&mut self)
Set the default way to compose queries to a conjunction.
By default, the query happy tax payer
is equivalent to the query
happy OR tax OR payer
. After calling .set_conjunction_by_default()
happy tax payer
will be interpreted by the parser as happy AND tax AND payer
.
sourcepub fn set_field_boost(&mut self, field: Field, boost: Score)
pub fn set_field_boost(&mut self, field: Field, boost: Score)
Sets a boost for a specific field.
The parse query will automatically boost this field.
If the query defines a query boost through the query language (e.g: country:France^3.0
),
the two boosts (the one defined in the query, and the one defined in the QueryParser
)
are multiplied together.
sourcepub fn parse_query(
&self,
query: &str
) -> Result<Box<dyn Query>, QueryParserError>
pub fn parse_query(
&self,
query: &str
) -> Result<Box<dyn Query>, QueryParserError>
Parse a query
Note that parse_query
returns an error if the input
is not a valid query.
There is currently no lenient mode for the query parser which makes it a bad choice for a public/broad user search engine.
Implementing a lenient mode for this query parser is tracked in Issue 5
Trait Implementations
sourceimpl Clone for QueryParser
impl Clone for QueryParser
sourcefn clone(&self) -> QueryParser
fn clone(&self) -> QueryParser
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
Auto Trait Implementations
impl RefUnwindSafe for QueryParser
impl Send for QueryParser
impl Sync for QueryParser
impl Unpin for QueryParser
impl UnwindSafe for QueryParser
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Downcast for T where
T: Any,
impl<T> Downcast for T where
T: Any,
sourcefn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
Convert Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
. Read more
sourcefn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
Convert Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
. Read more
sourcefn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert &Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s. Read more
sourcefn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert &mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s. Read more
sourceimpl<T> DowncastSync for T where
T: Any + Send + Sync,
impl<T> DowncastSync for T where
T: Any + Send + Sync,
impl<T> Pointable for T
impl<T> Pointable for T
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more