From 36a51f04919b7f2412af1236862c6c1e6e055ae4 Mon Sep 17 00:00:00 2001 From: DebaucheryLibrarian Date: Fri, 5 Jul 2024 02:55:02 +0200 Subject: [PATCH] Sharing title filter function between manticore update module and sync tool, fixing sync tool title filter word boundaries. --- src/tools/manticore-scenes.js | 3 ++- src/update-search.js | 3 ++- src/utils/filter-title.js | 9 +++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 src/utils/filter-title.js diff --git a/src/tools/manticore-scenes.js b/src/tools/manticore-scenes.js index 3561e600..d7a16200 100644 --- a/src/tools/manticore-scenes.js +++ b/src/tools/manticore-scenes.js @@ -6,6 +6,7 @@ const args = require('yargs').argv; const { format } = require('date-fns'); const knex = require('../knex'); +const filterTitle = require('../utils/filter-title'); const mantiClient = new manticore.ApiClient(); @@ -116,7 +117,7 @@ async function init() { const docs = scenes.map((scene) => { const flatActors = scene.actors.flatMap((actor) => actor.f2.match(/[\w']+/g)); // match word characters to filter out brackets etc. const flatTags = scene.tags.filter((tag) => tag.f3 > 6).flatMap((tag) => (tag.f4 ? `${tag.f2} ${tag.f4}` : tag.f2).match(/[\w']+/g)); // only make top tags searchable to minimize cluttered results - const filteredTitle = scene.title && [...flatActors, ...flatTags].reduce((accTitle, tag) => accTitle.replace(new RegExp(tag.replace(/[^\w\s]+/g, ''), 'i'), ''), scene.title).trim().replace(/\s{2,}/, ' '); + const filteredTitle = filterTitle(scene.title, [...flatActors, ...flatTags]); return { replace: { diff --git a/src/update-search.js b/src/update-search.js index 5652692f..f11413ee 100644 --- a/src/update-search.js +++ b/src/update-search.js @@ -7,6 +7,7 @@ const knex = require('./knex'); const logger = require('./logger')(__filename); const bulkInsert = require('./utils/bulk-insert'); const chunk = require('./utils/chunk'); +const filterTitle = require('./utils/filter-title'); const mantiClient = new manticore.ApiClient(); const indexApi = new manticore.IndexApi(mantiClient); @@ -123,7 +124,7 @@ async function updateManticoreSceneSearch(releaseIds) { const docs = scenes.rows.map((scene) => { const flatActors = scene.actors.flatMap((actor) => actor.f2.split(' ')); const flatTags = scene.tags.filter((tag) => tag.f3 > 6).flatMap((tag) => [tag.f2].concat(tag.f4)).filter(Boolean); // only make top tags searchable to minimize cluttered results - const filteredTitle = scene.title && [...flatActors, ...flatTags].reduce((accTitle, tag) => accTitle.replace(new RegExp(`\\b${tag.replace(/[^\w\s]+/g, '')}\\b`, 'gi'), ''), scene.title).trim().replace(/\s{2,}/, ' '); + const filteredTitle = filterTitle(scene.title, [...flatActors, ...flatTags]); return { replace: { diff --git a/src/utils/filter-title.js b/src/utils/filter-title.js new file mode 100644 index 00000000..a1f80dab --- /dev/null +++ b/src/utils/filter-title.js @@ -0,0 +1,9 @@ +function filterTitle(title, keys) { + if (!title) { + return title; + } + + return keys.reduce((accTitle, tag) => accTitle.replace(new RegExp(`\\b${tag.replace(/[^\w\s]+/g, '')}\\b`, 'gi'), ''), title).trim().replace(/\s{2,}/, ' '); +} + +module.exports = filterTitle;