]> git.openstreetmap.org Git - osqa.git/commitdiff
Better search, mixing FTS and exact match and better related questions functionality.
authorhernani <hernani@0cfe37f9-358a-4d5e-be75-b63607b5c754>
Thu, 20 May 2010 23:52:33 +0000 (23:52 +0000)
committerhernani <hernani@0cfe37f9-358a-4d5e-be75-b63607b5c754>
Thu, 20 May 2010 23:52:33 +0000 (23:52 +0000)
git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@310 0cfe37f9-358a-4d5e-be75-b63607b5c754

forum/models/base.py
forum/skins/default/media/js/osqa.ask.js [new file with mode: 0644]
forum/skins/default/templates/ask.html
forum/skins/default/templates/question_edit_tips.html
forum_modules/pgfulltext/handlers.py
forum_modules/pgfulltext/pg_fts_install.sql
forum_modules/pgfulltext/startup.py

index 276c8f6f06f4140c1373f7802052fe655b57d280..827dfb26818c7a12e644ff2fd6c3945663a7b630 100644 (file)
@@ -54,8 +54,6 @@ class CachedQuerySet(models.query.QuerySet):
             if obj is None:
                 obj = super(CachedQuerySet, self).get(*args, **kwargs)
                 obj.__class__.objects.cache_obj(obj)
-            else:
-                d = obj.__dict__
 
             return obj
 
diff --git a/forum/skins/default/media/js/osqa.ask.js b/forum/skins/default/media/js/osqa.ask.js
new file mode 100644 (file)
index 0000000..e5e8c20
--- /dev/null
@@ -0,0 +1,66 @@
+var currentSideBar = 'div#title_side_bar';
+function changeSideBar(enabled_bar) {
+    $(currentSideBar).hide();
+    currentSideBar = enabled_bar;
+    $(currentSideBar).fadeIn('slow');
+
+}
+$(function () {
+    $('div#editor_side_bar').hide();
+    $('div#tags_side_bar').hide();
+
+    $('input#id_title').focus(function(){changeSideBar('div#title_side_bar')});
+    $('textarea#editor').focus(function(){changeSideBar('div#editor_side_bar')});
+    $('input#id_tags').focus(function(){changeSideBar('div#tags_side_bar')});
+});
+
+$(function() {
+    var $input = $('#id_title');
+    var $box = $('#ask-related-questions');
+    var template = $('#question-summary-template').html();
+
+    var results_cache = {};
+
+    function reload_suggestions_box(e) {
+        var q = $input.val().trim();
+
+        if (q.length == 0) {
+            $('#ask-related-questions').html('');
+            return false;
+        }
+
+        if (results_cache[q] && results_cache[q] != '') {
+            $('#ask-related-questions').html(results_cache[q]);
+            return false;
+        }
+
+        $.post(related_questions_url, {title: q}, function(data) {
+            if (data) {
+                var c = $input.val().trim();
+
+                if (c != q) {
+                    return;
+                }
+
+                var html = '';
+                for (var i = 0; i < data.length; i++) {
+                    var item = template.replace(new RegExp('%URL%', 'g'), data[i].url)
+                                       .replace(new RegExp('%SCORE%', 'g'), data[i].score)
+                                       .replace(new RegExp('%TITLE%', 'g'), data[i].title)
+                                       .replace(new RegExp('%SUMMARY%', 'g'), data[i].summary);
+
+                    html += item;
+
+                }
+
+                results_cache[q] = html;
+
+                $('#ask-related-questions').html(html);
+            }
+        }, 'json');
+
+        return false;
+    }
+
+    $input.keyup(reload_suggestions_box);
+});
\ No newline at end of file
index ac393cde3355fabec8aa0aa1af44fd53c83396e5..87453314745a5eb18f08944c96c62450d44e0bfa 100644 (file)
@@ -16,7 +16,7 @@
             </div>
         </script>
         <script type="text/javascript">
-        $().ready(function(){
+        $(function(){
             //set current module button style
             $("#nav_ask").attr('className',"on");
             $('#editor').TextAreaResizer();
                 }
 
             });
-
-            var $input = $('#id_title');
-            var $box = $('#ask-related-questions');
-            var template = $('#question-summary-template').html()
-
-            function reload_suggestion_box(q) {
-                $.post('{% url related_questions %}', {title: q}, function(data) {
-                    if (data) {
-                        var html = '';
-                        for (var i = 0; i < data.length; i++) {
-                            var item = template.replace(new RegExp('%URL%', 'g'), data[i].url)
-                                               .replace(new RegExp('%SCORE%', 'g'), data[i].score)
-                                               .replace(new RegExp('%TITLE%', 'g'), data[i].title)
-                                               .replace(new RegExp('%SUMMARY%', 'g'), data[i].summary);
-
-                            html += item;
-
-                        }
-                        $('#ask-related-questions').html(html);
-                    }
-                }, 'json');
-            }
-
-            $input.keyup(function(e) {
-                var q = $input.val().trim();
-
-                if (e.which == 32 && q.length > 8 && q.split(' ').length > 2) {
-                    reload_suggestion_box(q);
-                }
-
-                return true;
-            });
-
-            $input.change(function() {
-                var q = $input.val().trim();
-
-                if (q.length > 8) {
-                    reload_suggestion_box(q);
-                }
-            });
         });
