-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreferred_term_controller.py
More file actions
136 lines (98 loc) · 4.74 KB
/
Copy pathpreferred_term_controller.py
File metadata and controls
136 lines (98 loc) · 4.74 KB
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
import json
import connexion
from swagger_server.models.approx_findby_name import ApproxFindbyName # noqa: E501
from swagger_server.models.error_model import ErrorModel # noqa: E501
from swagger_server.models.findby_name import FindbyName # noqa: E501
from swagger_server.models.name import Name # noqa: E501
from swagger_server import util
import config
from controllers.query_controller import *
def list_by_approx_name(lang: str, label: str): # noqa: E501
"""Search for a clinical entity by approximate preferred term
The result retrieves the list of clinical entity's ORPHAcode and its preferred term based on an approximate label search # noqa: E501
:param lang: Language
:type lang: str
:param label: Approximate preferred term of the clinical entity
:type label: str
:rtype: ApproxFindbyName
"""
es = config.elastic_server
index = "rdcode_orphanomenclature"
index = "{}_{}".format(index, lang.lower())
# print(label)
# Special FUZZY MATCH query
query_term_list = []
for term in label.strip().replace("-", " ").split(" "):
query_term = "{{\"query_string\": {{\"default_field\": \"Preferred term\", \"query\": \"*{}*\"}}}}".format(term)
query_term += ", {{\"fuzzy\": {{\"Preferred term\": {{\"value\" :\"{}\", \"fuzziness\": \"AUTO\"}}}}}}".format(term)
query_term_list.append(query_term)
# query_term_list.append(additional_query_term)
query_term_list = "[" + ", ".join(query_term_list) + "]"
# query_term = {
# "query_string": {
# "default_field" : "Preferred term",
# "query": "{}".format(term)
# }
# }
# print(json.dumps(query_term_list, indent=2))
# print(query_term_list)
query = "{\"query\": {\"bool\": {\"should\": " + query_term_list + "}}" + \
",\"_source\":[\"Date\", \"ORPHAcode\", \"Preferred term\"]}"
# print(query, flush=True)
response = multiple_res(es, index, query, 10000)
# return yaml if needed
response = if_yaml(connexion.request.accept_mimetypes.best, response)
return response
def search_by_name(lang, label): # noqa: E501
"""Search for a clinical entity by preferred term
The result retrieves the clinical entity's ORPHAcode and its preferred term. # noqa: E501
:param lang: Language
:type lang: str
:param label: Preferred term of the clinical entity
:type label: str
:rtype: FindbyName
"""
es = config.elastic_server
index = "rdcode_orphanomenclature"
index = "{}_{}".format(index, lang.lower())
# Special EXACT MATCH query with keyword
# query = "{\"query\": {\"fuzzy\": {\"Preferred term\": {\"value\": " + "\"*{}*\"".format(label) + ", \"fuzziness\":\"AUTO\"}}}," \
# "\"_source\":[\"Date\", \"ORPHAcode\", \"Preferred term\"]}"
query_term_list = []
for term in label.strip().replace("-", " ").split(" "):
query_term = "{{\"query_string\": {{\"default_field\": \"Preferred term\", \"query\": \"*{}*\"}}}}".format(term)
query_term += ", {{\"fuzzy\": {{\"Preferred term\": {{\"value\" :\"{}\", \"fuzziness\": \"AUTO\", \"max_expansions\" : 1}}}}}}".format(term)
query_term_list.append(query_term)
# query_term_list.append(additional_query_term)
query_term_list = "[" + ", ".join(query_term_list) + "]"
query = "{\"query\": {\"bool\": {\"should\": " + query_term_list + "}}" + \
",\"_source\":[\"Date\", \"ORPHAcode\", \"Preferred term\"]}"
<<<<<<< HEAD
# print("Searching: ", label, flush=True)
# print("Query: ", query, flush=True)
=======
print("Searching: ", label, flush=True)
print("Query: ", query, flush=True)
>>>>>>> 2ecd9ed0ddf2ad90574c15bb3816a450938d16df
response = single_res(es, index, query)
# return yaml if needed
response = if_yaml(connexion.request.accept_mimetypes.best, response)
return response
def list_name(lang, orphacode): # noqa: E501
"""Search for a clinical entity's preferred term by ORPHAcode
The result retrieves the clinical entity's ORPHAcode and its preferred term. # noqa: E501
:param lang: Language
:type lang: str
:param orphacode: A unique and time-stable numerical identifier attributed randomly by the Orphanet database to each clinical entity upon its creation.
:type orphacode: int
:rtype: Name
"""
es = config.elastic_server
index = "rdcode_orphanomenclature"
index = "{}_{}".format(index, lang.lower())
query = "{\"query\": {\"match\": {\"ORPHAcode\": " + str(orphacode) + "}}," \
"\"_source\":[\"Date\", \"ORPHAcode\", \"Preferred term\"]}"
response = single_res(es, index, query)
# return yaml if needed
response = if_yaml(connexion.request.accept_mimetypes.best, response)
return response