Module tantivy::schema

source ·
Expand description

Schema definition for tantivy’s indices.

§Setting your schema in Tantivy

Tantivy has a very strict schema. The schema defines information about the fields your index contains, that is, for each field:

  • the field name (may contain any characted, can’t start with a - and can’t be empty. Some characters may require escaping when using the query parser).
  • the type of the field (currently text, u64, i64, f64, bool, date, IpAddr, facets, bytes and json are supported)
  • how the field should be indexed / stored.

This very last point is critical as it will enable / disable some of the functionality for your index.

Tantivy’s schema is stored within the meta.json file at the root of your directory.

§Building a schema “programmatically”

§Setting a text field

§Example

use tantivy::schema::*;
let mut schema_builder = Schema::builder();
let title_options = TextOptions::default()
    .set_stored()
    .set_indexing_options(TextFieldIndexing::default()
    .set_tokenizer("default")
    .set_index_option(IndexRecordOption::WithFreqsAndPositions));
schema_builder.add_text_field("title", title_options);
let schema = schema_builder.build();

We can split the problem of generating a search result page into two phases:

  • identifying the list of 10 or so documents to be displayed (Conceptually query -> doc_ids[])
  • for each of these documents, retrieving the information required to generate the search results page. (doc_ids[] -> Document[])

In the first phase, the ability to search for documents by the given field is determined by the IndexRecordOption of our TextOptions.

The effect of each possible setting is described more in detail in TextOptions.

On the other hand setting the field as stored or not determines whether the field should be returned when Searcher::doc() is called.

§Setting a u64, a i64 or a f64 field

§Example

use tantivy::schema::*;
let mut schema_builder = Schema::builder();
let num_stars_options = NumericOptions::default()
    .set_stored()
    .set_indexed();
schema_builder.add_u64_field("num_stars", num_stars_options);
let schema = schema_builder.build();

Just like for Text fields (see above), setting the field as stored defines whether the field will be returned when Searcher::doc() is called, and setting the field as indexed means that we will be able perform queries such as num_stars:10. Note that unlike text fields, numeric fields can only be indexed in one way for the moment.

§Shortcuts

For convenience, it is possible to define your field indexing options by combining different flags using the | operator.

For instance, a schema containing the two fields defined in the example above could be rewritten:

use tantivy::schema::*;
let mut schema_builder = Schema::builder();
schema_builder.add_u64_field("num_stars", INDEXED | STORED);
schema_builder.add_text_field("title", TEXT | STORED);
let schema = schema_builder.build();

§Fast fields

This functionality is somewhat similar to Lucene’s DocValues.

Fields that are indexed as FAST will be stored in a special data structure that will make it possible to access the value given the doc id rapidly. This is useful if the value of the field is required during scoring or collection for instance.

use tantivy::schema::*;
let mut schema_builder = Schema::builder();
schema_builder.add_u64_field("population", STORED | FAST);
schema_builder.add_text_field("zip_code", STRING | FAST);
let schema = schema_builder.build();

Re-exports§

Modules§

  • Document definition for Tantivy to index and store.

Structs§

  • Define how a bytes field should be handled by tantivy.
  • Defines how DateTime field should be handled by tantivy.
  • A Facet represent a point in a given hierarchy.
  • Define how a facet field should be handled by tantivy.
  • Field is represented by an unsigned 32-bit integer type. The schema holds the mapping between field names and Field objects.
  • A FieldEntry represents a field and its configuration. Schema are a collection of FieldEntry
  • FieldValue holds together a Field and its Value.
  • Define how an ip field should be handled by tantivy.
  • The JsonObjectOptions make it possible to configure how a json object field should be indexed and stored.
  • Internal representation of a document used for JSON serialization.
  • Define how an u64, i64, or f64 field should be handled by tantivy.
  • Tantivy has a very strict schema. You need to specify in advance, whether a field is indexed or not, stored or not, and RAM-based or not.
  • Tantivy has a very strict schema. You need to specify in advance whether a field is indexed or not, stored or not, and RAM-based or not.
  • Term represents the value that the token can take. It’s a serialized representation over different types.
  • Configuration defining indexing for a text field.
  • Define how a text field should be handled by tantivy.
  • ValueBytes represents a serialized value. The value can be of any type of Type (e.g. string, u64, f64, bool, date, JSON). The serialized representation matches the lexographical order of the type.

Enums§

  • Precision with which datetimes are truncated when stored in fast fields. This setting is only relevant for fast fields. In the docstore, datetimes are always saved with nanosecond precision.
  • An error enum for facet parser.
  • A FieldType describes the type (text, u64) of a field as well as how it should be handled by tantivy.
  • IndexRecordOption describes an amount information associated with a given indexed field.
  • Type of the value that a field can take.

Constants§

  • Flag to mark the field as coerced.
  • The precision of the indexed date/time values in the inverted index.
  • Flag to mark the field as a fast field (similar to Lucene’s DocValues)
  • Flag to mark the field as indexed. An indexed field is searchable and has a fieldnorm.
  • Separates the json path and the value in a JSON term binary representation.
  • Flag to mark the field as stored. This flag can apply to any kind of field.
  • The field will be untokenized and indexed.
  • The field will be tokenized and indexed.

Traits§

Functions§

  • Validator for a potential field_name. Returns true if the name can be use for a field name.