-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from wullerot/feature/rebuild-index
management command for rebuilding index
- Loading branch information
Showing
5 changed files
with
78 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.idea/ | ||
.vscode/ | ||
*.egg-info/ | ||
*.pyc | ||
*.coverage | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import sys | ||
|
||
from django.contrib.contenttypes.models import ContentType | ||
|
||
from postgres_searchindex import conf | ||
from postgres_searchindex.models import IndexEntry | ||
from postgres_searchindex.source_pool import source_pool | ||
|
||
|
||
def delete_indexes(): | ||
IndexEntry.objects.all().delete() | ||
|
||
|
||
def update_indexes(): | ||
for index_key, index in conf.POSTGRES_SEARCHINDEX.items(): | ||
sys.stdout.write("====================================") | ||
sys.stdout.write( | ||
f"Updating index \"{index_key}\" with kwargs {index.get('kwargs', {})}" | ||
) | ||
for source_cls in source_pool.get_sources().values(): | ||
source = source_cls(**index.get("kwargs", {})) | ||
sys.stdout.write( | ||
f"{source.model.__name__}. " | ||
f"Indexing {source.get_queryset().count()} entries" | ||
) | ||
# index | ||
current_ids = [] | ||
for obj in source.get_queryset(): | ||
source.update(index_key, obj) | ||
current_ids.append(obj.id) | ||
# remove no more existing | ||
content_type = ContentType.objects.get_for_model(source.model) | ||
to_delete = IndexEntry.objects.filter( | ||
index_key=index_key, | ||
content_type=content_type, | ||
).exclude( | ||
object_id__in=current_ids, | ||
) | ||
delete_result = to_delete.delete() | ||
sys.stdout.write(f"> Done. Removed from index: {delete_result[0]}") |
29 changes: 26 additions & 3 deletions
29
postgres_searchindex/management/commands/postgres_searchindex_rebuild.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,31 @@ | ||
# commands/reindex_indexes.py | ||
from django.core.management import BaseCommand | ||
|
||
from ._utils import delete_indexes, update_indexes | ||
|
||
class Command(BaseCommand): | ||
help = "Try send something to Sentry!" | ||
help = "Reindexing postgres " | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
'--force', | ||
action='store_true', | ||
help='Force reindex without confirmation' | ||
) | ||
|
||
def handle(self, *args, **options): | ||
pass | ||
if not options['force']: | ||
self.stdout.write(self.style.WARNING( | ||
"This will delete all indexes and reindex them." | ||
" Are you sure you want to proceed?" | ||
)) | ||
|
||
confirmation = input("Type 'yes' to continue: ") | ||
|
||
if confirmation.lower() != 'yes': | ||
self.stdout.write(self.style.ERROR("Reindexing aborted.")) | ||
return | ||
|
||
self.stdout.write(self.style.SUCCESS("Starting the reindexing process...")) | ||
delete_indexes() | ||
update_indexes() | ||
self.stdout.write(self.style.SUCCESS("Reindexing completed successfully!")) |
32 changes: 2 additions & 30 deletions
32
postgres_searchindex/management/commands/postgres_searchindex_update.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,10 @@ | ||
from django.contrib.contenttypes.models import ContentType | ||
from django.core.management import BaseCommand | ||
|
||
from postgres_searchindex import conf | ||
from postgres_searchindex.models import IndexEntry | ||
from postgres_searchindex.source_pool import source_pool | ||
from ._utils import update_indexes | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Update/build index" | ||
|
||
def handle(self, *args, **options): | ||
for index_key, index in conf.POSTGRES_SEARCHINDEX.items(): | ||
self.stdout.write("====================================") | ||
self.stdout.write( | ||
f"Updating index \"{index_key}\" with kwargs {index.get('kwargs', {})}" | ||
) | ||
for source_cls in source_pool.get_sources().values(): | ||
source = source_cls(**index.get("kwargs", {})) | ||
self.stdout.write( | ||
f"{source.model.__name__}. " | ||
f"Indexing {source.get_queryset().count()} entries" | ||
) | ||
# index | ||
current_ids = [] | ||
for obj in source.get_queryset(): | ||
source.update(index_key, obj) | ||
current_ids.append(obj.id) | ||
# remove no more existing | ||
content_type = ContentType.objects.get_for_model(source.model) | ||
to_delete = IndexEntry.objects.filter( | ||
index_key=index_key, | ||
content_type=content_type, | ||
).exclude( | ||
object_id__in=current_ids, | ||
) | ||
delete_result = to_delete.delete() | ||
self.stdout.write(f"> Done. Removed from index: {delete_result[0]}") | ||
update_indexes() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters