Fix: Removes the FieldParser plugin from autocomplete searches (#4934)

This commit is contained in:
Trenton H 2023-12-11 10:21:58 -08:00 committed by GitHub
parent af0817ab74
commit 7e12bd1bef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 1090 additions and 1002 deletions

View File

@ -3,6 +3,7 @@ import math
import os import os
from collections import Counter from collections import Counter
from contextlib import contextmanager from contextlib import contextmanager
from typing import Optional
from dateutil.parser import isoparse from dateutil.parser import isoparse
from django.conf import settings from django.conf import settings
@ -26,6 +27,7 @@ from whoosh.qparser import MultifieldParser
from whoosh.qparser import QueryParser from whoosh.qparser import QueryParser
from whoosh.qparser.dateparse import DateParserPlugin from whoosh.qparser.dateparse import DateParserPlugin
from whoosh.qparser.dateparse import English from whoosh.qparser.dateparse import English
from whoosh.qparser.plugins import FieldsPlugin
from whoosh.scoring import TF_IDF from whoosh.scoring import TF_IDF
from whoosh.searching import ResultsPage from whoosh.searching import ResultsPage
from whoosh.searching import Searcher from whoosh.searching import Searcher
@ -425,7 +427,12 @@ class DelayedMoreLikeThisQuery(DelayedQuery):
return q, mask return q, mask
def autocomplete(ix: FileIndex, term: str, limit: int = 10, user: User = None): def autocomplete(
ix: FileIndex,
term: str,
limit: int = 10,
user: Optional[User] = None,
):
""" """
Mimics whoosh.reading.IndexReader.most_distinctive_terms with permissions Mimics whoosh.reading.IndexReader.most_distinctive_terms with permissions
and without scoring and without scoring
@ -434,6 +441,9 @@ def autocomplete(ix: FileIndex, term: str, limit: int = 10, user: User = None):
with ix.searcher(weighting=TF_IDF()) as s: with ix.searcher(weighting=TF_IDF()) as s:
qp = QueryParser("content", schema=ix.schema) qp = QueryParser("content", schema=ix.schema)
# Don't let searches with a query that happen to match a field override the
# content field query instead and return bogus, not text data
qp.remove_plugin_class(FieldsPlugin)
q = qp.parse(f"{term.lower()}*") q = qp.parse(f"{term.lower()}*")
user_criterias = get_permissions_criterias(user) user_criterias = get_permissions_criterias(user)
@ -453,7 +463,7 @@ def autocomplete(ix: FileIndex, term: str, limit: int = 10, user: User = None):
return terms return terms
def get_permissions_criterias(user: User = None): def get_permissions_criterias(user: Optional[User] = None):
user_criterias = [query.Term("has_owner", False)] user_criterias = [query.Term("has_owner", False)]
if user is not None: if user is not None:
if user.is_superuser: # superusers see all docs if user.is_superuser: # superusers see all docs

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff