High-level object for writing to an index.
To get a writer for a particular index, call writer() on the Index object.
>>> writer = my_index.writer()
You can use this object as a context manager. If an exception is thrown from within the context it calls cancel(), otherwise it calls commit() when the context exits.
Adds all the fields of a document at once. This is an alternative to calling start_document(), add_field() [...], end_document().
The keyword arguments map field names to the values to index/store.
For fields that are both indexed and stored, you can specify an alternate value to store using a keyword argument in the form “_stored_<fieldname>”. For example, if you have a field named “title” and you want to index the text “a b c” but store the text “e f g”, use keyword arguments like this:
writer.add_document(title=u"a b c", _stored_title=u"e f g")
Adds or replaces a document. At least one of the fields for which you supply values must be marked as ‘unique’ in the index’s schema.
The keyword arguments map field names to the values to index/store.
Note that this method will only replace a committed document; currently it cannot replace documents you’ve added to the IndexWriter but haven’t yet committed. For example, if you do this:
>>> writer.update_document(unique_id=u"1", content=u"Replace me")
>>> writer.update_document(unique_id=u"1", content=u"Replacement")
...this will add two documents with the same value of unique_id, instead of the second document replacing the first.
For fields that are both indexed and stored, you can specify an alternate value to store using a keyword argument in the form “_stored_<fieldname>”. For example, if you have a field named “title” and you want to index the text “a b c” but store the text “e f g”, use keyword arguments like this:
writer.update_document(title=u"a b c", _stored_title=u"e f g")
Convenience wrapper for a writer object that might fail due to locking (i.e. the filedb writer). This object will attempt once to obtain the underlying writer, and if it’s successful, will simply pass method calls on to it.
If this object can’t obtain a writer immediately, it will buffer delete, add, and update method calls in memory until you call commit(). At that point, this object will start running in a separate thread, trying to obtain the writer over and over, and once it obtains it, “replay” all the buffered method calls on it.
In a typical scenario where you’re adding a single or a few documents to the index as the result of a Web transaction, this lets you just create the writer, add, and commit, without having to worry about index locks, retries, etc.
The first argument is a callable which returns the actual writer. Usually this will be the writer method of your Index object. Any additional keyword arguments to the initializer are passed into the callable.
For example, to get an aynchronous writer, instead of this:
>>> writer = myindex.writer(postlimit=128 * 1024 * 1024)
Do this:
>>> from whoosh.writing import AsyncWriter
>>> writer = AsyncWriter(myindex.writer, postlimit=128 * 1024 * 1024)
Parameters: |
|
---|