Added --schema-only flag to update_index command

pull/2285/merge
Karl Hobley 2016-03-21 18:41:43 +00:00 zatwierdzone przez Matt Westcott
rodzic fead70bee3
commit b978fb826f
5 zmienionych plików z 51 dodań i 8 usunięć

Wyświetl plik

@ -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)

Wyświetl plik

@ -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:

Wyświetl plik

@ -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
~~~~~~~~~

Wyświetl plik

@ -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('')

Wyświetl plik

@ -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):