1616)
1717from  django .core .cache  import  cache 
1818from  django .db  import  models , transaction 
19- from  django .db .models  import  Q 
19+ from  django .db .models  import  (
20+     Case ,
21+     Q ,
22+     When ,
23+ )
2024from  django .db .models .fields .json  import  KeyTextTransform 
2125from  django .utils .functional  import  cached_property 
2226from  django .utils .html  import  strip_tags 
2731from  . import  utils 
2832from  .search  import  (
2933    DEFAULT_TEXT_SEARCH_CONFIG ,
30-     DOCUMENT_SEARCH_VECTOR ,
3134    START_SEL ,
3235    STOP_SEL ,
3336    TSEARCH_CONFIG_LANGUAGES ,
37+     get_document_search_vector ,
3438)
3539
3640
@@ -261,7 +265,7 @@ def search(self, query_text, release, document_category=None):
261265            search_query  =  SearchQuery (
262266                query_text , config = models .F ("config" ), search_type = "websearch" 
263267            )
264-             search_rank  =  SearchRank (models .F ("search " ), search_query )
268+             search_rank  =  SearchRank (models .F ("search_vector " ), search_query )
265269            search  =  partial (
266270                SearchHeadline ,
267271                start_sel = START_SEL ,
@@ -296,7 +300,7 @@ def search(self, query_text, release, document_category=None):
296300            )
297301            vector_qs  =  (
298302                base_qs .alias (rank = search_rank )
299-                 .filter (search = search_query )
303+                 .filter (search_vector = search_query )
300304                .order_by ("-rank" )
301305            )
302306            if  not  vector_qs :
@@ -314,22 +318,6 @@ def search(self, query_text, release, document_category=None):
314318        else :
315319            return  self .none ()
316320
317-     def  search_reset (self ):
318-         """Set to null all not null Document's search vector fields.""" 
319-         return  Document .objects .exclude (search = None ).update (search = None )
320- 
321-     def  search_update (self ):
322-         """ 
323-         Update Document's search vector fields using the document definition. 
324- 
325-         This method don't index the module pages (since source code is hard to 
326-         combine with full text search) and the big flattened index of the CBVs. 
327-         """ 
328-         return  Document .objects .exclude (
329-             Q (path__startswith = "_modules" )
330-             |  Q (path__startswith = "ref/class-based-views/flattened-index" )
331-         ).update (search = DOCUMENT_SEARCH_VECTOR )
332- 
333321
334322class  Document (models .Model ):
335323    """ 
@@ -344,8 +332,20 @@ class Document(models.Model):
344332    path  =  models .CharField (max_length = 500 )
345333    title  =  models .CharField (max_length = 500 )
346334    metadata  =  models .JSONField (default = dict )
347-     search  =  SearchVectorField (null = True , editable = False )
348-     config  =  models .SlugField (default = DEFAULT_TEXT_SEARCH_CONFIG )
335+     search_vector  =  models .GeneratedField (
336+         expression = Case (
337+             * [
338+                 When (config = lang , then = get_document_search_vector (lang ))
339+                 for  lang  in  TSEARCH_CONFIG_LANGUAGES .values ()
340+             ],
341+             default = get_document_search_vector (),
342+         ),
343+         output_field = SearchVectorField (),
344+         db_persist = True ,
345+     )
346+     config  =  models .SlugField (
347+         db_default = DEFAULT_TEXT_SEARCH_CONFIG , default = DEFAULT_TEXT_SEARCH_CONFIG 
348+     )
349349
350350    objects  =  DocumentQuerySet .as_manager ()
351351
@@ -354,7 +354,7 @@ class Meta:
354354            models .Index (
355355                fields = ["release" , "title" ], name = "document_release_title_idx" 
356356            ),
357-             GinIndex (fields = ["search"  ] ),
357+             GinIndex (fields = ["search_vector"  ],  name = "document_search_vector_idx" ),
358358        ]
359359        unique_together  =  ("release" , "path" )
360360
0 commit comments