88from django .conf import settings
99from django .contrib .auth .models import AnonymousUser
1010from django .core .exceptions import ImproperlyConfigured
11+ from django .db .models import Subquery
1112from django .utils .module_loading import import_string
1213
1314import requests
1718logger = logging .getLogger (__name__ )
1819
1920
21+ @cache
22+ def is_document_indexer_configured () -> bool :
23+ """Returns true if the indexer service is enabled and properly configured."""
24+ try :
25+ get_document_indexer_class ()
26+ except ImproperlyConfigured :
27+ return False
28+
29+ return True
30+
31+
2032@cache
2133def get_document_indexer_class () -> "BaseDocumentIndexer" :
2234 """Return the indexer backend class based on the settings."""
@@ -65,7 +77,7 @@ def get_batch_accesses_by_users_and_teams(paths):
6577 return dict (access_by_document_path )
6678
6779
68- def get_visited_document_ids_of (user ):
80+ def get_visited_document_ids_of (queryset , user ):
6981 """
7082 Returns the ids of the documents that have a linktrace to the user and NOT owned.
7183 It will be use to limit the opensearch responses to the public documents already
@@ -74,11 +86,18 @@ def get_visited_document_ids_of(user):
7486 if isinstance (user , AnonymousUser ):
7587 return []
7688
77- qs = models .LinkTrace .objects .filter (user = user ).exclude (
78- document__accesses__user = user ,
89+ qs = models .LinkTrace .objects .filter (user = user )
90+
91+ docs = (
92+ queryset .exclude (accesses__user = user )
93+ .filter (
94+ deleted_at__isnull = True ,
95+ ancestors_deleted_at__isnull = True ,
96+ )
97+ .filter (pk__in = Subquery (qs .values ("document_id" )))
7998 )
8099
81- return list ({str (id ) for id in qs .values_list ("document_id " , flat = True )})
100+ return list ({str (id ) for id in docs .values_list ("pk " , flat = True )})
82101
83102
84103class BaseDocumentIndexer (ABC ):
@@ -159,22 +178,23 @@ def push(self, data):
159178 Must be implemented by subclasses.
160179 """
161180
162- def search (self , text , user , token ):
181+ # pylint: disable-next=too-many-arguments,too-many-positional-arguments
182+ def search (self , text , token , visited = (), page = 1 , page_size = 50 ):
163183 """
164184 Search for documents in Find app.
165185 """
166- visited_ids = get_visited_document_ids_of (user )
167-
168186 response = self .search_query (
169187 data = {
170188 "q" : text ,
171- "visited" : visited_ids ,
189+ "visited" : visited ,
172190 "services" : ["docs" ],
191+ "page_number" : page ,
192+ "page_size" : page_size ,
173193 },
174194 token = token ,
175195 )
176196
177- return self . format_response ( response )
197+ return [ d [ "_id" ] for d in response ]
178198
179199 @abstractmethod
180200 def search_query (self , data , token ) -> dict :
@@ -184,14 +204,6 @@ def search_query(self, data, token) -> dict:
184204 Must be implemented by subclasses.
185205 """
186206
187- @abstractmethod
188- def format_response (self , data : dict ):
189- """
190- Convert the JSON response from Find app as document queryset.
191-
192- Must be implemented by subclasses.
193- """
194-
195207
196208class FindDocumentIndexer (BaseDocumentIndexer ):
197209 """
@@ -253,12 +265,6 @@ def search_query(self, data, token) -> requests.Response:
253265 logger .error ("HTTPError: %s" , e )
254266 raise
255267
256- def format_response (self , data : dict ):
257- """
258- Retrieve documents ids from Find app response and return a queryset.
259- """
260- return models .Document .objects .filter (pk__in = [d ["_id" ] for d in data ])
261-
262268 def push (self , data ):
263269 """
264270 Push a batch of documents to the Find backend.
0 commit comments