Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search: add Created and Updated fields #73

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions asclepias_broker/mappings/v6/relationships/v1.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@
},
"PublicationDate": {
"type": "date"
},
"Created": {
"type": "date"
},
"Updated": {
"type": "date"
}
}
},
Expand Down Expand Up @@ -270,8 +276,20 @@
},
"PublicationDate": {
"type": "date"
},
"Created": {
"type": "date"
},
"Updated": {
"type": "date"
}
}
},
"Created": {
"type": "date"
},
"Updated": {
"type": "date"
}
},
"dynamic": false
Expand Down
20 changes: 20 additions & 0 deletions asclepias_broker/search/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@ def build_group_metadata(group: Group) -> dict:
ids = id_group.identifiers
doc = deepcopy((id_group.data and id_group.data.json) or {})
all_ids = sum([g.identifiers for g in group.groups], [])
doc['Created'] = id_group.data.created
doc['Updated'] = id_group.data.updated
else:
doc = deepcopy((group.data and group.data.json) or {})
ids = group.identifiers
all_ids = ids
doc['Created'] = group.data.created
doc['Updated'] = group.data.updated

doc['Identifier'] = [build_id_info(i) for i in ids]
doc['SearchIdentifier'] = [build_id_info(i) for i in all_ids]
Expand Down Expand Up @@ -142,6 +146,20 @@ def build_doc(
rel_meta = build_relationship_metadata(rel)
grouping = grouping or \
('identity' if rel.type == GroupType.Identity else 'version')

updated = None
if src_meta["Updated"] > trg_meta["Updated"]:
updated = src_meta["Updated"]
else:
updated = trg_meta["Updated"]

created = None
for relationship in rel.relationships:
if not created:
created = relationship.data.created
elif created > relationship.data.created:
created = relationship.data.created

return {
'_id': str(doc_uuid),
'_source': {
Expand All @@ -151,6 +169,8 @@ def build_doc(
"History": rel_meta,
"Source": src_meta,
"Target": trg_meta,
"Created": created,
"Updated": updated
},
}

Expand Down
6 changes: 0 additions & 6 deletions asclepias_broker/search/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ def search_factory(self, search, query_parser=None):
from invenio_records_rest.sorter import default_sorter_factory
search_index = search._index[0]

# TODO: make "scheme" optional?
for field in ('id', 'scheme', 'relation'):
if field not in request.values:
raise RESTValidationError(
errors=[FieldError(field, 'Required field.')])

search, urlkwargs = default_facets_factory(search, search_index)
search, sortkwargs = default_sorter_factory(search, search_index)
for key, value in sortkwargs.items():
Expand Down
38 changes: 24 additions & 14 deletions tests/api/test_relationships.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,38 @@
from asclepias_broker.events.api import EventAPI


def test_invalid_search_parameters(client):
def test_optional_search_parameters(client, db, es_clear):
search_url = url_for('invenio_records_rest.relid_list')

_process_events([
['A', 'Cites', 'X'],
['A', 'Cites', 'B']
])

params = {}
resp = client.get(search_url)
assert resp.status_code == 400
assert resp.json['message'] == 'Validation error.'
assert resp.json['errors'][0]['field'] == 'id'
assert resp.status_code == 200
assert resp.json['hits']['total'] == 2

params['id'] = 'some-id'
params['scheme'] = 'doi'
resp = client.get(search_url, query_string=params)
assert resp.status_code == 400
assert resp.json['message'] == 'Validation error.'
assert len(resp.json['errors']) == 1
assert resp.json['errors'][0]['field'] == 'scheme'
assert resp.status_code == 200
assert resp.json['hits']['total'] == 2

params['scheme'] = 'doi'
params['id'] = 'X'
resp = client.get(search_url, query_string=params)
assert resp.status_code == 400
assert resp.json['message'] == 'Validation error.'
assert len(resp.json['errors']) == 1
assert resp.json['errors'][0]['field'] == 'relation'
assert resp.status_code == 200
assert resp.json['hits']['total'] == 1


def test_invalid_search_parameters(client):
search_url = url_for('invenio_records_rest.relid_list')

params = {}
params['id'] = 'some-id'
resp = client.get(search_url, query_string=params)
assert resp.status_code == 200
assert resp.json['hits']['total'] == 0

params['relation'] = 'not-a-valid-value'
resp = client.get(search_url, query_string=params)
Expand Down