Using rudimentary full text search.

This commit is contained in:
ThePendulum 2020-02-26 03:38:21 +01:00
parent 0b5b9c8aa4
commit 170cc4244d
2 changed files with 53 additions and 31 deletions

View File

@ -32,42 +32,44 @@ function initReleasesActions(store, _router) {
$limit: Int = 20 $limit: Int = 20
) { ) {
releases: searchReleases( releases: searchReleases(
search: $query query: $query
first: $limit first: $limit
) { ) {
id releaseById {
title
date
url
site {
id id
slug title
name date
url url
network { site {
id id
slug slug
name name
url url
network {
id
slug
name
url
}
} }
} actors: releasesActors {
actors: releasesActors { actor {
actor { id
id slug
slug name
name }
} }
} tags: releasesTags {
tags: releasesTags { tag {
tag { id
id name
name slug
slug }
} }
} poster: releasesPosterByReleaseId {
poster: releasesPosterByReleaseId { media {
media { thumbnail
thumbnail }
} }
} }
} }
@ -79,7 +81,7 @@ function initReleasesActions(store, _router) {
if (!res) return []; if (!res) return [];
return res.releases.map(release => curateRelease(release)); return res.releases.map(release => curateRelease(release.releaseById));
} }
async function fetchReleaseById({ _commit }, releaseId) { async function fetchReleaseById({ _commit }, releaseId) {

View File

@ -1,14 +1,34 @@
exports.up = knex => Promise.resolve() exports.up = knex => Promise.resolve()
.then(() => knex.raw(` .then(() => knex.raw(`
CREATE FUNCTION search_releases(search text) RETURNS SETOF releases AS $$ CREATE VIEW releases_document AS
SELECT * FROM releases SELECT
WHERE releases.id,
title ILIKE ('%' || search || '%') OR releases.site_id,
url ILIKE ('%' || search || '%') to_tsvector(releases.title) ||
to_tsvector(sites.name) ||
to_tsvector(sites.slug) ||
to_tsvector(replace(CAST(releases.date AS VARCHAR), '-', ' ')) ||
to_tsvector(string_agg(actors.name, ' ')) ||
to_tsvector(string_agg(tags.name, ' ')) as document
FROM releases
JOIN releases_actors AS local_actors ON local_actors.release_id = releases.id
JOIN releases_tags AS local_tags ON local_tags.release_id = releases.id
JOIN sites ON releases.site_id = sites.id
JOIN actors ON local_actors.actor_id = actors.id
JOIN tags ON local_tags.tag_id = tags.id
GROUP BY releases.id, sites.name, sites.slug;
COMMENT ON VIEW releases_document IS E'@foreignKey (id) references releases (id)\n@foreignKey (site_id) references sites (id)';
CREATE FUNCTION search_releases(query text) RETURNS SETOF releases_document AS $$
SELECT * FROM releases_document AS search
WHERE search.document @@ plainto_tsquery(replace(query, '.', ' '))
ORDER BY ts_rank(search.document, plainto_tsquery(replace(query, '.', ' '))) DESC;
$$ LANGUAGE SQL STABLE; $$ LANGUAGE SQL STABLE;
`)); `));
exports.down = knex => Promise.resolve() exports.down = knex => Promise.resolve()
.then(() => knex.raw(` .then(() => knex.raw(`
DROP FUNCTION IF EXISTS search_releases; DROP FUNCTION IF EXISTS search_releases;
DROP VIEW IF EXISTS releases_document;
`)); `));