1
|
import urllib2
|
2
|
|
3
|
import pysolr
|
4
|
from flask import json
|
5
|
|
6
|
from DLI_ObjectResolver import DLIObject
|
7
|
|
8
|
|
9
|
class QueryResolver(object):
|
10
|
def __init__(self, solr_url, base_url_dnet):
|
11
|
self.solr_url = solr_url
|
12
|
self.solr_server = pysolr.Solr(self.solr_url)
|
13
|
self.base_url_dnet = base_url_dnet
|
14
|
self.facet_renaming = {'entitytype': "Typology", 'provenance': "Content Provider",
|
15
|
"Content Provider": 'provenance', "Typology": 'entitytype'}
|
16
|
|
17
|
def get_facet(self, facet, query=None):
|
18
|
data = []
|
19
|
|
20
|
f = 'entitytype'
|
21
|
if query is not None:
|
22
|
data.append({'facet': self.facet_renaming[f], 'query_name': f, 'values': [
|
23
|
{'key': facet[f][i], 'value': facet[f][i + 1]} for i in range(0, len(facet[f]), 2)]})
|
24
|
else:
|
25
|
data.append({'facet': self.facet_renaming[f], 'query_name': f,
|
26
|
'values': [{'key': facet[f][i], 'value': facet[f][i + 1]} for i in
|
27
|
range(0, len(facet[f]), 2)]})
|
28
|
f = 'provenance'
|
29
|
data.append({'facet': self.facet_renaming[f], 'query_name': f,
|
30
|
'values': [{'key': facet[f][i], 'value': facet[f][i + 1]} for i in range(0, len(facet[f]), 2)]})
|
31
|
|
32
|
return data
|
33
|
|
34
|
def get_item(self, persistent_identifier):
|
35
|
query = 'objidentifier:"%s"' % persistent_identifier
|
36
|
response = self.solr_server.search(q=query)
|
37
|
query_response = []
|
38
|
for hit in response:
|
39
|
query_response.append(DLIObject(hit['dnetresourceidentifier'], self.base_url_dnet))
|
40
|
|
41
|
return {'objects': query_response}
|
42
|
|
43
|
def get_stats_detail(self):
|
44
|
data = urllib2.urlopen(self.base_url_dnet + "/mvc/ui/stats/getLastStats")
|
45
|
result = []
|
46
|
d = json.loads(data.read())
|
47
|
for k in d:
|
48
|
if 'Deduction' not in k['datasource']:
|
49
|
result.append(k)
|
50
|
return result
|
51
|
|
52
|
def get_stats(self):
|
53
|
|
54
|
data = urllib2.urlopen(self.base_url_dnet + "/mvc/ui/stats/getAllStats")
|
55
|
result = {}
|
56
|
d = json.loads(data.read())
|
57
|
for item in d:
|
58
|
result[item] = []
|
59
|
for k in d[item]:
|
60
|
if 'Deduction' not in k['datasource']:
|
61
|
result[item].append(k)
|
62
|
return result
|
63
|
|
64
|
def query(self, q, browse_fields, start=None, page_size=10, only_result=False, filter_query=None):
|
65
|
params = {}
|
66
|
if browse_fields is not None:
|
67
|
params['facet'] = 'true',
|
68
|
params['facet.field'] = browse_fields
|
69
|
params['facet.mincount'] = 1
|
70
|
|
71
|
if filter_query is not None:
|
72
|
params['fq'] = filter_query
|
73
|
if start is not None:
|
74
|
params['start'] = start
|
75
|
response = self.solr_server.search(q, rows=page_size, **params)
|
76
|
query_response = []
|
77
|
for hit in response:
|
78
|
dnet_id = hit['dnetresourceidentifier']
|
79
|
|
80
|
query_response.append(DLIObject(hit['dnetresourceidentifier'], self.base_url_dnet))
|
81
|
if not only_result:
|
82
|
facet = self.get_facet(response.facets['facet_fields'], q)
|
83
|
else:
|
84
|
facet = self.get_facet(response.facets['facet_fields'])
|
85
|
return {'objects': query_response, 'facet': facet, 'total': response.hits}
|
86
|
|
87
|
def get_ds_info(self, id, name):
|
88
|
param = ""
|
89
|
value = ""
|
90
|
if (len(id) > 0):
|
91
|
param = "id"
|
92
|
value = id
|
93
|
if len(name) > 0:
|
94
|
param = "name"
|
95
|
value = name.replace(" ", '%20')
|
96
|
url_query = self.base_url_dnet + "/mvc/ui/dli/datasource/info.do?%s=%s" % (param, value)
|
97
|
data = urllib2.urlopen(url_query)
|
98
|
d = json.loads(data.read())
|
99
|
return d
|
100
|
|
101
|
def get_all_ds(self):
|
102
|
data = urllib2.urlopen(self.base_url_dnet + "/mvc/ui/dli/datasource/datasources.do")
|
103
|
d = json.loads(data.read())
|
104
|
return d
|