93 lines
4.2 KiB
JavaScript
93 lines
4.2 KiB
JavaScript
'use strict';
|
|
|
|
const knex = require('./knex');
|
|
const logger = require('./logger')(__filename);
|
|
const bulkInsert = require('./utils/bulk-insert');
|
|
|
|
async function updateSceneSearch(releaseIds) {
|
|
logger.info(`Updating search documents for ${releaseIds ? releaseIds.length : 'all' } releases`);
|
|
|
|
const documents = await knex.raw(`
|
|
SELECT
|
|
releases.id AS release_id,
|
|
TO_TSVECTOR(
|
|
'english',
|
|
COALESCE(releases.title, '') || ' ' ||
|
|
releases.entry_id || ' ' ||
|
|
entities.name || ' ' ||
|
|
entities.slug || ' ' ||
|
|
COALESCE(array_to_string(entities.alias, ' '), '') || ' ' ||
|
|
COALESCE(parents.name, '') || ' ' ||
|
|
COALESCE(parents.slug, '') || ' ' ||
|
|
COALESCE(array_to_string(parents.alias, ' '), '') || ' ' ||
|
|
COALESCE(releases.shoot_id, '') || ' ' ||
|
|
COALESCE(TO_CHAR(releases.date, 'YYYY YY MM FMMM FMMonth mon DD FMDD'), '') || ' ' ||
|
|
STRING_AGG(COALESCE(actors.name, ''), ' ') || ' ' ||
|
|
STRING_AGG(COALESCE(directors.name, ''), ' ') || ' ' ||
|
|
STRING_AGG(COALESCE(tags.name, ''), ' ') || ' ' ||
|
|
STRING_AGG(COALESCE(tags_aliases.name, ''), ' ')
|
|
) as document
|
|
FROM releases
|
|
LEFT JOIN entities ON releases.entity_id = entities.id
|
|
LEFT JOIN entities AS parents ON parents.id = entities.parent_id
|
|
LEFT JOIN releases_actors AS local_actors ON local_actors.release_id = releases.id
|
|
LEFT JOIN releases_directors AS local_directors ON local_directors.release_id = releases.id
|
|
LEFT JOIN releases_tags AS local_tags ON local_tags.release_id = releases.id
|
|
LEFT JOIN actors ON local_actors.actor_id = actors.id
|
|
LEFT JOIN actors AS directors ON local_directors.director_id = directors.id
|
|
LEFT JOIN tags ON local_tags.tag_id = tags.id AND tags.priority >= 6
|
|
LEFT JOIN tags as tags_aliases ON local_tags.tag_id = tags_aliases.alias_for AND tags_aliases.secondary = true
|
|
${releaseIds ? 'WHERE releases.id = ANY(?)' : ''}
|
|
GROUP BY releases.id, entities.name, entities.slug, entities.alias, parents.name, parents.slug, parents.alias;
|
|
`, releaseIds && [releaseIds]);
|
|
|
|
if (documents.rows?.length > 0) {
|
|
await bulkInsert('releases_search', documents.rows, ['release_id']);
|
|
}
|
|
|
|
await knex.raw('REFRESH MATERIALIZED VIEW releases_summaries;');
|
|
}
|
|
|
|
async function updateMovieSearch(movieIds, target = 'movie') {
|
|
logger.info(`Updating search documents for ${movieIds ? movieIds.length : 'all' } ${target}s`);
|
|
|
|
const documents = await knex.raw(`
|
|
SELECT
|
|
${target}s.id AS ${target}_id,
|
|
TO_TSVECTOR(
|
|
'english',
|
|
COALESCE(${target}s.title, '') || ' ' ||
|
|
entities.name || ' ' ||
|
|
entities.slug || ' ' ||
|
|
COALESCE(array_to_string(entities.alias, ' '), '') || ' ' ||
|
|
COALESCE(parents.name, '') || ' ' ||
|
|
COALESCE(parents.slug, '') || ' ' ||
|
|
COALESCE(array_to_string(parents.alias, ' '), '') || ' ' ||
|
|
COALESCE(TO_CHAR(${target}s.date, 'YYYY YY MM FMMM FMMonth mon DD FMDD'), '') || ' ' ||
|
|
STRING_AGG(COALESCE(releases.title, ''), ' ') || ' ' ||
|
|
STRING_AGG(COALESCE(actors.name, ''), ' ') || ' ' ||
|
|
STRING_AGG(COALESCE(tags.name, ''), ' ')
|
|
) as document
|
|
FROM ${target}s
|
|
LEFT JOIN entities ON ${target}s.entity_id = entities.id
|
|
LEFT JOIN entities AS parents ON parents.id = entities.parent_id
|
|
LEFT JOIN ${target}s_scenes ON ${target}s_scenes.${target}_id = ${target}s.id
|
|
LEFT JOIN releases ON releases.id = ${target}s_scenes.scene_id
|
|
LEFT JOIN releases_actors ON releases_actors.release_id = ${target}s_scenes.scene_id
|
|
LEFT JOIN releases_tags ON releases_tags.release_id = releases.id
|
|
LEFT JOIN actors ON actors.id = releases_actors.actor_id
|
|
LEFT JOIN tags ON tags.id = releases_tags.tag_id
|
|
${movieIds ? `WHERE ${target}s.id = ANY(?)` : ''}
|
|
GROUP BY ${target}s.id, entities.name, entities.slug, entities.alias, parents.name, parents.slug, parents.alias;
|
|
`, movieIds && [movieIds]);
|
|
|
|
if (documents.rows?.length > 0) {
|
|
await bulkInsert(`${target}s_search`, documents.rows, [`${target}_id`]);
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
updateSceneSearch,
|
|
updateMovieSearch,
|
|
};
|