moved some code

This commit is contained in:
Jonas Winkler 2020-11-08 11:30:16 +01:00
parent 942fab7298
commit 7747e6512a
2 changed files with 19 additions and 14 deletions

View File

@ -2,9 +2,11 @@ import logging
from django.db import models from django.db import models
from django.dispatch import receiver from django.dispatch import receiver
from whoosh import highlight
from whoosh.fields import Schema, TEXT, NUMERIC from whoosh.fields import Schema, TEXT, NUMERIC
from whoosh.highlight import Formatter, get_text from whoosh.highlight import Formatter, get_text
from whoosh.index import create_in, exists_in, open_dir from whoosh.index import create_in, exists_in, open_dir
from whoosh.qparser import MultifieldParser
from whoosh.writing import AsyncWriter from whoosh.writing import AsyncWriter
from documents.models import Document from documents.models import Document
@ -97,6 +99,17 @@ def remove_document_from_index(document):
remove_document(writer, 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): def autocomplete(ix, term, limit=10):
with ix.reader() as reader: with ix.reader() as reader:
terms = [] terms = []

View File

@ -6,9 +6,6 @@ from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.decorators import action from rest_framework.decorators import action
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.views import APIView 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.db import GnuPG
from paperless.views import StandardPagination from paperless.views import StandardPagination
@ -194,18 +191,13 @@ class SearchView(APIView):
except (ValueError, TypeError): except (ValueError, TypeError):
page = 1 page = 1
with self.ix.searcher() as searcher: result_page = index.query_page(self.ix, query, page)
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( return Response(
{'count': len(result_page), {'count': len(result_page),
'page': result_page.pagenum, 'page': result_page.pagenum,
'page_count': result_page.pagecount, 'page_count': result_page.pagecount,
'results': list(map(self.add_infos_to_hit, result_page))}) 'results': list(map(self.add_infos_to_hit, result_page))})
else: else:
return Response({ return Response({