fixed an issue with the searcher.

This commit is contained in:
Jonas Winkler 2020-11-10 01:47:35 +01:00
parent 9d22d9c9b1
commit 54f04650d1
2 changed files with 13 additions and 9 deletions

View File

@ -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):

View File

@ -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({