Table of Contents

Class Query

Namespace
TantivyDotNet
Assembly
TantivyDotNet.dll
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 -c in 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 plus tieBreaker times 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 path or any category below it: Facet("genre", "/fiction") matches /fiction and /fiction/classic.

Fuzzy(string, string, int, bool, bool)

Matches terms of a text field within distance edits (insertions, deletions, substitutions) of value.

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. Typically hit.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 text appear in order in field. The text is analyzed with the field's tokenizer.

Range(string, IPAddress?, IPAddress?, bool, bool)

Matches documents whose field value 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 field value 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 field value 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 field value 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 field value 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 field value 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 field that 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 field that matches pattern as a whole (Rust regex syntax, anchored at both ends): Regex("title", "gr.*s") matches the term grapes. Terms are the indexed, analyzed tokens (lowercase with the default tokenizer).

Term(string, bool)

Matches documents whose field contains exactly value. For a JSON field, field is a path such as attrs.color.

Term(string, DateTime)

Matches documents whose date field equals value, at the index's date precision (seconds by default).

Term(string, DateTimeOffset)

Matches documents whose date field equals value, at the index's date precision (seconds by default).

Term(string, double)

Matches documents whose field contains exactly value. For a JSON field, field is a path such as attrs.color.

Term(string, long)

Matches documents whose field contains exactly value. Also works on u64 fields (for non-negative values) and f64 fields.

Term(string, IPAddress)

Matches documents whose IP address field equals value (IPv4 also matches its IPv4-mapped IPv6 form).

Term(string, ReadOnlySpan<byte>)

Matches documents whose field contains exactly value. For a JSON field, field is a path such as attrs.color.

Term(string, string)

Matches documents whose field contains exactly value. For a JSON field, field is a path such as attrs.color.

Term(string, ulong)

Matches documents whose field contains exactly value. For a JSON field, field is a path such as attrs.color.

TermSet(string, IEnumerable<long>)

Matches documents whose field contains any of values exactly. 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 field contains any of values exactly. 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 field contains any of values exactly. 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 field contains any of values exactly. 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 & !b means "a but not b".