Class Query
- Namespace
- TantivyDotNet
- Assembly
- TantivyDotNet.dll
A query built in code, passed to Search(Query, SearchOptions). Create one with
the static factories (Term(string, string), Phrase(string, string, int), Fuzzy(string, string, int, bool, bool),
Boolean(IEnumerable<Query>?, IEnumerable<Query>?, IEnumerable<Query>?), Range(string, long?, long?, bool, bool), Parse(string, QueryParserOptions), ...)
and combine them with &, | and !.
public abstract class Query
- Inheritance
-
Query
- Derived
- Inherited Members
Examples
var q = Query.Phrase("body", "of mice") & Query.Range("year", 1930, 1940) & !Query.Term("id", "b4");
var hits = searcher.Search(q);
Remarks
Queries are immutable, hold no native resources, and can be reused across searches, searchers and threads. Field names and value types are checked when the query runs; a mismatch throws TantivyException with InvalidArgument.
Properties
- All
Matches every document, all with the same score.
Methods
- Boolean(IEnumerable<Query>?, IEnumerable<Query>?, IEnumerable<Query>?)
Combines clauses, like
+a b -cin the query language.
- Boost(float)
Multiplies this query's scores by
factor, to weight it within a Boolean(IEnumerable<Query>?, IEnumerable<Query>?, IEnumerable<Query>?).
- ConstantScore(float)
Gives every document this query matches the same
score, for filters that shouldn't affect ranking, or to rank by clause rather than by term statistics.
- DisjunctionMax(IEnumerable<Query>, float)
Matches documents matching any of
queries, scored by the best-scoring one plustieBreakertimes the scores of the others. Use it to search the same words in several fields without rewarding documents that repeat them everywhere.
- Exists(string)
Matches documents that have at least one value in
field, which must be a fast field (fast: true). All matches score the same.
- Facet(string, string)
Matches documents in the facet category
pathor any category below it:Facet("genre", "/fiction")matches/fictionand/fiction/classic.
- Fuzzy(string, string, int, bool, bool)
Matches terms of a text
fieldwithindistanceedits (insertions, deletions, substitutions) ofvalue.
- MoreLikeThis(IReadOnlyDictionary<string, string>, MoreLikeThisOptions?)
Matches documents similar to the given text per field, for example a draft that isn't indexed:
MoreLikeThis(new Dictionary<string, string> { ["body"] = draft }).
- MoreLikeThis(Document, MoreLikeThisOptions?)
Matches documents similar to
document: its most distinctive terms (by TF-IDF, in text, numeric, date and facet fields) become a query. Typicallyhit.Document, which holds only stored fields. Exclude the document itself with& !Query.Term("id", ...).
- Parse(string, IEnumerable<string>?)
A query in Tantivy's query language, for example
body:mice AND year:[1930 TO 1940]. Combine it with built queries like any other query.
- Parse(string, QueryParserOptions)
A query in Tantivy's query language, parsed with
options: default fields and operator, per-field boosts and fuzziness, and lenient parsing for user input.
- Phrase(string, string, int)
Matches documents where the words of
textappear in order infield. The text is analyzed with the field's tokenizer.
- Range(string, IPAddress?, IPAddress?, bool, bool)
Matches documents whose
fieldvalue lies between the bounds. A null bound is open; at least one must be set. The field must be indexed or fast (fast is quicker).
- Range(string, DateTimeOffset?, DateTimeOffset?, bool, bool)
Matches documents whose
fieldvalue lies between the bounds. A null bound is open; at least one must be set. The field must be indexed or fast (fast is quicker).
- Range(string, DateTime?, DateTime?, bool, bool)
Matches documents whose
fieldvalue lies between the bounds. A null bound is open; at least one must be set. The field must be indexed or fast (fast is quicker).
- Range(string, double?, double?, bool, bool)
Matches documents whose
fieldvalue lies between the bounds. A null bound is open; at least one must be set. The field must be indexed or fast (fast is quicker).
- Range(string, long?, long?, bool, bool)
Matches documents whose
fieldvalue lies between the bounds. A null bound is open; at least one must be set. The field must be indexed or fast (fast is quicker).
- Range(string, ulong?, ulong?, bool, bool)
Matches documents whose
fieldvalue lies between the bounds. A null bound is open; at least one must be set. The field must be indexed or fast (fast is quicker).
- Range(string, string?, string?, bool, bool)
Matches terms of a text
fieldthat sort between the bounds (ordinal, byte-wise). Mostly useful on string fields. A null bound is open.
- Regex(string, string)
Matches documents with a term of the text
fieldthat matchespatternas a whole (Rustregexsyntax, anchored at both ends):Regex("title", "gr.*s")matches the termgrapes. Terms are the indexed, analyzed tokens (lowercase with the default tokenizer).
- Term(string, bool)
Matches documents whose
fieldcontains exactlyvalue. For a JSON field,fieldis a path such asattrs.color.
- Term(string, DateTime)
Matches documents whose date
fieldequalsvalue, at the index's date precision (seconds by default).
- Term(string, DateTimeOffset)
Matches documents whose date
fieldequalsvalue, at the index's date precision (seconds by default).
- Term(string, double)
Matches documents whose
fieldcontains exactlyvalue. For a JSON field,fieldis a path such asattrs.color.
- Term(string, long)
Matches documents whose
fieldcontains exactlyvalue. Also works on u64 fields (for non-negative values) and f64 fields.
- Term(string, IPAddress)
Matches documents whose IP address
fieldequalsvalue(IPv4 also matches its IPv4-mapped IPv6 form).
- Term(string, ReadOnlySpan<byte>)
Matches documents whose
fieldcontains exactlyvalue. For a JSON field,fieldis a path such asattrs.color.
- Term(string, string)
Matches documents whose
fieldcontains exactlyvalue. For a JSON field,fieldis a path such asattrs.color.
- Term(string, ulong)
Matches documents whose
fieldcontains exactlyvalue. For a JSON field,fieldis a path such asattrs.color.
- TermSet(string, IEnumerable<long>)
Matches documents whose
fieldcontains any ofvaluesexactly. Faster than an OR of many Term(string, string) queries; all matches score the same. Values aren't analyzed.
- TermSet(string, IEnumerable<IPAddress>)
Matches documents whose
fieldcontains any ofvaluesexactly. Faster than an OR of many Term(string, string) queries; all matches score the same. Values aren't analyzed.
- TermSet(string, IEnumerable<string>)
Matches documents whose
fieldcontains any ofvaluesexactly. Faster than an OR of many Term(string, string) queries; all matches score the same. Values aren't analyzed.
- TermSet(string, IEnumerable<ulong>)
Matches documents whose
fieldcontains any ofvaluesexactly. Faster than an OR of many Term(string, string) queries; all matches score the same. Values aren't analyzed.
- ToString()
A diagnostic description of the query tree. The format may change between versions.
Operators
- operator &(Query, Query)
Both queries must match. Nested
&chains are flattened into one BooleanQuery.
- operator |(Query, Query)
Either query must match. Nested
|chains are flattened into one BooleanQuery.
- operator !(Query)
Every document the query does not match.
a & !bmeans "a but not b".