From c48e1b2424c5f831a97719d6a490341953d571cb Mon Sep 17 00:00:00 2001 From: hakanaktas0 <104701041+hakanaktas0@users.noreply.github.com> Date: Tue, 21 Nov 2023 19:43:32 +0300 Subject: [PATCH 1/3] Update views.py --- project/backend/api/views.py | 37 +++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/project/backend/api/views.py b/project/backend/api/views.py index 879a81d4..85b43ed9 100644 --- a/project/backend/api/views.py +++ b/project/backend/api/views.py @@ -74,13 +74,13 @@ def get(self, request): def search(request): search = request.GET.get("query") search_type = request.GET.get("type") - if search == None or search == "": + if (search == None or search == "") and search_type != 'random': return JsonResponse({'status': 'Title to search must be given.'}, status=400) if search_type == None or search_type == "": return JsonResponse({'status': 'Type to search must be given.'}, status=400) - if search_type != 'node' and search_type != 'author' and search_type != 'all' and search_type != 'by': + if search_type != 'node' and search_type != 'author' and search_type != 'all' and search_type != 'by'and search_type != 'random' and search_type != 'semantic': return JsonResponse({'status': 'invalid search type.'}, status=400) - search_elements = search.split() + # similars = [] # TODO ADVANCED SEARCH # also_sees = [] # @@ -99,6 +99,7 @@ def search(request): if search_type == 'by' or search_type == 'all': # print(search_elements) + search_elements = search.split() for el in search_elements: res_name = User.objects.filter(first_name__icontains=el) res_surname = User.objects.filter(last_name__icontains=el) @@ -117,11 +118,13 @@ def search(request): contributors = [] if search_type == 'node' or search_type == 'all': + search_elements = search.split() for el in search_elements: res = Node.objects.annotate(search=SearchVector("node_title")).filter(node_title__icontains=el) for e in res: nodes.append(e.node_id) if search_type == 'author' or search_type == 'all': # TODO This method is too inefficient + search_elements = search.split() for el in search_elements: res_name = User.objects.filter(first_name__icontains=el) res_surname = User.objects.filter(last_name__icontains=el) @@ -131,6 +134,34 @@ def search(request): for e in res_surname: if Contributor.objects.filter(user_id=e.id).count() != 0: contributors.append(e.username) + if search_type == 'semantic': + wid = search + tag = SemanticTag.objects.filter(wid=wid) + if tag.count() == 0: + return JsonResponse({'message': 'No tag with this wid is found'}, status=404) + tag = tag[0] + nodes_q = tag.nodes() + related_nodes_q = tag.related_nodes() + for node in nodes_q: + nodes.append(node.node_id) + for rel_node in related_nodes_q: + nodes.append(rel_node.node_id) + + if search_type == 'random': + count = Node.objects.count() + prev = [] + if count < 20: + c = count + else: + c = 20 + i = 0 + while i < c: + ran = random.randint(0,count-1) + if ran not in prev: + prev.append(ran) + random_node = Node.objects.all()[ran] + nodes.append(random_node.node_id) + i += 1 contributors = list(set(contributors)) nodes = list(set(nodes)) res_authors = [] From 061a81d52b1c0c8ebab8884f195627c9929aafa4 Mon Sep 17 00:00:00 2001 From: hakanaktas0 <104701041+hakanaktas0@users.noreply.github.com> Date: Sun, 26 Nov 2023 14:29:05 +0300 Subject: [PATCH 2/3] semantic search suggestion is added --- project/backend/api/urls.py | 3 +++ project/backend/api/views.py | 13 +++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/project/backend/api/urls.py b/project/backend/api/urls.py index 5cb5f0aa..e1afe57d 100644 --- a/project/backend/api/urls.py +++ b/project/backend/api/urls.py @@ -30,4 +30,7 @@ path('send_collab_req/', send_collaboration_request, name='send_col_req'), path('update_req/', update_request_status, name='update_req'), path('send_rev_req/', send_review_request, name='send_rev_req'), + path('get_semantic_suggestion/', get_semantic_suggestion, name='get_semantic_suggestion'), + + ] diff --git a/project/backend/api/views.py b/project/backend/api/views.py index 85b43ed9..91d60eea 100644 --- a/project/backend/api/views.py +++ b/project/backend/api/views.py @@ -140,8 +140,8 @@ def search(request): if tag.count() == 0: return JsonResponse({'message': 'No tag with this wid is found'}, status=404) tag = tag[0] - nodes_q = tag.nodes() - related_nodes_q = tag.related_nodes() + nodes_q = tag.nodes + related_nodes_q = tag.related_nodes for node in nodes_q: nodes.append(node.node_id) for rel_node in related_nodes_q: @@ -382,6 +382,15 @@ def get_workspace_from_id(request): 'references':references, 'created_at':workspace.created_at, }, status=200) + +def get_semantic_suggestion(request): + search = request.GET.get("query") + result = SemanticTag.existing_search_results(search) + if len(result) == 0: + return JsonResponse({'message': 'There are no nodes with this semantic tag.'}, status=404) + return JsonResponse({'suggestions': result}, status=200) + + def delete_entry(request): id = int(request.GET.get("entry_id")) entry = Entry.objects.filter(entry_id=id) From 8d38b49c926a2cb88fae533658c769eb57274356 Mon Sep 17 00:00:00 2001 From: hakanaktas0 <104701041+hakanaktas0@users.noreply.github.com> Date: Sun, 26 Nov 2023 14:51:29 +0300 Subject: [PATCH 3/3] Update views.py --- project/backend/api/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/backend/api/views.py b/project/backend/api/views.py index 91d60eea..27ee0cbf 100644 --- a/project/backend/api/views.py +++ b/project/backend/api/views.py @@ -384,7 +384,7 @@ def get_workspace_from_id(request): }, status=200) def get_semantic_suggestion(request): - search = request.GET.get("query") + search = request.GET.get("keyword") result = SemanticTag.existing_search_results(search) if len(result) == 0: return JsonResponse({'message': 'There are no nodes with this semantic tag.'}, status=404)