1
|
from json import JSONEncoder
|
2
|
from elasticsearch import Elasticsearch
|
3
|
from elasticsearch_dsl import *
|
4
|
|
5
|
import os
|
6
|
from os import path
|
7
|
|
8
|
|
9
|
def get_property():
|
10
|
|
11
|
|
12
|
f = open(path.join(os.path.dirname(os.path.realpath(__file__)), '../../api.properties'))
|
13
|
p = {}
|
14
|
for line in f:
|
15
|
data = line.strip().split("=")
|
16
|
p[data[0].strip()] = data[1].strip()
|
17
|
return p
|
18
|
|
19
|
|
20
|
def create_typology_filter(value):
|
21
|
return Q('match', typology=value)
|
22
|
|
23
|
|
24
|
def create_pid_type_filter(value):
|
25
|
args = {'localIdentifier.type': value}
|
26
|
return Q('nested', path='localIdentifier', query=Q('bool', must=[Q('match', **args)]))
|
27
|
|
28
|
|
29
|
def create_publisher_filter(value):
|
30
|
return Q('match', publisher=value)
|
31
|
|
32
|
|
33
|
def create_datasource_filter(value):
|
34
|
args = {'datasources.datasourceName': value}
|
35
|
return Q('nested', path='datasources', query=Q('bool', must=[Q('match', **args)]))
|
36
|
|
37
|
|
38
|
class DLIESResponseEncoder(JSONEncoder):
|
39
|
def default(self, o):
|
40
|
return o.__dict__
|
41
|
|
42
|
|
43
|
class DLIESResponse(object):
|
44
|
def __init__(self, facet=None, total=0, hits=[]):
|
45
|
if facet is None:
|
46
|
facet = dict(pid=[], typology=[], datasource=[])
|
47
|
self.facet = facet
|
48
|
self.total = total
|
49
|
self.hits = hits
|
50
|
|
51
|
|
52
|
class DLIESConnector(object):
|
53
|
def __init__(self):
|
54
|
props = get_property()
|
55
|
self.index_host = [x.strip() for x in props['es_index'].split(',')]
|
56
|
self.client = Elasticsearch(hosts=self.index_host)
|
57
|
self.index_name = props['api.index']
|
58
|
|
59
|
def simple_query(self, textual_query, start=None, end=None, user_filter=None):
|
60
|
s = Search(using=self.client, index=self.index_name).doc_type('object')
|
61
|
q = Q('match', _all=textual_query)
|
62
|
s.aggs.bucket('typologies', 'terms', field='typology')
|
63
|
s.aggs.bucket('all_datasources', 'nested', path='datasources').bucket('all_names', 'terms',
|
64
|
field='datasources.datasourceName')
|
65
|
s.aggs.bucket('all_publisher', 'terms', field='publisher')
|
66
|
|
67
|
filter_queries = []
|
68
|
if user_filter is not None and len(user_filter) > 0:
|
69
|
for f in user_filter.split('__'):
|
70
|
filter_key = f.split('_')[0]
|
71
|
filter_value = f.split('_')[1]
|
72
|
if filter_key == 'typology':
|
73
|
filter_queries.append(create_typology_filter(filter_value))
|
74
|
elif filter_key == 'datasource':
|
75
|
filter_queries.append(create_datasource_filter(filter_value))
|
76
|
elif filter_key == 'pidtype':
|
77
|
filter_queries.append(create_pid_type_filter(filter_value))
|
78
|
elif filter_key == 'publisher':
|
79
|
filter_queries.append(create_publisher_filter(filter_value))
|
80
|
|
81
|
if len(filter_queries) > 0:
|
82
|
s = s.query(q).filter(Q('bool', must=filter_queries))
|
83
|
else:
|
84
|
s = s.query(q)
|
85
|
|
86
|
s.aggs.bucket('all_pids', 'nested', path='localIdentifier').bucket('all_types', 'terms',
|
87
|
field='localIdentifier.type')
|
88
|
|
89
|
if start is not None:
|
90
|
if end is None:
|
91
|
end = start + 10
|
92
|
s = s[start:end]
|
93
|
response = s.execute()
|
94
|
|
95
|
hits = []
|
96
|
|
97
|
for index_result in response.hits:
|
98
|
hits.append(index_result.__dict__['_d_'])
|
99
|
|
100
|
pid_types = []
|
101
|
for tag in response.aggs.all_pids.all_types.buckets:
|
102
|
pid_types.append(dict(key=tag.key, count=tag.doc_count))
|
103
|
|
104
|
datasources = []
|
105
|
for tag in response.aggs.all_datasources.all_names.buckets:
|
106
|
datasources.append(dict(key=tag.key, count=tag.doc_count))
|
107
|
|
108
|
typologies = []
|
109
|
for tag in response.aggs.typologies.buckets:
|
110
|
typologies.append(dict(key=tag.key, count=tag.doc_count))
|
111
|
|
112
|
publishers = []
|
113
|
for tag in response.aggs.all_publisher.buckets:
|
114
|
if len(tag.key) > 0:
|
115
|
publishers.append(dict(key=tag.key, count=tag.doc_count))
|
116
|
|
117
|
return DLIESResponse(total=response.hits.total,
|
118
|
facet=dict(pid=pid_types, typology=typologies, datasource=datasources,
|
119
|
publishers=publishers), hits=hits)
|
120
|
|
121
|
def related_type(self, object_id, object_type, start=None):
|
122
|
args = {'target.objectType': object_type}
|
123
|
query_type = Q('nested', path='target', query=Q('bool', must=[Q('match', **args)]))
|
124
|
args_id = {'source.dnetIdentifier': object_id}
|
125
|
query_for_id = Q('nested', path='source', query=Q('bool', must=[Q('match', **args_id)]))
|
126
|
s = Search(using=self.client).index(self.index_name).doc_type('scholix').query(query_for_id & query_type)
|
127
|
if start:
|
128
|
s = s[start:start + 10]
|
129
|
|
130
|
response = s.execute()
|
131
|
hits = []
|
132
|
|
133
|
for index_hit in response.hits:
|
134
|
hits.append(index_hit.__dict__['_d_'])
|
135
|
|
136
|
return hits
|
137
|
|
138
|
def fix_collectedFrom(self, source, relation):
|
139
|
relSource = relation.get('source')
|
140
|
collectedFrom = relSource['collectedFrom']
|
141
|
for coll in collectedFrom:
|
142
|
for d in source['datasources']:
|
143
|
if d['datasourceName'] == coll['provider']['name']:
|
144
|
d['provisionMode'] = coll['provisionMode']
|
145
|
return source
|
146
|
|
147
|
def item_by_id(self, id, type=None, start=None):
|
148
|
try:
|
149
|
res = self.client.get(index=self.index_name, doc_type='object', id=id)
|
150
|
hits = []
|
151
|
input_source = res['_source']
|
152
|
related_publications = []
|
153
|
related_dataset = []
|
154
|
related_unknown = []
|
155
|
|
156
|
rel_source = None
|
157
|
if input_source.get('relatedPublications') > 0:
|
158
|
if 'publication' == type:
|
159
|
related_publications = self.related_type(id, 'publication', start)
|
160
|
else:
|
161
|
related_publications = self.related_type(id, 'publication')
|
162
|
rel_source = related_publications[0]
|
163
|
if input_source.get('relatedDatasets') > 0:
|
164
|
if 'dataset' == type:
|
165
|
related_dataset = self.related_type(id, 'dataset', start)
|
166
|
else:
|
167
|
related_dataset = self.related_type(id, 'dataset')
|
168
|
rel_source = related_dataset[0]
|
169
|
if input_source.get('relatedUnknown') > 0:
|
170
|
if 'unknown' == type:
|
171
|
related_unknown = self.related_type(id, 'unknown', start)
|
172
|
else:
|
173
|
related_unknown = self.related_type(id, 'unknown')
|
174
|
rel_source = related_unknown[0]
|
175
|
|
176
|
input_source = self.fix_collectedFrom(input_source, rel_source)
|
177
|
hits.append(input_source)
|
178
|
|
179
|
hits.append(dict(related_publications=related_publications, related_dataset=related_dataset,
|
180
|
related_unknown=related_unknown))
|
181
|
|
182
|
return DLIESResponse(total=1, hits=hits)
|
183
|
except Exception as e:
|
184
|
print "Error on getting item"
|
185
|
print e
|
186
|
return DLIESResponse()
|