diff --git a/gui/src/components/api.js b/gui/src/components/api.js index e6f45d1499810fff2e7431390d9f483d17edffd4..3d4019b54ab72aaa7f17e19b3bbc919b977a2d04 100644 --- a/gui/src/components/api.js +++ b/gui/src/components/api.js @@ -384,7 +384,9 @@ class Api { async search(search) { this.onStartLoading() return this.swagger() - .then(client => client.apis.repo.search(search)) + .then(client => client.apis.repo.search({ + exclude: ['atoms', 'only_atoms', 'files', 'quantities', 'optimade', 'labels', 'geometries'], + ...search})) .catch(handleApiError) .then(response => response.body) .finally(this.onFinishLoading) diff --git a/nomad/app/api/repo.py b/nomad/app/api/repo.py index e8a3ed4b77d5e40e338b993621755cd1b9d47bf9..04900ddd6ccd6dba6ad0ea2e961065364c2fcf39 100644 --- a/nomad/app/api/repo.py +++ b/nomad/app/api/repo.py @@ -84,6 +84,8 @@ _search_request_parser.add_argument( 'Possible values are %s.' % ', '.join(datamodel.Domain.instance.metrics_names))) _search_request_parser.add_argument( 'statistics', type=bool, help=('Return statistics.')) +_search_request_parser.add_argument( + 'exclude', type=str, action='split', help='Excludes the given keys in the returned data.') for group_name in search.groups: _search_request_parser.add_argument( group_name, type=bool, help=('Return %s group data.' % group_name)) @@ -150,8 +152,9 @@ class RepoCalcsResource(Resource): """ try: + parsed_args = _search_request_parser.parse_args() args = { - key: value for key, value in _search_request_parser.parse_args().items() + key: value for key, value in parsed_args.items() if value is not None} scroll = args.get('scroll', False) @@ -202,6 +205,9 @@ class RepoCalcsResource(Resource): elif len(metrics) > 0: search_request.totals(metrics_to_use=metrics) + if 'exclude' in parsed_args: + search_request.exclude(*parsed_args['exclude']) + try: if scroll: results = search_request.execute_scrolled(scroll_id=scroll_id, size=per_page) diff --git a/nomad/search.py b/nomad/search.py index 859308bcebe58f83ad7254b657b012a46034f4cd..f44a12d4db402091ca1363af567bb46c54a26b13 100644 --- a/nomad/search.py +++ b/nomad/search.py @@ -543,6 +543,11 @@ class SearchRequest: return self + def exclude(self, *args): + """ Exclude certain elastic keys from the search results. """ + self._search = self._search.source(excludes=args) + return self + def execute(self): """ Exectutes without returning actual results. Only makes sense if the request diff --git a/tests/app/test_api.py b/tests/app/test_api.py index 8da6e5e8feedc9709f2a9bb2746a81b2bc626c5a..4ef6deb3acc34f19045ff42add31ac14b352b489 100644 --- a/tests/app/test_api.py +++ b/tests/app/test_api.py @@ -881,6 +881,14 @@ class TestRepo(): assert len(statistics['system']) == 1 assert value in statistics['system'] + def test_search_exclude(self, api, example_elastic_calcs, no_warn): + rv = api.get('/repo/?exclude=atoms,only_atoms') + assert rv.status_code == 200 + result = json.loads(rv.data)['results'][0] + assert 'atoms' not in result + assert 'only_atoms' not in result + assert 'basis_set' in result + metrics_permutations = [[], search.metrics_names] + [[metric] for metric in search.metrics_names] def test_search_admin(self, api, example_elastic_calcs, no_warn, admin_user_auth): diff --git a/tests/test_search.py b/tests/test_search.py index c131e57a5e5d09112a9a153acb4f9d79875b0ec9..e538e84cb3f0025f3ad735950bddf9666b170839 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -137,6 +137,14 @@ def test_search_totals(elastic, example_search_data): assert 'quantities' not in results +def test_search_excludes(elastic, example_search_data): + for item in SearchRequest().execute_paginated()['results']: + assert 'atoms' in item + + for item in SearchRequest().exclude('atoms').execute_paginated()['results']: + assert 'atoms' not in item + + @pytest.mark.parametrize("order_by", [None, 'upload_id']) def test_search_quantity( elastic, normalized: parsing.LocalBackend, test_user: datamodel.User,