22
22
import json
23
23
import logging
24
24
import os
25
+ import time
25
26
26
27
from django .core .management .base import BaseCommand
27
28
28
29
from forum .models import Post
29
30
from search import solrapi
31
+ from search .management .commands .post_dirty_sounds_to_search_engine import time_stats
30
32
from utils .search import get_search_engine
31
33
from utils .search .search_forum import add_posts_to_search_engine , get_all_post_ids_from_search_engine , \
32
34
delete_all_posts_from_search_engine , delete_posts_from_search_engine
@@ -46,13 +48,6 @@ def add_arguments(self, parser):
46
48
type = int ,
47
49
help = 'How many posts to add at once' )
48
50
49
- parser .add_argument (
50
- '--recreate-index' ,
51
- action = 'store_true' ,
52
- dest = 'recreate_index' ,
53
- default = False ,
54
- help = 'Create a new index and index into it. Update the forum alias to point to this new index.' )
55
-
56
51
def handle (self , * args , ** options ):
57
52
search_engine = get_search_engine ()
58
53
@@ -65,30 +60,23 @@ def handle(self, *args, **options):
65
60
collection_name = f"forum_{ current_date } "
66
61
new_collection_url = f"{ search_engine .solr_base_url } /solr/{ collection_name } "
67
62
solr_api = solrapi .SolrManagementAPI (search_engine .solr_base_url , collection_name )
68
- recreate_index = options ['recreate_index' ]
69
-
70
- if recreate_index :
71
- solr_api .create_collection_and_schema (delete_default_fields_definition , forum_schema_definition , "thread_id" )
63
+ solr_api .create_collection_and_schema (delete_default_fields_definition , forum_schema_definition , "thread_id" )
72
64
73
65
# Select all moderated forum posts and index them
74
66
all_posts = Post .objects .select_related ("thread" , "author" , "thread__author" , "thread__forum" )\
75
67
.filter (moderation_state = "OK" )
76
68
num_posts = len (all_posts )
77
69
console_logger .info ("Re-indexing %d forum posts" , num_posts )
78
70
slice_size = options ['size_size' ]
71
+ n_posts_indexed_correctly = 0
72
+ starttime = time .monotonic ()
79
73
for i in range (0 , num_posts , slice_size ):
80
74
post_ids_slice = all_posts [i :i + slice_size ]
75
+ n_posts_indexed = len (post_ids_slice )
81
76
add_posts_to_search_engine (post_ids_slice , solr_collection_url = new_collection_url )
77
+ n_posts_indexed_correctly += n_posts_indexed
78
+ elapsed , remaining = time_stats (n_posts_indexed_correctly , num_posts , starttime )
79
+ console_logger .info (f"Added { n_posts_indexed_correctly } /{ num_posts } posts. Elapsed: { elapsed } , Remaining: { remaining } " )
82
80
83
- # Find all indexed forum posts which are not in the DB and remove them. This part of the code should do nothing
84
- # as deleted forum posts should be removed from the index in due time. In particular, if the "clear index" is
85
- # passed, this bit of code should remove no posts.
86
- indexed_post_ids = get_all_post_ids_from_search_engine (solr_collection_url = new_collection_url )
87
- post_ids_to_delete = list (set (indexed_post_ids ).difference (all_posts .values_list ('id' , flat = True )))
88
- console_logger .info ("Deleting %d non-existing posts from the search engine" , len (post_ids_to_delete ))
89
- if post_ids_to_delete :
90
- delete_posts_from_search_engine (post_ids_to_delete , solr_collection_url = new_collection_url )
91
-
92
- if recreate_index :
93
- console_logger .info ("Updating the forum alias to point to the new index" )
94
- solr_api .create_collection_alias ("forum" )
81
+ console_logger .info ("Updating the forum alias to point to the new index" )
82
+ solr_api .create_collection_alias ("forum" )
0 commit comments