From 54f04650d16278f23e9239af196493dbf52f92e1 Mon Sep 17 00:00:00 2001 From: Jonas Winkler Date: Tue, 10 Nov 2020 01:47:35 +0100 Subject: [PATCH] fixed an issue with the searcher. --- src/documents/index.py | 9 +++++++-- src/documents/views.py | 13 ++++++------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/documents/index.py b/src/documents/index.py index a099f670c..82a35a63e 100644 --- a/src/documents/index.py +++ b/src/documents/index.py @@ -1,4 +1,5 @@ import logging +from contextlib import contextmanager from django.db import models from django.dispatch import receiver @@ -99,15 +100,19 @@ def remove_document_from_index(document): remove_document(writer, document) +@contextmanager def query_page(ix, query, page): - with ix.searcher() as searcher: + searcher = ix.searcher() + try: query_parser = MultifieldParser(["content", "title", "correspondent"], ix.schema).parse(query) result_page = searcher.search_page(query_parser, page) result_page.results.fragmenter = highlight.ContextFragmenter( surround=50) result_page.results.formatter = JsonFormatter() - return result_page + yield result_page + finally: + searcher.close() def autocomplete(ix, term, limit=10): diff --git a/src/documents/views.py b/src/documents/views.py index b3d6012f1..8cc330141 100755 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -191,13 +191,12 @@ class SearchView(APIView): except (ValueError, TypeError): page = 1 - result_page = index.query_page(self.ix, query, page) - - return Response( - {'count': len(result_page), - 'page': result_page.pagenum, - 'page_count': result_page.pagecount, - 'results': list(map(self.add_infos_to_hit, result_page))}) + with index.query_page(self.ix, query, page) as result_page: + return Response( + {'count': len(result_page), + 'page': result_page.pagenum, + 'page_count': result_page.pagecount, + 'results': list(map(self.add_infos_to_hit, result_page))}) else: return Response({