Revision 46775
Added by Sandro La Bruzzo about 7 years ago
DLIESConnector.py | ||
---|---|---|
1 | 1 |
from elasticsearch import * |
2 | 2 |
from elasticsearch_dsl import * |
3 | 3 |
from typing import List |
4 |
from swagger_server.models import Scholix, ObjectType, DSType, IdentifierType, RelationshipType, ObjectType,ObjectTypeObjectType, CreatorType |
|
4 |
from swagger_server.models import Scholix, ObjectType, DSType, IdentifierType, RelationshipType, ObjectType, \ |
|
5 |
ObjectTypeObjectType, CreatorType |
|
5 | 6 |
|
6 | 7 |
|
7 |
|
|
8 | 8 |
class DLIESConnector(object): |
9 | 9 |
def __init__(self, index_host, index_name='dli'): |
10 | 10 |
self.index_host = index_host |
... | ... | |
12 | 12 |
self.index_name = index_name |
13 | 13 |
|
14 | 14 |
def create_pid_pidType_query(self, pidType, pid): |
15 |
args = {'target.identifier.schema': pidType}
|
|
16 |
args2 = {'target.identifier.identifier': pid}
|
|
17 |
return Q('nested', path='target.identifier', query=Q('bool', must=[Q('match', **args), Q('match', **args2)]))
|
|
15 |
args = {'source.identifier.schema': pidType}
|
|
16 |
args2 = {'source.identifier.identifier': pid}
|
|
17 |
return Q('nested', path='source.identifier', query=Q('bool', must=[Q('match', **args), Q('match', **args2)]))
|
|
18 | 18 |
|
19 |
def create_target_pid_query(self, value):
|
|
20 |
args = {'target.identifier.identifier': value}
|
|
21 |
return Q('nested', path='target.identifier', query=Q('bool', must=[Q('match', **args)]))
|
|
19 |
def create_source_pid_query(self, value):
|
|
20 |
args = {'source.identifier.identifier': value}
|
|
21 |
return Q('nested', path='source.identifier', query=Q('bool', must=[Q('match', **args)]))
|
|
22 | 22 |
|
23 | 23 |
def create_typology_query(self, value): |
24 | 24 |
args = {'target.objectType': value} |
... | ... | |
30 | 30 |
|
31 | 31 |
def list_datasources(self): |
32 | 32 |
search_object = Search(using=self.client, index=self.index_name).doc_type('scholix').query() |
33 |
search_object.aggs.bucket('all_datasources', 'nested', path='linkprovider').bucket('all_names', 'terms',field='linkprovider.name', size=100) |
|
33 |
search_object.aggs.bucket('all_datasources', 'nested', path='linkprovider').bucket('all_names', 'terms', |
|
34 |
field='linkprovider.name', |
|
35 |
size=100) |
|
34 | 36 |
|
35 | 37 |
response = search_object.execute() |
36 |
|
|
37 | 38 |
return [item.key for item in response.aggs.all_datasources.all_names.buckets] |
38 | 39 |
|
39 |
|
|
40 | 40 |
def convertLinkProvider(self, lp): |
41 | 41 |
result = [] |
42 | 42 |
for item in lp: |
43 |
current_identifiers =[] |
|
44 |
for ids in item['identifiers']:
|
|
43 |
current_identifiers = []
|
|
44 |
for ids in item['identifiers']: |
|
45 | 45 |
current_identifiers.append(IdentifierType(identifier=ids['identifier'], schema=ids['schema'])) |
46 |
tmp = DSType(name=item['name'], identifiers=[IdentifierType(x['identifier'], x['schema']) for x in item['identifiers']] ) |
|
46 |
tmp = DSType(name=item['name'], |
|
47 |
identifiers=[IdentifierType(x['identifier'], x['schema']) for x in item['identifiers']]) |
|
47 | 48 |
result.append(tmp) |
48 | 49 |
return result |
49 | 50 |
|
50 | 51 |
def convertObject(self, inputObj): |
51 | 52 |
result = ObjectType() |
52 |
result.identifiers= [] |
|
53 |
result.identifiers = []
|
|
53 | 54 |
for item in inputObj.identifier: |
54 |
result.identifiers.append(IdentifierType(item.identifier,item.schema)) |
|
55 |
result.identifiers.append(IdentifierType(item.identifier, item.schema))
|
|
55 | 56 |
result.identifiers.append(IdentifierType(inputObj.dnetIdentifier, 'dnetIdentifier')) |
56 |
result.object_provider= self.convertLinkProvider([x.provider for x in inputObj.collectedFrom]) |
|
57 |
result.objectType = ObjectTypeObjectType(inputObj.objectType,'')
|
|
57 |
result.object_provider = self.convertLinkProvider([x.provider for x in inputObj.collectedFrom])
|
|
58 |
result.object_type = ObjectTypeObjectType(inputObj.objectType, '')
|
|
58 | 59 |
if 'title' in inputObj: |
59 | 60 |
result.title = inputObj.title |
60 | 61 |
if 'creator' in inputObj: |
61 | 62 |
result.creators = [CreatorType(x.name) for x in inputObj.creator] |
62 | 63 |
if 'publisher' in inputObj: |
63 | 64 |
if len(inputObj.publisher.name) > 0: |
64 |
result.publisher=[DSType(inputObj.publisher.name)]
|
|
65 |
result.publisher = [DSType(inputObj.publisher.name)]
|
|
65 | 66 |
return result |
66 | 67 |
|
67 |
def convertScholix(self,response): |
|
68 |
def convertScholix(self, response):
|
|
68 | 69 |
result = [] |
69 | 70 |
for item in response.hits: |
70 |
s = Scholix()
|
|
71 |
s.relationship= RelationshipType(item.relationship.name,item.relationship.schema,'')
|
|
71 |
s = Scholix() |
|
72 |
s.relationship = RelationshipType(item.relationship.name, item.relationship.schema, '')
|
|
72 | 73 |
s.link_provider = self.convertLinkProvider(item['linkprovider']) |
73 |
s.source= self.convertObject(item.source) |
|
74 |
s.target= self.convertObject(item.target) |
|
74 |
s.source = self.convertObject(item.source)
|
|
75 |
s.target = self.convertObject(item.target)
|
|
75 | 76 |
result.append(s) |
76 | 77 |
return result |
77 | 78 |
|
78 |
def realtionToPid(self, pid, pidType=None, page=0): |
|
79 |
def realtionToPid(self, pid, pidType=None, datasource=None, typology = None, page=0):
|
|
79 | 80 |
if pidType: |
80 |
query = self.create_pid_pidType_query( pidType, pid)
|
|
81 |
query = self.create_pid_pidType_query(pidType, pid) |
|
81 | 82 |
else: |
82 |
query = self.create_target_pid_query(pid) |
|
83 |
query = self.create_source_pid_query(pid) |
|
84 |
filters = [] |
|
85 |
if datasource and len(datasource): |
|
86 |
filters.append(self.create_dataSource_query(datasource)) |
|
87 |
if typology and len(typology): |
|
88 |
filters.append(self.create_typology_query(typology)) |
|
89 |
search_object = Search(using=self.client, index=self.index_name).doc_type('scholix').query(query) |
|
83 | 90 |
|
84 |
search_object = Search(using=self.client, index=self.index_name).doc_type('scholix').query(query) |
|
85 |
return self.convertScholix(search_object[page:page+10].execute()) |
|
91 |
if len(filters): |
|
92 |
search_object = search_object.filter(Q('bool', must=filters)) |
|
93 |
return self.convertScholix(search_object[page:page + 10].execute()) |
|
86 | 94 |
|
87 | 95 |
def realtionToTypology(self, typology, page=0): |
88 |
search_object = Search(using=self.client, index=self.index_name).doc_type('scholix').query(self.create_typology_query(typology)) |
|
89 |
return self.convertScholix(search_object[page:page+10].execute()) |
|
96 |
search_object = Search(using=self.client, index=self.index_name).doc_type('scholix').query( |
|
97 |
self.create_typology_query(typology)) |
|
98 |
return self.convertScholix(search_object[page:page + 10].execute()) |
|
90 | 99 |
|
91 | 100 |
def realtionFromDatasource(self, datasource, page=0): |
92 |
search_object = Search(using=self.client, index=self.index_name).doc_type('scholix').query(self.create_dataSource_query(datasource)) |
|
93 |
return self.convertScholix(search_object[page:page+10].execute()) |
|
101 |
search_object = Search(using=self.client, index=self.index_name).doc_type('scholix').query( |
|
102 |
self.create_dataSource_query(datasource)) |
|
103 |
return self.convertScholix(search_object[page:page + 10].execute()) |
Also available in: Unified diff
uploaded a new version of dli_api