mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-02 13:45:10 -05:00
added autocomplete to backend
This commit is contained in:
parent
fb0ad94a9c
commit
653edc1fdc
@ -75,6 +75,7 @@ def update_document(writer, doc):
|
||||
content=doc.content
|
||||
)
|
||||
|
||||
|
||||
@receiver(models.signals.post_save, sender=Document)
|
||||
def add_document_to_index(sender, instance, **kwargs):
|
||||
ix = open_index()
|
||||
@ -102,3 +103,13 @@ def query_index(ix, querystr):
|
||||
'score': r.score,
|
||||
'title': r['title']
|
||||
} for r in results]
|
||||
|
||||
|
||||
def autocomplete(ix, term, limit=10):
|
||||
with ix.reader() as reader:
|
||||
terms = []
|
||||
for t in reader.expand_prefix("content", term.lower()):
|
||||
terms.append(t)
|
||||
if len(terms) >= limit:
|
||||
break
|
||||
return terms
|
||||
|
@ -151,7 +151,11 @@ class LogViewSet(ReadOnlyModelViewSet):
|
||||
|
||||
|
||||
class SearchView(APIView):
|
||||
|
||||
permission_classes = (IsAuthenticated,)
|
||||
|
||||
ix = index.open_index()
|
||||
|
||||
def get(self, request, format=None):
|
||||
if 'query' in request.query_params:
|
||||
query = request.query_params['query']
|
||||
@ -162,3 +166,26 @@ class SearchView(APIView):
|
||||
return Response(query_results)
|
||||
else:
|
||||
return Response([])
|
||||
|
||||
|
||||
class SearchAutoCompleteView(APIView):
|
||||
|
||||
permission_classes = (IsAuthenticated,)
|
||||
|
||||
ix = index.open_index()
|
||||
|
||||
def get(self, request, format=None):
|
||||
if 'term' in request.query_params:
|
||||
term = request.query_params['term']
|
||||
else:
|
||||
term = None
|
||||
|
||||
if 'limit' in request.query_params:
|
||||
limit = int(request.query_params['limit'])
|
||||
else:
|
||||
limit = 10
|
||||
|
||||
if term is not None:
|
||||
return Response(index.autocomplete(self.ix, term, limit))
|
||||
else:
|
||||
return Response([])
|
||||
|
@ -11,7 +11,8 @@ from documents.views import (
|
||||
TagViewSet,
|
||||
DocumentTypeViewSet,
|
||||
SearchView,
|
||||
IndexView
|
||||
IndexView,
|
||||
SearchAutoCompleteView
|
||||
)
|
||||
|
||||
api_router = DefaultRouter()
|
||||
@ -26,6 +27,7 @@ urlpatterns = [
|
||||
|
||||
# API
|
||||
url(r"^api/auth/",include(('rest_framework.urls', 'rest_framework'), namespace="rest_framework")),
|
||||
url(r"^api/search/autocomplete/", SearchAutoCompleteView.as_view(), name="autocomplete"),
|
||||
url(r"^api/search/", SearchView.as_view(), name="search"),
|
||||
url(r"^api/token/", views.obtain_auth_token), url(r"^api/", include((api_router.urls, 'drf'), namespace="drf")),
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user