Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit a9f15be

Browse files
authored
feat: support for aliased index (#460)
* fix: support index alias * chore: remove debug code
1 parent 298553d commit a9f15be

8 files changed

Lines changed: 33 additions & 14 deletions

File tree

analytics_data_api/management/commands/create_elasticsearch_learners_indices.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
from analytics_data_api.v0.documents import RosterEntry, RosterUpdate
77

88

9+
class TestRosterEntry(RosterEntry):
10+
class Index:
11+
name = settings.ELASTICSEARCH_LEARNERS_INDEX
12+
aliases = {settings.ELASTICSEARCH_LEARNERS_INDEX_ALIAS: {}}
13+
14+
915
class Command(BaseCommand):
1016
help = 'Creates Elasticsearch indices used by the Analytics Data API.'
1117

@@ -20,7 +26,7 @@ def handle(self, *args, **options):
2026
if es.indices.exists(settings.ELASTICSEARCH_LEARNERS_INDEX):
2127
self.stderr.write(f'"{settings.ELASTICSEARCH_LEARNERS_INDEX}" index already exists.')
2228
else:
23-
RosterEntry.init(using=es)
29+
TestRosterEntry.init(using=es)
2430

2531
if es.indices.exists(settings.ELASTICSEARCH_LEARNERS_UPDATE_INDEX):
2632
self.stderr.write(f'"{settings.ELASTICSEARCH_LEARNERS_UPDATE_INDEX}" index already exists.')

analytics_data_api/management/commands/delete_elasticsearch_learners_indices.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ class Command(BaseCommand):
99
def handle(self, *args, **options):
1010
es = Elasticsearch([settings.ELASTICSEARCH_LEARNERS_HOST])
1111
for index in [settings.ELASTICSEARCH_LEARNERS_INDEX, settings.ELASTICSEARCH_LEARNERS_UPDATE_INDEX]:
12-
if es.indices.exists(settings.ELASTICSEARCH_LEARNERS_UPDATE_INDEX):
12+
if es.indices.exists(index):
1313
es.indices.delete(index=index)

analytics_data_api/management/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ def elasticsearch_settings_defined():
66
setting is not None for setting in (
77
settings.ELASTICSEARCH_LEARNERS_HOST,
88
settings.ELASTICSEARCH_LEARNERS_INDEX,
9+
settings.ELASTICSEARCH_LEARNERS_INDEX_ALIAS,
910
settings.ELASTICSEARCH_LEARNERS_UPDATE_INDEX
1011
)
1112
)

analytics_data_api/v0/documents.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from fnmatch import fnmatch
23

34
from django.conf import settings
45
from elasticsearch_dsl import Date, Document, Float, Integer, Keyword, Q
@@ -22,7 +23,7 @@ class Index:
2223

2324
@classmethod
2425
def get_last_updated(cls):
25-
return cls.search().query('term', target_index=settings.ELASTICSEARCH_LEARNERS_INDEX).execute()
26+
return cls.search().query('term', target_index=settings.ELASTICSEARCH_LEARNERS_INDEX_ALIAS).execute()
2627

2728

2829
class RosterEntry(Document):
@@ -61,9 +62,19 @@ class RosterEntry(Document):
6162
last_updated = Date()
6263

6364
class Index:
64-
name = settings.ELASTICSEARCH_LEARNERS_INDEX
65+
name = settings.ELASTICSEARCH_LEARNERS_INDEX_ALIAS
66+
aliases = {settings.ELASTICSEARCH_LEARNERS_INDEX_ALIAS: {}}
6567
settings = settings.ELASTICSEARCH_INDEX_SETTINGS
6668

69+
@classmethod
70+
def _matches(cls, hit):
71+
# override _matches to match indices in a pattern instead of just index name.
72+
# The search target may be an index or an alias but the default implementation will strictly
73+
# match the return on index causing queries to fail to map back to this python type.
74+
#
75+
# hit is the raw dict as returned by elasticsearch
76+
return fnmatch(hit["_index"], settings.ELASTICSEARCH_LEARNERS_INDEX_ALIAS + '_*')
77+
6778
@classmethod
6879
def get_course_user(cls, course_id, username):
6980
"""
@@ -149,14 +160,7 @@ def get_users_in_course(
149160
}
150161
for sort_policy in sort_policies
151162
])
152-
res = search_request.execute()
153-
# debugging, to be removed
154-
logger.warning(res.__class__.__name__)
155-
items = res.hits
156-
if items:
157-
logger.warning(items[0])
158-
logger.warning(dir(items[0]))
159-
return items
163+
return search_request.execute()
160164

161165
@classmethod
162166
def get_course_metadata(cls, course_id):

analytics_data_api/v0/tests/views/test_learners.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def create_update_index(self, date=None):
119119
index=settings.ELASTICSEARCH_LEARNERS_UPDATE_INDEX,
120120
body={
121121
'date': date,
122-
'target_index': settings.ELASTICSEARCH_LEARNERS_INDEX,
122+
'target_index': settings.ELASTICSEARCH_LEARNERS_INDEX_ALIAS,
123123
}
124124
)
125125
self._es.indices.refresh(index=settings.ELASTICSEARCH_LEARNERS_UPDATE_INDEX)

analyticsdataserver/settings/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
########## ELASTICSEARCH CONFIGURATION
8585
ELASTICSEARCH_LEARNERS_HOST = environ.get('ELASTICSEARCH_LEARNERS_HOST', None)
8686
ELASTICSEARCH_LEARNERS_INDEX = environ.get('ELASTICSEARCH_LEARNERS_INDEX', None)
87+
ELASTICSEARCH_LEARNERS_INDEX_ALIAS = environ.get('ELASTICSEARCH_LEARNERS_INDEX_ALIAS', None)
8788
ELASTICSEARCH_LEARNERS_UPDATE_INDEX = environ.get('ELASTICSEARCH_LEARNERS_UPDATE_INDEX', None)
8889

8990
# access credentials for signing requests to AWS.

analyticsdataserver/settings/local.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,10 @@
106106
)
107107
CORS_ALLOW_CREDENTIALS = True
108108

109+
# Default elasticsearch port when running locally
110+
ELASTICSEARCH_LEARNERS_HOST = environ.get("ELASTICSEARCH_LEARNERS_HOST", 'http://localhost:9223/')
111+
ELASTICSEARCH_LEARNERS_INDEX = 'roster_entry_001'
112+
ELASTICSEARCH_LEARNERS_INDEX_ALIAS = 'roster_entry'
113+
ELASTICSEARCH_LEARNERS_UPDATE_INDEX = 'index_update'
114+
109115
########## END ANALYTICS DATA API CONFIGURATION

analyticsdataserver/settings/test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525

2626
# Default elasticsearch port when running locally
2727
ELASTICSEARCH_LEARNERS_HOST = environ.get("ELASTICSEARCH_LEARNERS_HOST", 'http://localhost:9223/')
28-
ELASTICSEARCH_LEARNERS_INDEX = 'roster_test'
28+
ELASTICSEARCH_LEARNERS_INDEX = 'roster_test_001'
29+
ELASTICSEARCH_LEARNERS_INDEX_ALIAS = 'roster_test'
2930
ELASTICSEARCH_LEARNERS_UPDATE_INDEX = 'index_update_test'
3031

3132
# Default the django-storage settings so we can test easily

0 commit comments

Comments
 (0)