@@ -0,0 +1,3236 @@
+import sys%0A%0Afrom django.core.management.base import BaseCommand, CommandError%0A%0Afrom haystack import connections as haystack_connections%0Afrom haystack.exceptions import NotHandled%0Afrom haystack.query import SearchQuerySet%0Afrom haystack.utils.app_loading import get_models, load_apps%0A%0Adef get_all_indexed_models():%0A%0A    backends = haystack_connections.connections_info.keys()%0A%0A    available_models = %7B%7D%0A%0A    for backend_key in backends:%0A        unified_index = haystack_connections%5Bbackend_key%5D.get_unified_index()%0A        for app in load_apps():%0A            for model in get_models(app):%0A                try:%0A                    unified_index.get_index(model)%0A                except NotHandled:%0A                    continue%0A                model_name = model.__module__ + '.' + model.__name__%0A                available_models%5Bmodel_name%5D = %7B%0A                    'backend_key': backend_key,%0A                    'app': app,%0A                    'model': model,%0A                %7D%0A%0A    return available_models%0A%0Adef get_models_to_check(model_names, available_models):%0A    %0A    models_to_check = %5B%5D%0A%0A    if model_names:%0A        missing_models = False%0A        for model_name in model_names:%0A            if model_name in available_models:%0A                models_to_check.append(model_name)%0A            else:%0A                missing_models = True%0A                print %22There was no model %7B0%7D with a search index%22.format(model_name)%0A            if missing_models:%0A                print %22Some models were not found; they must be one of:%22%0A                for model in sorted(available_models.keys()):%0A                    print %22 %22, model%0A                sys.exit(1)%0A    else:%0A        models_to_check = sorted(available_models.keys())%0A%0A    return models_to_check%0A%0A%0Aclass Command(BaseCommand):%0A    args = 'MODEL ...'%0A    help = 'Get all search results for the given models'%0A%0A    def handle(self, *args, **options):%0A%0A        available_models = get_all_indexed_models()%0A%0A        models_to_check = get_models_to_check(args, available_models)%0A%0A        # Now we know which models to check, do that:%0A%0A        for model_name in models_to_check:%0A            model_details = available_models%5Bmodel_name%5D%0A%0A            backend_key = model_details%5B'backend_key'%5D%0A            model = model_details%5B'model'%5D%0A%0A            backend = haystack_connections%5Bbackend_key%5D.get_backend()%0A            unified_index = haystack_connections%5Bbackend_key%5D.get_unified_index()%0A            %0A            index = unified_index.get_index(model)%0A%0A            qs = index.build_queryset()%0A            print %22Checking %7B0%7D (%7B1%7D objects in the database)%22.format(%0A                model_name, qs.count()%0A            )%0A            # Get all the primary keys from the database:%0A            pks_in_database = set(%0A                unicode(pk) for pk in qs.values_list('pk', flat=True)%0A            )%0A            # Then go through every search result for that%0A            # model, and check that the primary key is one%0A            # that's in the database:%0A            for search_result in SearchQuerySet(using=backend.connection_alias).models(model):%0A                if search_result.pk not in pks_in_database:%0A                    print %22      stale search entry for primary key%22, search_result.pk%0A

Add a command to help find out-of-sync objects in Elasticsearch