TantivyEx.IndexWriter (TantivyEx v0.4.1)
View SourceIndexWriter for adding documents to a TantivyEx index.
The IndexWriter is responsible for adding documents to an index and committing changes to make them searchable.
Summary
Functions
Adds a document to the index.
Commits all pending changes to the index.
Deletes all documents in the index.
Deletes all documents matching the given query.
Creates a new IndexWriter for the given index.
Rolls back any pending changes and cancels the current operation.
Types
@type t() :: reference()
Functions
Adds a document to the index.
The document should be a map where keys are field names and values are the field values. The field names should match those defined in the schema.
Parameters
writer: The IndexWriterdocument: A map representing the document to add
Examples
iex> # Assuming writer is already created
iex> document = %{"title" => "Hello World", "body" => "This is a test document"}
iex> :ok = TantivyEx.IndexWriter.add_document(writer, document)
:ok
Commits all pending changes to the index.
After calling commit, all added documents become searchable. This operation flushes the current segment to disk.
Parameters
writer: The IndexWriter
Examples
iex> :ok = TantivyEx.IndexWriter.commit(writer)
:ok
Deletes all documents in the index.
This operation marks all documents for deletion but does not make the deletions visible until the writer is committed.
Parameters
writer: The IndexWriter
Examples
iex> :ok = TantivyEx.IndexWriter.delete_all_documents(writer)
:ok
iex> :ok = TantivyEx.IndexWriter.commit(writer)
:ok
@spec delete_documents(t(), TantivyEx.Query.t()) :: :ok | {:error, String.t()}
Deletes all documents matching the given query.
This operation marks documents for deletion but does not make the deletions visible until the writer is committed.
Parameters
writer: The IndexWriterquery: A TantivyEx.Query to match documents to delete
Examples
iex> query = TantivyEx.Query.term(schema, "status", "inactive")
iex> :ok = TantivyEx.IndexWriter.delete_documents(writer, query)
:ok
@spec new(TantivyEx.Index.t(), pos_integer()) :: {:ok, t()} | {:error, String.t()}
Creates a new IndexWriter for the given index.
Parameters
index: The index to write tomemory_budget: Memory budget in bytes (default: 50MB)
Examples
iex> schema = TantivyEx.Schema.new()
iex> schema = TantivyEx.Schema.add_text_field(schema, "title", :text_stored)
iex> {:ok, index} = TantivyEx.Index.create_in_ram(schema)
iex> {:ok, writer} = TantivyEx.IndexWriter.new(index)
iex> is_reference(writer)
true
Rolls back any pending changes and cancels the current operation.
This should be called when errors occur during a batch index operation to avoid partial updates.
Parameters
writer: The IndexWriter
Examples
iex> :ok = TantivyEx.IndexWriter.rollback(writer)
:ok