+
+        var related_questions_url = "{% url related_questions %}";
         </script>
+        <script src="{% media "/media/js/osqa.ask.js" %}" type="text/javascript"></script>
 {% endblock %}
 
 {% block content %}
index 3e72a1e6c3d3e4301e6a3a407e0d649b3088e261..4ec544243cd91527b2b9e3cfda8b978ef6aa724d 100644 (file)
@@ -1,24 +1,5 @@
 <!-- question_edit_tips.html -->
 {% load i18n %}
-{% block forejs %}
-        <script type="text/javascript">
-            var currentSideBar = 'div#title_side_bar';
-            function changeSideBar(enabled_bar) {
-                $(currentSideBar).hide();
-                currentSideBar = enabled_bar;
-                $(currentSideBar).fadeIn('slow');
-
-            }
-            $(function () {
-                $('div#editor_side_bar').hide();
-                $('div#tags_side_bar').hide();
-
-                $('input#id_title').focus(function(){changeSideBar('div#title_side_bar')});
-                $('textarea#editor').focus(function(){changeSideBar('div#editor_side_bar')});
-                $('input#id_tags').focus(function(){changeSideBar('div#tags_side_bar')});
-            });
-        </script>
-{% endblock %}
 <div class="boxC" id="title_side_bar">
     <p class="subtitle darkred">{% trans "Title Tips" %}</p>
     <div>
index e1d98f20877875e6ac958a0b0862c41ee031f38d..6e2165b247278613753a05367441e80c846931af 100644 (file)
@@ -1,15 +1,22 @@
+from django.db.models import Q
 from forum.models.question import Question, QuestionManager
 from forum.modules.decorators import decorate
 
 @decorate(QuestionManager.search, needs_origin=False)
 def question_search(self, keywords):
     return self.extra(
-                    tables=['forum_rootnode_doc'],
+                    tables = ['forum_rootnode_doc'],
                     select={
-                        'ranking': 'ts_rank_cd(\'{0.1, 0.2, 0.8, 1.0}\'::float4[], "forum_rootnode_doc"."document", plainto_tsquery(\'english\', %s), 32)',
+                        'ranking': """
+                                rank_exact_matches(ts_rank_cd('{0.1, 0.2, 0.8, 1.0}'::float4[], "forum_rootnode_doc"."document", plainto_tsquery('english', %s), 32))
+                                """,
                     },
-                    where=['"forum_rootnode_doc"."node_id" = "forum_node"."id"', '"forum_rootnode_doc"."document" @@ plainto_tsquery(\'english\', %s)'],
+                    where=["""
+                           "forum_rootnode_doc"."node_id" = "forum_node"."id" AND ("forum_rootnode_doc"."document" @@ plainto_tsquery('english', %s) OR
+                           "forum_node"."title" ILIKE '""" + keywords + """%%')
+                           """],
                     params=[keywords],
                     select_params=[keywords],
                     order_by=['-ranking']
-                )
\ No newline at end of file
+                )
+
index ddc48a1b8f63e52f22b18c050e71c02746c384b5..4161fd8dce1288d9af0d1d105b33e4cc7a407364 100644 (file)
@@ -105,4 +105,15 @@ select case when public.doc_table_exists()=0 then public.build_doc_table()end;
 drop function build_doc_table();
 drop function doc_table_exists();
 
+CREATE OR REPLACE FUNCTION rank_exact_matches(rank float) RETURNS float AS $$
+begin
+       IF rank = 0 THEN
+               return 1;
+       ELSE
+               return rank;
+       END IF;
+
+end
+$$ LANGUAGE plpgsql;
+
 UPDATE forum_noderevision SET id = id WHERE TRUE;
index c7cd5eab8a62b26092ac4febf37ea613a6a32e2a..22f61a8b409dc10abe9c76fbdff054cd68aa5a22 100644 (file)
@@ -3,7 +3,7 @@ from forum.models import KeyValue
 from django.db import connection, transaction\r
 import settings\r
 \r
-VERSION = 7\r
+VERSION = 8\r
 \r
 if int(settings.PG_FTSTRIGGERS_VERSION) < VERSION:\r
     f = open(os.path.join(os.path.dirname(__file__), 'pg_fts_install.sql'), 'r')\r