Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

moved some code

Jonas Winkler7747e6512a06
diff --git a/src/documents/index.py b/src/documents/index.py
index c55402e10..a099f670c 100644
--- a/src/documents/index.py
+++ b/src/documents/index.py
@@ -2,9 +2,11 @@ import logging
from django.db import models
from django.dispatch import receiver
+from whoosh import highlight
from whoosh.fields import Schema, TEXT, NUMERIC
from whoosh.highlight import Formatter, get_text
from whoosh.index import create_in, exists_in, open_dir
+from whoosh.qparser import MultifieldParser
from whoosh.writing import AsyncWriter
from documents.models import Document
@@ -97,6 +99,17 @@ def remove_document_from_index(document):
remove_document(writer, document)
+def query_page(ix, query, page):
+ with ix.searcher() as searcher:
+ 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
+
+
def autocomplete(ix, term, limit=10):
with ix.reader() as reader:
terms = []
diff --git a/src/documents/views.py b/src/documents/views.py
index 28ac7ae26..b3d6012f1 100755
--- a/src/documents/views.py
+++ b/src/documents/views.py
@@ -6,9 +6,6 @@ from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.views import APIView
-from whoosh import highlight
-from whoosh.qparser import QueryParser
-from whoosh.query import terms
from paperless.db import GnuPG
from paperless.views import StandardPagination
@@ -194,18 +191,13 @@ class SearchView(APIView):
except (ValueError, TypeError):
page = 1
- with self.ix.searcher() as searcher:
- query_parser = QueryParser("content", self.ix.schema).parse(query)
- result_page = searcher.search_page(query_parser, page)
- result_page.results.fragmenter = highlight.ContextFragmenter(
- surround=50)
- result_page.results.formatter = index.JsonFormatter()
-
- 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))})
+ 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))})
else:
return Response({