11import logging
2+ from fnmatch import fnmatch
23
34from django .conf import settings
45from 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
2829class 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 ):
0 commit comments