A community-supported supercharged document management system: scan, index and archive all your documents
Python aiangulararchivingdjangodmsdocument-managementdocument-management-systemllmmachine-learningocroptical-character-recognitionpdf
moved some code
Jonas Winkler7747e6512a06
diff --git a/src/documents/index.py b/src/documents/index.pyindex c55402e10..a099f670c 100644--- a/src/documents/index.py+++ b/src/documents/index.py@@ -2,9 +2,11 @@ import loggingfrom django.db import modelsfrom django.dispatch import receiver+from whoosh import highlightfrom whoosh.fields import Schema, TEXT, NUMERICfrom whoosh.highlight import Formatter, get_textfrom whoosh.index import create_in, exists_in, open_dir+from whoosh.qparser import MultifieldParserfrom whoosh.writing import AsyncWriterfrom 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.pyindex 28ac7ae26..b3d6012f1 100755--- a/src/documents/views.py+++ b/src/documents/views.py@@ -6,9 +6,6 @@ from django_filters.rest_framework import DjangoFilterBackendfrom rest_framework.decorators import actionfrom rest_framework.response import Responsefrom rest_framework.views import APIView-from whoosh import highlight-from whoosh.qparser import QueryParser-from whoosh.query import termsfrom paperless.db import GnuPGfrom 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({