Skip to content

Commit

Permalink
Merge pull request #793 from aaxelb/feature/open-badges
Browse files Browse the repository at this point in the history
[ENG-3905] add osf_related_resource_types index field
  • Loading branch information
aaxelb committed Aug 8, 2022
2 parents 4eb2b3c + 9ba5094 commit a702759
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 6 deletions.
3 changes: 2 additions & 1 deletion share/apps.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from django.apps import AppConfig
from django.db.models.signals import post_migrate
from share.signals import post_migrate_load_sources
from share.signals import ensure_latest_elastic_mappings, post_migrate_load_sources


class ShareConfig(AppConfig):
name = 'share'

def ready(self):
post_migrate.connect(post_migrate_load_sources, sender=self)
post_migrate.connect(ensure_latest_elastic_mappings, sender=self)
8 changes: 8 additions & 0 deletions share/bin/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ def setup(args, argv):
elastic_manager.update_primary_alias(primary_index)


@search.subcommand('Update mappings for an existing index')
def update_mappings(args, argv):
"""
Usage: {0} search update_mappings <index_name>
"""
ElasticManager().update_mappings(args['<index_name>'])


@search.subcommand('Set the "primary" index used to serve search results')
def set_primary(args, argv):
"""
Expand Down
3 changes: 3 additions & 0 deletions share/metadata_formats/sharev2_elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ def format(self, normalized_datum):
'subjects': self._get_subjects(central_work, source_name),
'subject_synonyms': self._get_subject_synonyms(central_work),

# osf-specific extra
'osf_related_resource_types': (central_work['extra'] or {}).get('osf_related_resource_types'),

# a bunch of nested data because reasons -- used mostly for rendering search results
'lists': {
'affiliations': self._build_related_agent_list(central_work, ['agentworkrelation']),
Expand Down
14 changes: 9 additions & 5 deletions share/search/elastic_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,18 @@ def create_index(self, index_name):
body={'settings': index_setup.index_settings},
)

logger.info('Putting Elasticsearch mappings')
self.update_mappings(index_name)

self.es_client.indices.refresh(index_name)

logger.debug('Waiting for yellow status')
self.es_client.cluster.health(wait_for_status='yellow')
logger.info('Finished setting up Elasticsearch index %s', index_name)

def update_mappings(self, index_name):
index_setup = self.get_index_setup(index_name)

logger.info('Putting Elasticsearch mappings')
for doc_type, mapping in index_setup.index_mappings.items():
logger.debug('Putting mapping for %s', doc_type)
self.es_client.indices.put_mapping(
Expand All @@ -61,10 +69,6 @@ def create_index(self, index_name):
index=index_name,
)

self.es_client.indices.refresh(index_name)

logger.info('Finished setting up Elasticsearch index %s', index_name)

def stream_actions(self, actions):
stream = elastic_helpers.streaming_bulk(
self.es_client,
Expand Down
1 change: 1 addition & 0 deletions share/search/index_setup/postrend_backcompat.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def index_mappings(self):
'type': {'type': 'keyword', 'include_in_all': False},
'types': {'type': 'keyword', 'include_in_all': False},
'withdrawn': {'type': 'boolean', 'include_in_all': False},
'osf_related_resource_types': {'type': 'object', 'dynamic': True, 'include_in_all': False},
'lists': {'type': 'object', 'dynamic': True, 'include_in_all': False},
},
'dynamic_templates': [
Expand Down
8 changes: 8 additions & 0 deletions share/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ def post_migrate_load_sources(sender, **kwargs):
except ProgrammingError:
return
management.call_command('loadsources')


def ensure_latest_elastic_mappings(sender, **kwargs):
from share.search.elastic_manager import ElasticManager
elastic_manager = ElasticManager()

for index_name in elastic_manager.get_primary_indexes():
elastic_manager.update_mappings(index_name)
6 changes: 6 additions & 0 deletions tests/share/bin/test_sharectl.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ def test_set_primary(self):
run_sharectl('search', 'set_primary', 'blazblat')
assert mock_elastic_manager.update_primary_alias.mock_calls == [mock.call('blazblat')]

def test_update_mappings(self):
mock_elastic_manager = mock.Mock()
with mock.patch('share.bin.search.ElasticManager', return_value=mock_elastic_manager):
run_sharectl('search', 'update_mappings', 'blazblat')
assert mock_elastic_manager.update_mappings.mock_calls == [mock.call('blazblat')]

def test_daemon(self, settings):
expected_indexes = ['bliz', 'blaz', 'bluz']
settings.ELASTICSEARCH['ACTIVE_INDEXES'] = expected_indexes
Expand Down
43 changes: 43 additions & 0 deletions tests/share/metadata_formats/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,49 @@
},
},
},
'with-osf-extra': {
'suid_id': 99,
'source_name': 'OsfProbably',
'raw_datum_kwargs': {
'date_created': dateutil.parser.isoparse('2017-04-07T21:09:05.023090+00:00'),
},
'normalized_datum_kwargs': {
'created_at': dateutil.parser.isoparse('2017-04-07T21:09:05.023090+00:00'),
'data': {
'@graph': [
{
'@id': '_:p',
'@type': 'person',
'name': 'Open McOperton',
},
{
'@id': '_:c',
'@type': 'creator',
'agent': {'@id': '_:p', '@type': 'person'},
'creative_work': {'@id': '_:w', '@type': 'creativework'},
'cited_as': 'Open McOperton',
'order_cited': 0,
},
{
'@id': '_:i',
'@type': 'workidentifier',
'creative_work': {'@id': '_:w', '@type': 'creativework'},
'uri': 'https://example.com/open',
},
{
'@id': '_:w',
'@type': 'creativework',
'title': 'So open',
'date_updated': '2017-03-31T05:39:48+00:00',
'extra': {
'osf_related_resource_types': {'foo': True, 'bar': False},
},
},
],
'@context': {}
},
},
},
}


Expand Down
14 changes: 14 additions & 0 deletions tests/share/metadata_formats/test_oai_dc_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,18 @@ def assert_formatter_outputs_equal(self, actual_output, expected_output):
<dc:relation>http://staging.osf.io/vroom/</dc:relation>
</oai_dc:dc>
''',
'with-osf-extra': '''
<oai_dc:dc
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd"
>
<dc:title>So open</dc:title>
<dc:creator>Open McOperton</dc:creator>
<dc:date>2017-03-31T05:39:48Z</dc:date>
<dc:type>creativework</dc:type>
<dc:identifier>https://example.com/open</dc:identifier>
</oai_dc:dc>
''',
}
43 changes: 43 additions & 0 deletions tests/share/metadata_formats/test_sharev2_elastic_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,47 @@ def assert_formatter_outputs_equal(self, actual_output, expected_output):
'publishers': [],
},
},
'with-osf-extra': {
'affiliations': [],
'contributors': ['Open McOperton'],
'date': '2017-03-31T05:39:48+00:00',
'date_created': '2017-04-07T21:09:05.023090+00:00',
'date_modified': '2017-04-07T21:09:05.023090+00:00',
'date_updated': '2017-03-31T05:39:48+00:00',
'id': 'encoded-99',
'identifiers': ['https://example.com/open'],
'sources': ['OsfProbably'],
'subject_synonyms': [],
'subjects': [],
'title': 'So open',
'type': 'creative work',
'types': ['creative work'],
'retracted': False,
'funders': [],
'hosts': [],
'publishers': [],
'tags': [],
'osf_related_resource_types': {
'foo': True,
'bar': False,
},
'lists': {
'affiliations': [],
'contributors': [
{
'cited_as': 'Open McOperton',
'identifiers': [],
'name': 'Open McOperton',
'order_cited': 0,
'relation': 'creator',
'type': 'person',
'types': ['person', 'agent'],
},
],
'lineage': [],
'funders': [],
'hosts': [],
'publishers': [],
},
},
}

0 comments on commit a702759

Please sign in to comment.