Skip to content

Commit

Permalink
Add DB support for merged_with column
Browse files Browse the repository at this point in the history
This is meant to provide the foundation for gracefully redirecting requests for
old URLs to the new URLs. For now we can start filtering out search requests,
etc.

Signed-off-by: Dan Scott <[email protected]>
  • Loading branch information
dbs committed Apr 4, 2018
1 parent d0ffc64 commit e49d09d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
4 changes: 3 additions & 1 deletion ris2sql
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class RISParser:
"ALTER TABLE citations ADD COLUMN series TEXT;",
"ALTER TABLE citations ADD COLUMN pub_title TEXT;",
"ALTER TABLE citations ADD COLUMN kw_tsv TSVECTOR;",
"ALTER TABLE citations ADD COLUMN merged_with TEXT;",
r"""UPDATE citations SET pub_date = NULL WHERE pub_date !~ '^\d{4}';""",
"DROP FUNCTION IF EXISTS ris_search(TEXT);",
"DROP TYPE IF EXISTS search_result CASCADE;",
Expand Down Expand Up @@ -146,10 +147,11 @@ class RISParser:
r"""UPDATE citations SET kw_tsv = kw_tsv || setweight(to_tsvector('english', COALESCE(
(SELECT STRING_AGG(author_name, ' ') FROM authored_v WHERE citation = citations.id), '')), 'B');""",
"CREATE INDEX kw_tsv_index ON citations USING GIN(kw_tsv);",
r"""CREATE FUNCTION ris_search(query TEXT) RETURNS SETOF search_result AS $$
r"""CREATE OR REPLACE FUNCTION ris_search(query TEXT) RETURNS SETOF search_result AS $$
SELECT id, title, substring(pub_date from 1 for 4)::INT AS pub_year, doc_type, ts_rank_cd(kw_tsv, to_tsquery(query))
FROM citations
WHERE kw_tsv @@ to_tsquery(query)
AND merged_with IS NULL
ORDER BY 3 DESC$$ LANGUAGE SQL;""",


Expand Down
14 changes: 12 additions & 2 deletions ris2web
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,9 @@ def home_html():
with conn:
with conn.cursor() as t:
t.execute(r"""SELECT COUNT(*)
FROM citations;"""
FROM citations
WHERE merged_with IS NULL
;"""
)
total = t.fetchone()[0]

Expand All @@ -378,21 +380,27 @@ def home_html():
p.execute(r"""SELECT pub_title, COUNT(*)
FROM citations
WHERE pub_title IS NOT NULL
AND merged_with IS NULL
GROUP BY pub_title
ORDER BY 2 DESC
LIMIT 50;"""
)
pubs = p.fetchall()

with conn.cursor() as pc:
pc.execute(r"""WITH x AS (SELECT DISTINCT pub_title FROM citations)
pc.execute(r"""WITH x AS (
SELECT DISTINCT pub_title
FROM citations
WHERE merged_with IS NULL
)
SELECT COUNT(*) FROM x"""
)
pcnt = pc.fetchone()[0]

with conn.cursor() as t:
t.execute(r"""SELECT doc_type, COUNT(*)
FROM citations
WHERE merged_with IS NULL
GROUP BY doc_type
ORDER BY 2 DESC"""
)
Expand All @@ -402,6 +410,7 @@ def home_html():
with conn.cursor() as c:
c.execute(r"""SELECT language, COUNT(*)
FROM citations
WHERE merged_with IS NULL
GROUP BY language
ORDER BY 2 DESC"""
)
Expand All @@ -411,6 +420,7 @@ def home_html():
c.execute(r"""SELECT series, COUNT(*)
FROM citations
WHERE series IS NOT NULL
AND merged_with IS NULL
GROUP BY series
ORDER BY 2 DESC"""
)
Expand Down

0 comments on commit e49d09d

Please sign in to comment.