5
5
from dataclasses import dataclass
6
6
import json
7
7
import requests
8
+ from linkml_dataops .query .query_model import AbstractQuery , FetchById
9
+ from linkml_dataops .query .queryengine import QueryEngine
8
10
9
11
from linkml_runtime .dumpers import json_dumper
10
12
from linkml_runtime .utils .formatutils import underscore
11
13
from linkml_runtime .linkml_model .meta import SchemaDefinition , ClassDefinition , YAMLRoot , ElementName , SlotDefinition , SlotDefinitionName
12
14
13
15
from linkml_solr .solrmodel import SolrEndpoint , SolrQuery , SolrQueryResult , RawSolrResult , FIELD
14
- from linkml_solr .solrschema import Transaction
15
16
from linkml_solr .solrschemagen import SolrSchemaGenerator
16
-
17
17
from linkml_solr .mapper import LinkMLMapper
18
18
19
19
# https://stackoverflow.com/questions/1176136/convert-string-to-python-class-object
@@ -25,17 +25,17 @@ def class_for_name(module_name, class_name):
25
25
return c
26
26
27
27
28
- # TODO: inherit from linkml_runtime_api.QueryEngine
29
28
@dataclass
30
- class SolrQueryEngine (object ):
29
+ class SolrQueryEngine (QueryEngine ):
31
30
"""
32
31
ORM wrapper for SOLR endpoint
33
32
"""
34
33
35
- endpoint : SolrEndpoint
36
- schema : SchemaDefinition
34
+ endpoint : SolrEndpoint = None
35
+ schema : SchemaDefinition = None
37
36
mapper : LinkMLMapper = None
38
37
discriminator_field : SlotDefinitionName = None
38
+ python_classes : List [Type [YAMLRoot ]] = None
39
39
40
40
def __post_init__ (self ):
41
41
# TODO: use schemaview
@@ -147,6 +147,33 @@ def fetch_object(self, row: Dict,
147
147
logging .debug (new_obj )
148
148
return cls (** new_obj )
149
149
150
+ def fetch_by_id (self , q : AbstractQuery ) -> YAMLRoot :
151
+ if isinstance (q , FetchById ):
152
+ tgt_classes = [c for c in self .python_classes if c .class_name == q .target_class ]
153
+ id_field = None
154
+ for c in self .schema .classes .values ():
155
+ if c .name == q .target_class :
156
+ for s in c .slots :
157
+ if self .schema .slots [s ].identifier :
158
+ id_field = s
159
+ if not id_field :
160
+ raise ValueError (f'No ID found for { q .target_class } ' )
161
+ params = {'target_class' : tgt_classes [0 ],
162
+ id_field : q .id }
163
+ result = self .search (** params )
164
+ return result .items
165
+
166
+ def simple_query (self , target_class : str , ** kwargs ) -> List [YAMLRoot ]:
167
+ tgt_classes = [c for c in self .python_classes if c .class_name == target_class ]
168
+ if len (tgt_classes ) != 1 :
169
+ raise ValueError (f'Class: { target_class } ' )
170
+ py_class = tgt_classes [0 ]
171
+ params = {k : v for k , v in kwargs .items () if v is not None }
172
+ result = self .search (target_class = py_class , ** params )
173
+ return result .items
174
+
175
+
176
+
150
177
def execute (self , query : SolrQuery ) -> RawSolrResult :
151
178
"""
152
179
Execute a solr query on endpoint
0 commit comments