elasticsearch.py 8.09 KB
Newer Older
1
from typing import Tuple, List, Union, Dict, Set
2
3
4
5
6
7
8
9
10
11
12
from fastapi import HTTPException
from elasticsearch_dsl import Search, Q

from optimade.filterparser import LarkParser
from optimade.server.entry_collections import EntryCollection
from optimade.server.query_params import EntryListingQueryParams, SingleEntryQueryParams
from optimade.server.exceptions import BadRequest
from optimade.server.mappers import StructureMapper
from optimade.models import StructureResource

from nomad import config, datamodel, files, search, utils
13
14
15
from nomad.normalizing.optimade import (
    optimade_chemical_formula_reduced, optimade_chemical_formula_anonymous,
    optimade_chemical_formula_hill)
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

from .filterparser import _get_transformer as get_transformer


logger = utils.get_logger(__name__)


class ElasticsearchStructureCollection(EntryCollection):

    def __init__(self):
        super().__init__(
            collection=config.elastic.index_name,
            resource_cls=StructureResource,
            resource_mapper=StructureMapper,
            transformer=get_transformer(nomad_properties='dft', without_prefix=False))

        self.parser = LarkParser(version=(1, 0, 0), variant="default")

        # check aliases do not clash with mongo operators
        self._check_aliases(self.resource_mapper.all_aliases())
        self._check_aliases(self.resource_mapper.all_length_aliases())

        self.client = Search

    def _base_search_request(self):
        request = search.SearchRequest().owner('public', None)
        request.search_parameter('processed', True)
        # TODO use the elastic annotations when done
        request.query(Q('exists', field='dft.optimade.elements'))
        return request

    def __len__(self) -> int:
        # TODO cache
        return self._base_search_request().execute()['total']

    def count(self, **kwargs) -> int:
        # This seams solely mongodb specific
        raise NotImplementedError()

    def find(
            self,
            params: Union[EntryListingQueryParams, SingleEntryQueryParams]) \
            -> Tuple[List[StructureResource], int, bool, set]:

        criteria = self.handle_query_params(params)

        all_fields = criteria.pop("fields")
        if getattr(params, "response_fields", False):
            fields = set(params.response_fields.split(","))
            fields |= self.resource_mapper.get_required_fields()
        else:
            fields = all_fields.copy()

        sort, order = criteria.get('sort', (('chemical_formula_reduced', 1),))[0]
        sort_quantity = datamodel.OptimadeEntry.m_def.all_quantities.get(sort, None)
        if sort_quantity is None:
            raise BadRequest(detail='Unable to sort on field %s' % sort)
        sort_quantity_a_optimade = sort_quantity.m_get_annotations('optimade')
        if not sort_quantity_a_optimade.sortable:
            raise BadRequest(detail='Unable to sort on field %s' % sort)

        search_request = self._base_search_request().include('calc_id', 'upload_id')

        filter = criteria['filter']
        if filter is None:
            filter_param = getattr(params, 'filter')
            logger.error('could not parse optimade filter', filter=filter_param)
            raise NotImplementedError(
                'some features used in filter query %s are not implemented' % filter_param)
85
86

        if filter != {}:
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
            search_request.query(filter)

        es_response = search_request.execute_paginated(
            page_offset=criteria.get('skip', 0),
            per_page=criteria['limit'],
            order=order,
            order_by='dft.optimade.%s' % sort)
        results = self._es_to_optimade_results(es_response['results'], response_fields=fields)

        nresults_now = len(results)
        if isinstance(params, EntryListingQueryParams):
            data_returned = es_response['pagination']['total']
            more_data_available = data_returned >= criteria.get('skip', 0) + criteria['limit']
        else:
            # SingleEntryQueryParams, e.g., /structures/{entry_id}
            data_returned = nresults_now
            more_data_available = False
            if nresults_now > 1:
                raise HTTPException(
                    status_code=404,
107
                    detail=f'Instead of a single entry, {nresults_now} entries were found')
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
            results = results[0] if results else None

        return results, data_returned, more_data_available, all_fields - fields

    def _check_aliases(self, aliases):
        pass

    def _es_to_optimade_result(
            self, es_result: dict,
            response_fields: Set[str],
            upload_files_cache: Dict[str, files.UploadFiles]) -> StructureResource:

        calc_id, upload_id = es_result['calc_id'], es_result['upload_id']
        upload_files = upload_files_cache.get(upload_id)

        if upload_files is None:
            upload_files = files.UploadFiles.get(upload_id, is_authorized=lambda: True)
            if upload_files is None:
                logger.error('missing upload', upload_id=upload_id)
                return None

            upload_files_cache[upload_id] = upload_files

        try:
            archive = upload_files.read_archive(calc_id)
        except KeyError:
            logger.error('missing archive entry', upload_id=upload_id, calc_id=calc_id)
            return None

        metadata = archive[calc_id]['section_metadata'].to_dict()
        entry = datamodel.EntryMetadata.m_from_dict(metadata)

        def include(key):
            return response_fields is None or (key in response_fields) or not key.startswith('_')

        attrs = {
            key: value
            for key, value in entry.dft.optimade.m_to_dict().items()
            if include(key)}

        if include('immutable_id'):
            attrs['immutable_id'] = calc_id
        if include('last_modified'):
            attrs['last_modified'] = entry.last_processing if entry.last_processing is not None else entry.upload_time

153
        # TODO this should be removed, once all data is reprocessed with the right normalization
154
155
        attrs['chemical_formula_reduced'] = optimade_chemical_formula_reduced(
            attrs['chemical_formula_reduced'])
156
157
158
159
160
        attrs['chemical_formula_anonymous'] = optimade_chemical_formula_anonymous(
            attrs['chemical_formula_reduced'])
        attrs['chemical_formula_hill'] = optimade_chemical_formula_hill(
            attrs['chemical_formula_hill'])
        attrs['chemical_formula_descriptive'] = attrs['chemical_formula_hill']
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
        dimension_types = attrs['dimension_types']
        if isinstance(dimension_types, int):
            attrs['dimension_types'] = [1] * dimension_types + [0] * (3 - dimension_types)
            attrs['nperiodic_dimensions'] = dimension_types
        elif isinstance(dimension_types, list):
            attrs['nperiodic_dimensions'] = sum(dimension_types)

        if response_fields is not None:
            for request_field in response_fields:
                if not request_field.startswith('_nmd_'):
                    continue

                try:
                    if request_field.startswith('_nmd_dft_'):
                        attrs[request_field] = getattr(entry.dft, request_field[9:])
                    else:
                        attrs[request_field] = getattr(entry, request_field[5:])
                except AttributeError:
                    # if unknown properties where provided, we will ignore them
                    pass

        return self.resource_cls(
            type='structures',
            id=entry.calc_id,
            attributes=attrs,
            relationships=None)

    def _es_to_optimade_results(self, es_results: List[dict], response_fields: Set[str]):
        upload_files_cache: Dict[str, files.UploadFiles] = {}
        optimade_results = []
        try:
            for es_result in es_results:
                optimade_result = self._es_to_optimade_result(
                    es_result, response_fields, upload_files_cache)
                if optimade_result is not None:
                    optimade_results.append(optimade_result)
        finally:
            for upload_files in upload_files_cache.values():
                upload_files.close()

        return optimade_results