alvas
alvas

Reputation: 122122

Resolving Whoosh IndexingError: Writer is closed

Different from python whoosh IndexingError when interrupted, I've not interrupted any commits but the IndexingError occurs when creating a new index:

import uuid
import os

from whoosh.index import create_in
from whoosh.fields import *
from whoosh.qparser import QueryParser

schema = Schema(hashID=TEXT(stored=True))
indexdir = 'foobar'
if not os.path.exists(indexdir):
    os.mkdir(indexdir)

ix = create_in(indexdir, schema)

with ix.writer() as writer:
    writer.add_document(hashID=str(uuid.uuid4()))
    writer.commit()

The error:

---------------------------------------------------------------------------
IndexingError                             Traceback (most recent call last)
<ipython-input-1-85a42bebdce8> in <module>()
     15 with ix.writer() as writer:
     16     writer.add_document(hashID=str(uuid.uuid4()))
---> 17     writer.commit()

/usr/local/lib/python3.5/site-packages/whoosh/writing.py in __exit__(self, exc_type, exc_val, exc_tb)
    208             self.cancel()
    209         else:
--> 210             self.commit()
    211 
    212     def group(self):

/usr/local/lib/python3.5/site-packages/whoosh/writing.py in commit(self, mergetype, optimize, merge)
    918         """
    919 
--> 920         self._check_state()
    921         # Merge old segments if necessary
    922         finalsegments = self._merge_segments(mergetype, optimize, merge)

/usr/local/lib/python3.5/site-packages/whoosh/writing.py in _check_state(self)
    553     def _check_state(self):
    554         if self.is_closed:
--> 555             raise IndexingError("This writer is closed")
    556 
    557     def _setup_doc_offsets(self):

IndexingError: This writer is closed

The writer should be within the context scope so I'm not sure why it's closed though it's newly created. How to resolve the IndexingError on a new index?

Upvotes: 1

Views: 1298

Answers (1)

Max
Max

Reputation: 1815

writer.commit() saves the changes and closes the writer.

Then at the end of the statement, with ix.writer() as writer: tries to close the writer which is already closed and doesn't exist.

Thus, your with statement is equivalent to:

try:
    writer = ix.writer()
    writer.add_document(hashID=str(uuid.uuid4()))
    writer.commit()
finally:
    writer.commit()

As a solution, whether you omit writer.commit() in your with statement or you get rid of with statement and recreate writer every time you want to commit.

Upvotes: 4

Related Questions