Muninn.IndexWriter (Muninn v0.5.5)
View SourceIndexWriter for adding documents to a Muninn index.
The IndexWriter handles document insertion and manages commits to make documents searchable. Writers are automatically managed per index.
Usage
{:ok, index} = Muninn.Index.create("/path/to/index", schema)
# Add a single document
doc = %{"title" => "Hello", "views" => 100}
:ok = Muninn.IndexWriter.add_document(index, doc)
# Add multiple documents
docs = [
%{"title" => "First", "views" => 10},
%{"title" => "Second", "views" => 20}
]
:ok = Muninn.IndexWriter.add_documents(index, docs)
# Commit changes
:ok = Muninn.IndexWriter.commit(index)
Summary
Functions
Adds a single document to the index.
Adds multiple documents to the index in a batch operation.
Commits all pending changes to the index.
Deletes all documents matching a (field, value) term.
Rolls back all uncommitted changes.
Functions
Adds a single document to the index.
The document should be a map where keys match the field names defined in the schema. Field values are automatically converted to the correct types.
Parameters
index- The index to add the document todocument- A map with field names as keys
Returns
:ok- Document added successfully{:error, reason}- Failed to add document
Examples
doc = %{
"title" => "Hello World",
"views" => 100,
"price" => 19.99,
"published" => true
}
:ok = Muninn.IndexWriter.add_document(index, doc)
Adds multiple documents to the index in a batch operation.
This is more efficient than calling add_document/2 multiple times.
Parameters
index- The index to add documents todocuments- A list of document maps
Returns
:ok- All documents added successfully{:error, reason}- Failed to add documents
Examples
docs = [
%{"title" => "First", "views" => 10},
%{"title" => "Second", "views" => 20},
%{"title" => "Third", "views" => 30}
]
:ok = Muninn.IndexWriter.add_documents(index, docs)
Commits all pending changes to the index.
After committing, all added documents become searchable. This operation flushes the current segment to disk.
Parameters
index- The index to commit
Returns
:ok- Commit successful{:error, reason}- Failed to commit
Examples
:ok = Muninn.IndexWriter.add_document(index, doc)
:ok = Muninn.IndexWriter.commit(index)
@spec delete_term(reference(), String.t(), String.t() | integer() | boolean()) :: :ok | {:error, String.t()}
Deletes all documents matching a (field, value) term.
Tantivy marks the matching documents as deleted; the delete becomes
visible to searches after the next commit/1. Useful for incremental
refresh: delete docs whose key matches value, then re-add the new
versions in the same transaction.
Supports text, u64, i64, and bool fields. f64 is not supported (Tantivy has no stable term encoding for floats).
Parameters
index- The index to delete fromfield_name- Name of the indexed field to match againstvalue- The value to match (string / integer / boolean)
Returns
:ok- Delete queued (visible aftercommit/1){:error, reason}- Failed (e.g. field not found, unsupported type)
Examples
:ok = Muninn.IndexWriter.add_document(index, %{"id" => "doc-1", "body" => "hello"})
:ok = Muninn.IndexWriter.commit(index)
:ok = Muninn.IndexWriter.delete_term(index, "id", "doc-1")
:ok = Muninn.IndexWriter.commit(index)
# "doc-1" no longer searchable
Rolls back all uncommitted changes.
Discards all documents added since the last commit without making them searchable.
Parameters
index- The index to rollback
Returns
:ok- Rollback successful{:error, reason}- Failed to rollback
Examples
:ok = Muninn.IndexWriter.add_document(index, doc)
:ok = Muninn.IndexWriter.rollback(index)
# Document is not searchable