-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScooner-API-03172018.py
executable file
·140 lines (88 loc) · 3.94 KB
/
Scooner-API-03172018.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
import json
from bottle import run, get, template, route, post, request, put
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search
#####################################################################
@route('/scooner-pubmed/_doc/_key', method='POST') # decorator for POST method(keyword searching)
def search_by_keywords():
"""
Use POST instead of GET as that is
what the Tomcat server is looking for
"""
keywords = request.forms.get('keywords') #to accept parameter value from a user
page = request.forms.get('page')
skip = ((int(page)-1)*10)
client = Elasticsearch()
response = client.search(
index="scooner-pubmed",
body=template('abstract_template', keywords=keywords, skip=skip) #bottle template following elasticsearch_dsl format----look at 'abstract_template.tpl' file
)
results = []
for hit in response['hits']['hits']:
r = hit['_source']
r['id'] = hit['_id']
results.append(r)
return {'results' : results}
#########################################################################
@route('/scooner-pubmed/_doc/_ids', method='POST')
def search_by_id():
pmids = request.forms.get('pmids') # comma seperated list of _id terms
client = Elasticsearch()
response = client.search(
index="scooner-pubmed",
body=template('ID_query', pmids=pmids) #bottle template following elasticsearch_dsl format----look at 'ID_query.tpl' file
)
results = []
for hit in response['hits']['hits']:
r = hit['_source']
r['id'] = hit['_id']
results.append(r)
return {'results' : results}
#########################################################################
@route('/scooner-pubmed/_doc/_id')
def search_by_id():
id = request.query.id
client = Elasticsearch()
response = client.search(
index="scooner-pubmed",
body=template('ID_query', id = id) #bottle template following elasticsearch_dsl format----look at 'ID_query.tpl' file
)
results = []
for hit in response['hits']['hits']:
results.append(hit['_source'])
return {'results' : results}
########################################################################
@route('/scooner-pubmed/_doc/_minor')
def search_minor():
minor = request.query.minor
client = Elasticsearch()
response = client.search(
index="scooner-pubmed",
body=template('MeSH_Minor', minor = minor) #bottle template following elasticsearch_dsl format----look at 'MeSH_Minor.tpl' file
)
results = []
for hit in response['hits']['hits']:
results.append(hit['_source'])
return {'results' : results}
#########################################################################
@route('/scooner-pubmed/_doc/_major')
def search_major():
major = request.query.major
client = Elasticsearch()
response = client.search(
index="scooner-pubmed",
body=template('MeSH_Major', major = major) #bottle template following elasticsearch_dsl format----look at 'MeSH_Major.tpl' file
)
results = []
for hit in response['hits']['hits']:
results.append(hit['_source'])
return {'results' : results}
#########################################################################
@route('/scooner-pubmed/_doc/search', method='POST') #decorator for POST method
def search():
query = template('abstract_template', keyword=request.json['keyword'])
search = Search.from_dict(json.loads(query))
print(search.to_dict())
return search.to_dict()
#########################################################################
run(host='localhost', port=8088, debug=True) #socket to listen on