Skip to content

Commit

Permalink
Merge pull request #487 from bounswe/semantic-search
Browse files Browse the repository at this point in the history
Semantic and Random search implementation
  • Loading branch information
hakanaktas0 authored Nov 26, 2023
2 parents 150ce8a + 331a0f2 commit dce8906
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
3 changes: 3 additions & 0 deletions project/backend/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,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'),


]
46 changes: 43 additions & 3 deletions project/backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ def post(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 = []
#
Expand All @@ -137,6 +137,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)
Expand All @@ -155,11 +156,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)
Expand All @@ -169,6 +172,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 = []
Expand Down Expand Up @@ -388,6 +419,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("keyword")
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)


@csrf_exempt
def delete_entry(request):
entry_id = request.POST.get("entry_id")
Expand Down

0 comments on commit dce8906

Please sign in to comment.