Skip to content

Commit

Permalink
sharectl: add reindex_all_suids command
Browse files Browse the repository at this point in the history
for when you have an empty elasticsearch index that you want full

example: after adding `my_new_index` to
`project.settings.ELASTICSEARCH['INDEXES']`:
```
sharectl search setup my_new_index
sharectl search reindex_all_suids my_new_index
```
  • Loading branch information
aaxelb committed Mar 5, 2021
1 parent 8a9a117 commit 60e4182
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions share/bin/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
from project.celery import app as celery_app

from django.conf import settings
from django.db.models import Exists, OuterRef

from share.bin.util import command
from share.models import FormattedMetadataRecord, SourceUniqueIdentifier
from share.search import MessageType, SearchIndexer
from share.search.daemon import SearchIndexerDaemon
from share.search.elastic_manager import ElasticManager

Expand Down Expand Up @@ -66,6 +69,31 @@ def set_primary(args, argv):
ElasticManager().update_primary_alias(args['<index_name>'])


@search.subcommand('Queue daemon messages to reindex all suids')
def reindex_all_suids(args, argv):
"""
Usage: {0} search reindex_all_suids <index_name>
Most likely useful for a freshly `setup` index (perhaps after a purge).
"""
# TODO check for a specific format of FMR, not just that *any* FMR exists
suid_id_queryset = (
SourceUniqueIdentifier.objects
.annotate(
has_fmr=Exists(
FormattedMetadataRecord.objects.filter(suid_id=OuterRef('id'))
)
)
.filter(has_fmr=True)
.values_list('id', flat=True)
)
SearchIndexer().send_messages(
message_type=MessageType.INDEX_SUID,
target_ids=suid_id_queryset,
index_names=[args['<index_name>']],
)


@search.subcommand('Start the search indexing daemon')
def daemon(args, argv):
"""
Expand Down

0 comments on commit 60e4182

Please sign in to comment.