Used whoosh.writing.AsyncWriter. See #2198.

This commit is contained in:
Norman Jäckel 2016-08-03 16:46:52 +02:00
parent e688a345c0
commit 7f1b894db4

View File

@ -9,6 +9,7 @@ from whoosh import fields
from whoosh.filedb.filestore import RamStorage from whoosh.filedb.filestore import RamStorage
from whoosh.index import create_in, exists_in, open_dir from whoosh.index import create_in, exists_in, open_dir
from whoosh.qparser import QueryParser from whoosh.qparser import QueryParser
from whoosh.writing import AsyncWriter
def get_schema(): def get_schema():
@ -113,11 +114,9 @@ def index_add_instance(sender, instance, **kwargs):
If the instance has an method get_search_string, then it is written If the instance has an method get_search_string, then it is written
into the search index. The method has to return an dictonary that can be into the search index. The method has to return an dictonary that can be
used as keyword arguments to writer.add_document. used as keyword arguments to writer.add_document.
This function uses whoosh.writing.AsyncWriter.
""" """
# TODO: This method blocks the search index. So in a multi thread environment
# this method can raise whoosh.store.LockError. Therefore it has to
# be done in tornado to support the big mode.
# See: https://pythonhosted.org/Whoosh/indexing.html#indexing-documents
try: try:
get_search_index_string = instance.get_search_index_string get_search_index_string = instance.get_search_index_string
except AttributeError: except AttributeError:
@ -132,7 +131,7 @@ def index_add_instance(sender, instance, **kwargs):
'collection': instance.get_collection_string(), 'collection': instance.get_collection_string(),
'content': get_search_index_string()} 'content': get_search_index_string()}
with index.get_or_create_index().writer() as writer: with AsyncWriter(index.get_or_create_index()) as writer:
if created: if created:
writer.add_document(**writer_kwargs) writer.add_document(**writer_kwargs)
else: else:
@ -144,6 +143,8 @@ def index_del_instance(sender, instance, **kwargs):
Like index_add_instance but deletes the instance from the index. Like index_add_instance but deletes the instance from the index.
Should be called by the post_delete signal. Should be called by the post_delete signal.
This function uses whoosh.writing.AsyncWriter.
""" """
try: try:
# Try to get the arrribute get_search_attributes. It is not needed # Try to get the arrribute get_search_attributes. It is not needed
@ -154,7 +155,7 @@ def index_del_instance(sender, instance, **kwargs):
# If the instance is not searchable, then exit this signal early. # If the instance is not searchable, then exit this signal early.
return return
with index.get_or_create_index().writer() as writer: with AsyncWriter(index.get_or_create_index()) as writer:
writer.delete_by_term('id_collection', combine_id_and_collection(instance)) writer.delete_by_term('id_collection', combine_id_and_collection(instance))