-
Notifications
You must be signed in to change notification settings - Fork 20
feat: add elastic search commands #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,25 @@ | ||
| """Management command for deleting unused indices""" | ||
|
|
||
| from django.core.management.base import BaseCommand | ||
|
|
||
| from forum.search.es_helper import ElasticsearchHelper | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = ( | ||
| "Delete all Elasticsearch indices that are not the latest for each model type." | ||
| ) | ||
|
|
||
| def handle(self, *args: list[str], **kwargs: dict[str, str]) -> None: | ||
| """ | ||
| Handles the execution of the delete_unused_forum_indices command. | ||
|
|
||
| This command deletes all Elasticsearch indices that are not the latest for each model type. | ||
| """ | ||
| es_helper = ElasticsearchHelper() | ||
| indices_deleted_count = es_helper.delete_unused_indices() | ||
| self.stdout.write( | ||
| self.style.SUCCESS( | ||
| f"{indices_deleted_count} unused indices deleted successfully." | ||
| ) | ||
| ) |
This file contains hidden or 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,41 @@ | ||
| """Management command for initialize forum indices""" | ||
|
|
||
| from argparse import ArgumentParser | ||
| from typing import Any | ||
|
|
||
| from django.core.management.base import BaseCommand | ||
|
|
||
| from forum.search.es_helper import ElasticsearchHelper | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = "Initialize Elasticsearch indices, optionally forcing the creation of new indices." | ||
|
|
||
| def add_arguments(self, parser: ArgumentParser) -> None: | ||
| """ | ||
| Adds command line arguments to the initialize_forum_indices command. | ||
|
|
||
| Args: | ||
| parser: The argument parser to which the --force argument is added. | ||
| """ | ||
| parser.add_argument( | ||
| "--force", | ||
| action="store_true", | ||
| help="Force the creation of new indices even if they exist.", | ||
| ) | ||
|
|
||
| def handle(self, *args: list[str], **kwargs: dict[str, Any]) -> None: | ||
| """ | ||
| Handles the execution of the initialize_indices command. | ||
|
|
||
| Initializes Elasticsearch indices. If the --force option is provided, | ||
| it forces the creation of new indices even if they exist. | ||
|
|
||
| Args: | ||
| args: Additional arguments. | ||
| kwargs: Command options. | ||
| """ | ||
| es_helper = ElasticsearchHelper() | ||
| force_new_index = bool(kwargs.get("force", False)) | ||
| es_helper.initialize_indices(force_new_index=force_new_index) | ||
| self.stdout.write(self.style.SUCCESS("Forum indices initialized successfully.")) | ||
This file contains hidden or 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,55 @@ | ||
| """Management command for rebuild forum indices""" | ||
|
|
||
| from argparse import ArgumentParser | ||
|
|
||
| from django.core.management.base import BaseCommand | ||
|
|
||
| from forum.search.es_helper import ElasticsearchHelper | ||
| from forum.utils import get_int_value_from_collection | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = "Rebuild Elasticsearch indices by creating new indices and reindexing data." | ||
|
|
||
| def add_arguments(self, parser: ArgumentParser) -> None: | ||
| """ | ||
| Adds command line arguments to the rebuild_forum_indices command. | ||
|
|
||
| Args: | ||
| parser: The argument parser to which the --batch_size and --extra_catchup_minutes arguments are added. | ||
| """ | ||
| parser.add_argument( | ||
| "--batch_size", | ||
| type=int, | ||
| default=500, | ||
| help="Number of documents to process in each batch.", | ||
| ) | ||
| parser.add_argument( | ||
| "--extra_catchup_minutes", | ||
| type=int, | ||
| default=5, | ||
| help="Extra minutes to adjust the start time for catch-up.", | ||
| ) | ||
|
|
||
| def handle(self, *args: list[str], **kwargs: dict[str, int]) -> None: | ||
| """ | ||
| Handles the execution of the rebuild_indices command. | ||
|
|
||
| Rebuilds Elasticsearch indices by creating new indices and reindexing data. | ||
| Batch size and extra catch-up minutes can be specified. | ||
|
|
||
| Args: | ||
| args: Additional arguments. | ||
| kwargs: Command options. | ||
| """ | ||
| es_helper = ElasticsearchHelper() | ||
|
|
||
| batch_size = get_int_value_from_collection(kwargs, "batch_size", 500) | ||
| extra_catchup_minutes = get_int_value_from_collection( | ||
| kwargs, "extra_catchup_minutes", 5 | ||
| ) | ||
|
|
||
| es_helper.rebuild_indices( | ||
| batch_size=batch_size, extra_catchup_minutes=extra_catchup_minutes | ||
| ) | ||
| self.stdout.write(self.style.SUCCESS("Forum indices rebuilt successfully.")) |
This file contains hidden or 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,22 @@ | ||
| """Management command for validating forum mappings""" | ||
|
|
||
| from django.core.management.base import BaseCommand | ||
|
|
||
| from forum.search.es_helper import ElasticsearchHelper | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = "Validate Elasticsearch indices for correct mappings and properties." | ||
|
|
||
| def handle(self, *args: list[str], **kwargs: dict[str, str]) -> None: | ||
| """ | ||
| Handles the execution of the validate_forum_indices command. | ||
|
|
||
| This command validates that the Elasticsearch indices have the correct mappings and properties. | ||
|
|
||
| Raises: | ||
| ValueError: If indices do not exist or if mappings/properties are missing or incorrect. | ||
| """ | ||
| es_helper = ElasticsearchHelper() | ||
| es_helper.validate_indices() | ||
| self.stdout.write(self.style.SUCCESS("Forum indices validated successfully.")) |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.