]> git.openstreetmap.org Git - osqa.git/blobdiff - forum_modules/mysqlfulltext/startup.py
Resolves Jira OSQA-733, we encode the unicode data according to the ASCII XML Unicode...
[osqa.git] / forum_modules / mysqlfulltext / startup.py
index b1031a532bccf8519a12f50159155deec9f05483..8c2af5fadb488a6e10e70cecce6142fa42e4d532 100644 (file)
@@ -8,8 +8,17 @@ from forum.models.question import Question, QuestionManager
 from forum.models.node import Node
 from forum.modules import decorate
 
 from forum.models.node import Node
 from forum.modules import decorate
 
+VERSION = 4
+
+f_name = None
+
 if not bool(settings.MYSQL_FTS_INSTALLED):
 if not bool(settings.MYSQL_FTS_INSTALLED):
-    f = open(os.path.join(os.path.dirname(__file__), 'fts_install.sql'), 'r')
+    f_name = os.path.join(os.path.dirname(__file__), 'fts_install.sql')
+elif int(settings.MYSQL_FTS_VERSION < VERSION):
+    f_name = os.path.join(os.path.dirname(__file__), 'fts_update.sql')
+
+if f_name:
+    f = open(f_name, 'r')
 
     try:
         cursor = connection.cursor()
 
     try:
         cursor = connection.cursor()
@@ -17,6 +26,7 @@ if not bool(settings.MYSQL_FTS_INSTALLED):
         transaction.commit_unless_managed()
 
         settings.MYSQL_FTS_INSTALLED.set_value(True)
         transaction.commit_unless_managed()
 
         settings.MYSQL_FTS_INSTALLED.set_value(True)
+        settings.MYSQL_FTS_VERSION.set_value(VERSION)
 
     except Exception, e:
         #import sys, traceback
 
     except Exception, e:
         #import sys, traceback
@@ -31,4 +41,18 @@ word_re = re.compile(r'\w+', re.UNICODE)
 
 @decorate(QuestionManager.search, needs_origin=False)
 def question_search(self, keywords):
 
 @decorate(QuestionManager.search, needs_origin=False)
 def question_search(self, keywords):
-    return False, self.filter(models.Q(ftsindex__body__search=keywords.upper()))
\ No newline at end of file
+    keywords = keywords.upper()
+
+    qs = self.filter(
+            models.Q(ftsindex__body__isnull=False)
+    ).extra(
+        select={
+            'ranking': """
+                match(forum_mysqlftsindex.tagnames) against (%s) * 2 +
+                match(forum_mysqlftsindex.title) against (%s) * 4 +
+                match(forum_mysqlftsindex.body) against (%s) * 1
+                """,
+            },
+        select_params=[keywords, keywords, keywords]
+    ).filter(Q(ftsindex__title__search=keywords) | Q(ftsindex__tagnames__search=keywords) | Q(ftsindex__body__search=keywords))
+    return '-ranking', qs