Add support for searching in internal pages
authorMagnus Hagander <magnus@hagander.net>
Thu, 23 Mar 2017 16:03:56 +0000 (17:03 +0100)
committerMagnus Hagander <magnus@hagander.net>
Thu, 23 Mar 2017 16:03:56 +0000 (17:03 +0100)
In particular, this re-enables the ability to search in the developer
docs

pgweb/search/views.py
tools/search/sql/functions.sql

index 72eae2b69bd2b26b43be99a38cf02c30bb87d68a..c97777c9b3948a9c3703f8e2937371d5ab0dde11 100644 (file)
@@ -251,14 +251,23 @@ def search(request):
                                        'search_error': 'Could not connect to search database.'
                                        }, RequestContext(request))
 
+               # This is kind of a hack, but... Some URLs are flagged as internal
+               # and should as such only be included in searches that explicitly
+               # reference the suburl that they are in.
+               if suburl.startswith('/docs/devel'):
+                       include_internal = True
+               else:
+                       include_internal = False
+
                # perform the query for general web search
                try:
-                       curs.execute("SELECT * FROM site_search(%(query)s, %(firsthit)s, %(hitsperpage)s, %(allsites)s, %(suburl)s)", {
+                       curs.execute("SELECT * FROM site_search(%(query)s, %(firsthit)s, %(hitsperpage)s, %(allsites)s, %(suburl)s, %(internal)s)", {
                                'query': query,
                                'firsthit': firsthit - 1,
                                'hitsperpage': hitsperpage,
                                'allsites': allsites,
-                               'suburl': suburl
+                               'suburl': suburl,
+                               'internal': include_internal,
                                })
                except psycopg2.ProgrammingError:
                        return render_to_response('search/sitesearch.html', {
index 9e86df3eb355887b65f10762f279e482cfcfca7e..6d8cb02429cdcb26702e4a4a057df398f7450162 100644 (file)
@@ -60,7 +60,7 @@ LANGUAGE 'plpgsql';
 ALTER FUNCTION archives_search(text, int, timestamptz, timestamptz, int, int, char) SET default_text_search_config = 'public.pg';
 
 
-CREATE OR REPLACE FUNCTION site_search(query text, startofs int, hitsperpage int, allsites bool, _suburl text)
+CREATE OR REPLACE FUNCTION site_search(query text, startofs int, hitsperpage int, allsites bool, _suburl text, includeinternal boolean DEFAULT False)
 RETURNS TABLE (siteid int, baseurl text, suburl text, title text, headline text, rank float)
 AS $$
 DECLARE
@@ -82,13 +82,13 @@ BEGIN
 
     IF allsites THEN
         SELECT INTO pagecount sum(sites.pagecount) FROM sites;
-        OPEN curs FOR SELECT sites.id AS siteid, sites.baseurl, webpages.suburl, ts_rank_cd(fti,tsq) * relprio AS ts_rank_cd FROM webpages INNER JOIN sites ON webpages.site=sites.id WHERE fti @@ tsq ORDER BY ts_rank_cd(fti,tsq) * relprio DESC LIMIT 1000;
+        OPEN curs FOR SELECT sites.id AS siteid, sites.baseurl, webpages.suburl, ts_rank_cd(fti,tsq) * relprio AS ts_rank_cd FROM webpages INNER JOIN sites ON webpages.site=sites.id WHERE fti @@ tsq AND (includeinternal OR NOT isinternal) ORDER BY ts_rank_cd(fti,tsq) * relprio DESC LIMIT 1000;
     ELSE
         SELECT INTO pagecount sites.pagecount FROM sites WHERE id=1;
         IF _suburl IS NULL THEN
-            OPEN curs FOR SELECT sites.id AS siteid, sites.baseurl, webpages.suburl, ts_rank_cd(fti,tsq) * relprio AS ts_rank_cd FROM webpages INNER JOIN sites ON webpages.site=sites.id WHERE fti @@ tsq AND site=1 ORDER BY ts_rank_cd(fti,tsq) * relprio DESC LIMIT 1000;
+            OPEN curs FOR SELECT sites.id AS siteid, sites.baseurl, webpages.suburl, ts_rank_cd(fti,tsq) * relprio AS ts_rank_cd FROM webpages INNER JOIN sites ON webpages.site=sites.id WHERE fti @@ tsq AND site=1 AND (includeinternal OR NOT isinternal) ORDER BY ts_rank_cd(fti,tsq) * relprio DESC LIMIT 1000;
         ELSE
-            OPEN curs FOR SELECT sites.id AS siteid, sites.baseurl, webpages.suburl, ts_rank_cd(fti,tsq) * relprio AS ts_rank_cd FROM webpages INNER JOIN sites ON webpages.site=sites.id WHERE fti @@ tsq AND site=1 AND webpages.suburl LIKE _suburl||'%' ORDER BY ts_rank_cd(fti,tsq) * relprio DESC LIMIT 1000;
+            OPEN curs FOR SELECT sites.id AS siteid, sites.baseurl, webpages.suburl, ts_rank_cd(fti,tsq) * relprio AS ts_rank_cd FROM webpages INNER JOIN sites ON webpages.site=sites.id WHERE fti @@ tsq AND site=1 AND webpages.suburl LIKE _suburl||'%' AND (includeinternal OR NOT isinternal) ORDER BY ts_rank_cd(fti,tsq) * relprio DESC LIMIT 1000;
         END IF;
     END IF;
     LOOP
@@ -106,4 +106,4 @@ BEGIN
 END;
 $$
 LANGUAGE 'plpgsql';
-ALTER FUNCTION site_search(text, int, int, bool, text) SET default_text_search_config = 'public.pg';
\ No newline at end of file
+ALTER FUNCTION site_search(text, int, int, bool, text, boolean) SET default_text_search_config = 'public.pg';