From 1d6aae3d56d91988955f84837f76641a21614523 Mon Sep 17 00:00:00 2001 From: Magnus Hagander Date: Thu, 23 Mar 2017 17:03:56 +0100 Subject: [PATCH] Add support for searching in internal pages In particular, this re-enables the ability to search in the developer docs --- pgweb/search/views.py | 13 +++++++++++-- tools/search/sql/functions.sql | 10 +++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/pgweb/search/views.py b/pgweb/search/views.py index 72eae2b6..c97777c9 100644 --- a/pgweb/search/views.py +++ b/pgweb/search/views.py @@ -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', { diff --git a/tools/search/sql/functions.sql b/tools/search/sql/functions.sql index 9e86df3e..6d8cb024 100644 --- a/tools/search/sql/functions.sql +++ b/tools/search/sql/functions.sql @@ -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'; -- 2.39.5