diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 6f6844050b..fea83d56d3 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -18,6 +18,7 @@ Changelog * Increased size of Save button on site settings (Liam Brenner) * Optimised Site.find_for_request to only perform one database query (Matthew Downey) * Notification messages on creating / editing sites now include the site name if specified (Chris Rogers) + * Added ``--schema-only`` option to ``update_index`` management command * Fix: The currently selected day is now highlighted only in the correct month in date pickers (Jonas Lergell) * Fix: Fixed crash when an image without a source file was resized with the "dynamic serve view" * Fix: Registered settings admin menu items now show active correctly (Matthew Downey) diff --git a/docs/reference/management_commands.rst b/docs/reference/management_commands.rst index 7d6805f4c6..5b65c57483 100644 --- a/docs/reference/management_commands.rst +++ b/docs/reference/management_commands.rst @@ -76,6 +76,17 @@ For example, to update just the default backend: python manage.py update_index --backend default +Indexing the schema only +```````````````````````` + +.. versionadded:: 1.5 + +You can prevent the ``update_index`` command from indexing any data by using the ``--schema-only`` option: + +.. code-block:: sh + + python manage.py update_index --schema-only + .. _search_garbage_collect: diff --git a/docs/releases/1.5.rst b/docs/releases/1.5.rst index 2695dd0d70..ccb3a07de0 100644 --- a/docs/releases/1.5.rst +++ b/docs/releases/1.5.rst @@ -38,6 +38,7 @@ Minor features * Increased size of Save button on site settings (Liam Brenner) * Optimised Site.find_for_request to only perform one database query (Matthew Downey) * Notification messages on creating / editing sites now include the site name if specified (Chris Rogers) + * Added ``--schema-only`` option to ``update_index`` management command Bug fixes ~~~~~~~~~ diff --git a/wagtail/wagtailsearch/management/commands/update_index.py b/wagtail/wagtailsearch/management/commands/update_index.py index 7ade2ed7ed..2e4df29dd7 100644 --- a/wagtail/wagtailsearch/management/commands/update_index.py +++ b/wagtail/wagtailsearch/management/commands/update_index.py @@ -9,7 +9,7 @@ from wagtail.wagtailsearch.index import get_indexed_models class Command(BaseCommand): - def update_backend(self, backend_name): + def update_backend(self, backend_name, schema_only=False): # Print info self.stdout.write("Updating backend: " + backend_name) @@ -36,13 +36,15 @@ class Command(BaseCommand): # Add model index.add_model(model) - # Add items (1000 at a time) - count = 0 - for chunk in self.print_iter_progress(self.queryset_chunks(model.get_indexed_objects())): - index.add_items(model, chunk) - count += len(chunk) + # Index objects + object_count = 0 + if not schema_only: + # Add items (1000 at a time) + for chunk in self.print_iter_progress(self.queryset_chunks(model.get_indexed_objects())): + index.add_items(model, chunk) + object_count += len(chunk) - self.stdout.write("(indexed %d objects)" % count) + self.stdout.write("(indexed %d objects)" % object_count) self.print_newline() # Finish rebuild @@ -53,6 +55,9 @@ class Command(BaseCommand): parser.add_argument( '--backend', action='store', dest='backend_name', default=None, help="Specify a backend to update") + parser.add_argument( + '--schema-only', action='store', dest='schema_only', default=None, + help="Prevents loading any data into the index") def handle(self, **options): # Get list of backends to index @@ -68,7 +73,7 @@ class Command(BaseCommand): # Update backends for backend_name in backend_names: - self.update_backend(backend_name) + self.update_backend(backend_name, schema_only=options['schema_only']) def print_newline(self): self.stdout.write('') diff --git a/wagtail/wagtailsearch/tests/test_elasticsearch_backend.py b/wagtail/wagtailsearch/tests/test_elasticsearch_backend.py index 79717888fd..a0f82c85d7 100644 --- a/wagtail/wagtailsearch/tests/test_elasticsearch_backend.py +++ b/wagtail/wagtailsearch/tests/test_elasticsearch_backend.py @@ -4,11 +4,15 @@ from __future__ import absolute_import, unicode_literals import datetime import json import os +import time import unittest import mock + +from django.core import management from django.db.models import Q from django.test import TestCase +from django.utils.six import StringIO from elasticsearch.serializer import JSONSerializer from wagtail.tests.search import models @@ -224,6 +228,27 @@ class TestElasticSearchBackend(BackendTests, TestCase): results = self.backend.search("Hello World", models.SearchTest, operator='and', fields=['title']) self.assertEqual(list(results), [a]) + def test_update_index_command_schema_only(self): + # Reset the index, this should clear out the index + self.backend.reset_index() + + # Give Elasticsearch some time to catch up... + time.sleep(1) + + results = self.backend.search(None, models.SearchTest) + self.assertEqual(set(results), set()) + + # Run update_index command + with self.ignore_deprecation_warnings(): + # ignore any DeprecationWarnings thrown by models with old-style indexed_fields definitions + management.call_command( + 'update_index', backend_name=self.backend_name, schema_only=True, interactive=False, stdout=StringIO() + ) + + # Unlike the test_update_index_command test. This should not give any results + results = self.backend.search(None, models.SearchTest) + self.assertEqual(set(results), set()) + class TestElasticSearchQuery(TestCase): def assertDictEqual(self, a, b):