Skip to content

Instantly share code, notes, and snippets.

@andrewschoen
Created October 24, 2014 18:02
Show Gist options
  • Save andrewschoen/30884fab2ec54057dc13 to your computer and use it in GitHub Desktop.
Save andrewschoen/30884fab2ec54057dc13 to your computer and use it in GitHub Desktop.
diff --git a/haystack/management/commands/clear_index.py b/haystack/management/commands/clear_index.py
index 93b5f91..3c09f78 100644
--- a/haystack/management/commands/clear_index.py
+++ b/haystack/management/commands/clear_index.py
@@ -18,6 +18,9 @@ class Command(BaseCommand):
help='Update only the named backend (can be used multiple times). '
'By default all backends will be updated.'
),
+ make_option('--nocommit', action='store_false', dest='commit',
+ default=True, help='Will pass commit=False to the backend.'
+ ),
)
option_list = BaseCommand.option_list + base_options
@@ -25,6 +28,7 @@ class Command(BaseCommand):
"""Clears out the search index completely."""
from haystack import connections
self.verbosity = int(options.get('verbosity', 1))
+ self.commit = options.get('commit', True)
using = options.get('using')
if not using:
@@ -47,7 +51,7 @@ class Command(BaseCommand):
for backend_name in using:
backend = connections[backend_name].get_backend()
- backend.clear()
+ backend.clear(commit=self.commit)
if self.verbosity >= 1:
print("All documents removed.")
diff --git a/test_haystack/solr_tests/test_management_commands.py b/test_haystack/solr_tests/test_management_commands.py
index 7d5bf95..0a2a8bb 100644
--- a/test_haystack/solr_tests/test_management_commands.py
+++ b/test_haystack/solr_tests/test_management_commands.py
@@ -74,6 +74,9 @@ class ManagementCommandTestCase(TestCase):
call_command('rebuild_index', interactive=False, verbosity=0, commit=True)
self.assertEqual(self.solr.search('*:*').hits, 23)
+ call_command('clear_index', interactive=False, verbosity=0, commit=False)
+ self.assertEqual(self.solr.search('*:*').hits, 23)
+
def test_remove(self):
call_command('clear_index', interactive=False, verbosity=0)
self.assertEqual(self.solr.search('*:*').hits, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment