From a5f4049002f054e3ac5555c2f00506261e4197e9 Mon Sep 17 00:00:00 2001 From: Akshatha Nayak Date: Wed, 26 May 2021 13:01:58 +0530 Subject: [PATCH] fix incorrect response when searching by id where id has spaces --- api/views.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/api/views.py b/api/views.py index 8bbabc2..82ba760 100644 --- a/api/views.py +++ b/api/views.py @@ -100,13 +100,17 @@ def detail(request, name, id): if name not in ALLOWED_INDICES: return HttpResponse("This index doesn't exist!\n") es = Elasticsearch([settings.NODE1, settings.NODE2]) - results = es.search(index=name, q="_id:{}".format(id)) - if results['hits']['total'] == 0: - results = es.search(index=name, q="alternativeId:{}".format(id), - doc_type="_doc") - if results['hits']['total'] == 0: - results = es.search(index=name, q="biosampleId:{}".format(id), - doc_type="_doc") + result = es.get(index=name, id=id) + if result['found']: + results = {'hits': {'hits': [result]}} + else: + results = es.search(index=name, q="_id:{}".format(id)) + if results['hits']['total'] == 0: + results = es.search(index=name, q="alternativeId:{}".format(id), + doc_type="_doc") + if results['hits']['total'] == 0: + results = es.search(index=name, q="biosampleId:{}".format(id), + doc_type="_doc") return JsonResponse(results)