From: Charles Connell Date: Fri, 3 Jan 2014 21:10:02 +0000 (-0500) Subject: Scoring of search results X-Git-Tag: release-20150131~314 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=81b95ea789e08200ffd2705e8c088d01caaae4b3;p=oweals%2Fkarmaworld.git Scoring of search results --- diff --git a/karmaworld/apps/notes/search.py b/karmaworld/apps/notes/search.py index 856a84a..3908180 100644 --- a/karmaworld/apps/notes/search.py +++ b/karmaworld/apps/notes/search.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # -*- coding:utf8 -*- # Copyright (C) 2013 FinalsClub Foundation +import calendar import time import indextank.client as itc @@ -20,6 +21,12 @@ index = api_client.get_index(secret.INDEX) while not index.has_started(): time.sleep(0.5) +# Default scoring function +# Results are sorted by combination of "relevance" +# and number of thanks they have received. +# "Relevance" is a black box provided by IndexDen. +index.add_function(0, 'relevance * log(doc.var[0])') + def note_to_dict(note): d = { 'name': note.name, @@ -32,13 +39,16 @@ def note_to_dict(note): if note.course: d['course_id'] = note.course.id + if note.uploaded_at: + d['timestamp'] = calendar.timegm(note.uploaded_at.timetuple()) + return d def add_document(note): if note.text: - index.add_document(note.id, note_to_dict(note)) + index.add_document(note.id, note_to_dict(note), variables={0: note.thanks}) else: - logger.warn("Note {n} has no text, will not add to IndexDen".format(n=note)) + logger.warn("Note {n} has no text or thanks value, will not add to IndexDen".format(n=note)) def remove_document(note): index.delete_document(note.id) @@ -51,7 +61,7 @@ def search(query, course_id=None): else: real_query = '"%s" OR name:"%s"' % (query, query) - raw_results = index.search(real_query, snippet_fields=['text']) + raw_results = index.search(real_query, scoring_function=0, snippet_fields=['text']) results = {r['docid']: r['snippet_text'] for r in raw_results['results']}