Actually i have a working code but the issue that i am facing is how to sort the result of the queryset based on multiple rule. This is my models.py :
class Node(MPTTModel):
parent = TreeForeignKey('self', on_delete=models.CASCADE, blank=True, null=True, related_name='children')
name = models.TextField(blank=True, null=True)`
viewed_by = models.ManyToManyField(CustomUser, related_name='viewed_by', blank=True)
bookmarked_by = models.ManyToManyField(CustomUser, related_name='bookmarked_by', blank=True)
thumbs_up = models.ManyToManyField(CustomUser, related_name='thumbs_up', blank=True)
In my views.py i have managed to queryset the database and show all the results based on all matching words, but the missing point here is that i have managed only to sort the result by the number of bookmarks. For i.e : i have this two objects :
Object 1 : name = How to use django forms ?
Object 2 : name = Using django forms for searching the database.
With object 1 is bookmarked by 20 users and Object 2 is bookmarked by 10 users and i type in my search bar : Using django forms database In the result i have the first object as the first answer shown in the list even if the second one have much more matchs with the searched keywords. So what i want to do here is to sort the result first based on the number of matching keywords and than sort it by number of bookmarks. This my view so far :
search_text_imported = request.session['import_search_text']
if search_text_imported != '':
result_list = []
get_result_list = [x for x in search_text_imported.split() if len(x) > 2]
for keyword in get_result_list:
tree_list = Node.objects.filter((Q(name__icontains=keyword) | Q(Tags__icontains=keyword)), tree_type='root', published=True ).annotate(num_bookmarks=Count('bookmarked_by')).order_by('-num_bookmarks')
result_list += list(tree_list)
result = list(OrderedDict.fromkeys(result_list))
context = {
'tree_result': result,
}
Please let me know if there is something missing here, any help will be much appreciated.