Table of Contents

Class IndexWriter

Namespace
TantivyDotNet
Assembly
TantivyDotNet.dll

Adds and deletes documents. Changes become visible to new searchers only after Commit().

public sealed class IndexWriter : IDisposable
Inheritance
IndexWriter
Implements
Inherited Members

Remarks

Not thread-safe. Use a writer from one thread at a time. (Tantivy already parallelizes indexing internally across threadCount threads.) Concurrent calls won't corrupt memory, because the native side serializes them, but the order of operations and commits is then undefined.

Dispose() does not commit: anything added or deleted since the last Commit() is discarded. Only one writer may exist per index at a time.

Methods

AddDocument(object)

Serializes document (for example an anonymous object) with reflection-based System.Text.Json and adds it. Property names must match the schema's field names exactly. For trimmed or NativeAOT apps, use AddDocument<T>(T, JsonTypeInfo<T>) instead.

AddDocument(ReadOnlySpan<byte>)

Adds a document given as a JSON object mapping field names to a value or an array of values, for example {"title": "Of Mice and Men", "year": 1937}. A property that isn't a schema field is an error unless the writer was created with IgnoreUnknownFields.

AddDocument(string)

Adds a document given as a JSON object mapping field names to a value or an array of values, for example {"title": "Of Mice and Men", "year": 1937}. A property that isn't a schema field is an error unless the writer was created with IgnoreUnknownFields.

AddDocument<T>(T, JsonTypeInfo<T>)

Serializes document with System.Text.Json source-generated metadata and adds it. This overload is trimming- and NativeAOT-safe. JSON property names must match the schema's field names.

Commit()

Makes all changes since the last commit durable and visible to new searchers.

DeleteAllDocuments()

Deletes all documents (takes effect on Commit()).

DeleteDocuments(Query)

Deletes every document matching query, among those added before this call (documents added later aren't affected). Takes effect on Commit().

DeleteTerm(string, long)

Deletes every document whose indexed i64 field field equals value.

DeleteTerm(string, string)

Deletes every document whose text field field contains the term value. The value isn't tokenized: for a field using the default tokenizer, pass a single lowercase word. To delete by ID, use a field added with AddStringField(string, bool, bool).

DeleteTerm(string, ulong)

Deletes every document whose indexed u64 field field equals value.

Dispose()

Releases the writer and its index lock without committing. Safe to call more than once.

ForceMerge(int)

Merges the committed segments until at most maxSegments remain, and rewrites segments that contain deleted documents to drop them. Blocks until done. Typically called once after building an index that is then only read: fewer segments make searches faster.

GarbageCollectFiles()

Deletes the index files that neither the index nor an open searcher uses anymore, such as segments replaced by a merge. Tantivy also does this after commits and merges, so this is rarely needed; it's useful to reclaim disk space right after disposing old searchers.

Rollback()

Discards all changes since the last commit.

UpdateDocument(TermQuery, object)

Replaces the documents containing term with a new document given as JSON: deletes them and adds the new document in one operation, so a commit includes both or neither. Typically term is an ID: Query.Term("id", "b42").

UpdateDocument(TermQuery, ReadOnlySpan<byte>)

Replaces the documents containing term with a new document given as JSON: deletes them and adds the new document in one operation, so a commit includes both or neither. Typically term is an ID: Query.Term("id", "b42").

UpdateDocument(TermQuery, string)

Replaces the documents containing term with a new document given as JSON: deletes them and adds the new document in one operation, so a commit includes both or neither. Typically term is an ID: Query.Term("id", "b42").

UpdateDocument<T>(TermQuery, T, JsonTypeInfo<T>)

Replaces the documents containing term with a new document given as JSON: deletes them and adds the new document in one operation, so a commit includes both or neither. Typically term is an ID: Query.Term("id", "b42").