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,9 +32,10 @@ function initReleasesActions(store, _router) {
$limit: Int = 20
) {
releases: searchReleases(
search: $query
query: $query
first: $limit
) {
releaseById {
id
title
date
@ -72,6 +73,7 @@ function initReleasesActions(store, _router) {
}
}
}
}
`, {
query,
limit,
@ -79,7 +81,7 @@ function initReleasesActions(store, _router) {
if (!res) return [];
return res.releases.map(release => curateRelease(release));
return res.releases.map(release => curateRelease(release.releaseById));
}
async function fetchReleaseById({ _commit }, releaseId) {

View File

@ -1,14 +1,34 @@
exports.up = knex => Promise.resolve()
.then(() => knex.raw(`
CREATE FUNCTION search_releases(search text) RETURNS SETOF releases AS $$
SELECT * FROM releases
WHERE
title ILIKE ('%' || search || '%') OR
url ILIKE ('%' || search || '%')
CREATE VIEW releases_document AS
SELECT
releases.id,
releases.site_id,
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;
`));
exports.down = knex => Promise.resolve()
.then(() => knex.raw(`
DROP FUNCTION IF EXISTS search_releases;
DROP VIEW IF EXISTS releases_document;
`));