I have been trying to search and retrieve results based on 3 user input values and find the closest value that satisfies these three conditions. The doc has about 30 million rows and the query takes about 10 seconds to execute.
def sgrna(factor, efficiency, specificity, chr):
es = Elasticsearch()
minimum = min(factor)
request = []
min(factor)
for i in range(len(chr)):
req_head = {'index': 'lab', 'type': 'sg_rna'}
req_body = {
"query": {
"bool": {
"must": [
{
"term": {
"BS_CHR": chr[i]
}
},
{
"range": {
"Efficiency": {
"gte": int(efficiency)
}
}
},
{
"range": {
"Specificity": {
"gte": int(specificity)
}
}
} ]
}
}
,
"sort" : {
"_script" : {
"type" : "number",
"script" : {
"lang": "painless",
"params": {
"factor": int(factor[i])
},
"inline": "def cur = 0; cur = (params.factor - doc['BS_START'].value); if (cur < 0) { cur = cur * -1 } else { cur = cur}" },
"order" : "asc"
}
}
}
request.extend([req_head, req_body])
resp = es.msearch(body=request)
print(resp)
response = []
for i in range(len(resp["responses"])):
response.append(resp['responses'][i]['hits']["hits"][0]["_source"])
return response