summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDave Hall <dave@etianen.com>2017-12-06 15:21:27 (GMT)
committerGitHub <noreply@github.com>2017-12-06 15:21:27 (GMT)
commit2f6ccc8bec8281e00232ec343c89ba50e47025ff (patch)
tree531b26b168f3e3ff0bc00230173b87ec8895d338
parent2226de139b6e177bfbe2824b1749478dbcce3318 (diff)
parentc187407932b1ab658bb83d549f67e420002a8547 (diff)
downloaddjango-watson-2f6ccc8bec8281e00232ec343c89ba50e47025ff.zip
django-watson-2f6ccc8bec8281e00232ec343c89ba50e47025ff.tar.gz
django-watson-2f6ccc8bec8281e00232ec343c89ba50e47025ff.tar.bz2
Merge pull request #226 from dustinrb/slim-indexing
"Slim" Indexing
-rw-r--r--watson/management/commands/buildwatson.py25
1 files changed, 21 insertions, 4 deletions
diff --git a/watson/management/commands/buildwatson.py b/watson/management/commands/buildwatson.py
index 59703d2..ff08946 100644
--- a/watson/management/commands/buildwatson.py
+++ b/watson/management/commands/buildwatson.py
@@ -28,7 +28,7 @@ def get_engine(engine_slug_):
raise CommandError("Search Engine \"%s\" is not registered!" % force_text(engine_slug_))
-def rebuild_index_for_model(model_, engine_slug_, verbosity_):
+def rebuild_index_for_model(model_, engine_slug_, verbosity_, slim_=False):
"""rebuilds index for a model"""
search_engine_ = get_engine(engine_slug_)
@@ -36,7 +36,13 @@ def rebuild_index_for_model(model_, engine_slug_, verbosity_):
local_refreshed_model_count = [0] # HACK: Allows assignment to outer scope.
def iter_search_entries():
- for obj in model_._default_manager.all().iterator():
+ # Only index specified objects if slim_ is True
+ if slim_ and search_engine_._registered_models[model_].get_live_queryset():
+ obj_list = search_engine_._registered_models[model_].get_live_queryset()
+ else:
+ obj_list = model_._default_manager.all()
+
+ for obj in obj_list.iterator():
for search_entry in search_engine_._update_obj_index_iter(obj):
yield search_entry
local_refreshed_model_count[0] += 1
@@ -74,6 +80,14 @@ class Command(BaseCommand):
action="store",
help='Search engine models are registered with'
)
+ parser.add_argument(
+ '--slim',
+ action='store_true',
+ default=False,
+ help="Only include objects which satisfy the filter specified during \
+ model registration. WARNING: buildwatson must be rerun if the filter \
+ changes or the index will be incomplete."
+ )
@transaction.atomic()
def handle(self, *args, **options):
@@ -89,6 +103,9 @@ class Command(BaseCommand):
engine_slug = "default"
engine_selected = False
+ # Do we do a partial index?
+ slim = options.get("slim")
+
# work-around for legacy optparser hack in BaseCommand. In Django=1.10 the
# args are collected in options['apps'], but in earlier versions they are
# kept in args.
@@ -123,7 +140,7 @@ class Command(BaseCommand):
if verbosity >= 3:
print("Using search engine \"%s\"" % engine_slug)
for model in models:
- refreshed_model_count += rebuild_index_for_model(model, engine_slug, verbosity)
+ refreshed_model_count += rebuild_index_for_model(model, engine_slug, verbosity, slim_=slim)
else: # full rebuild (for one or all search engines)
if engine_selected:
@@ -139,7 +156,7 @@ class Command(BaseCommand):
registered_models = search_engine.get_registered_models()
# Rebuild the index for all registered models.
for model in registered_models:
- refreshed_model_count += rebuild_index_for_model(model, engine_slug, verbosity)
+ refreshed_model_count += rebuild_index_for_model(model, engine_slug, verbosity, slim_=slim)
# Clean out any search entries that exist for stale content types.
# Only do it during full rebuild