Refactored manticore SQL query to use scenes_stashed as primary table.

This commit is contained in:
DebaucheryLibrarian 2024-03-17 03:03:36 +01:00
parent 911461784b
commit 929de64aa0
6 changed files with 69 additions and 34 deletions

View File

@ -50,14 +50,23 @@
class="input" class="input"
@change="search({ autoScope: false })" @change="search({ autoScope: false })"
> >
<option value="likes">Likes</option> <!-- not selected in SSR without prop -->
<option
v-if="pageStash"
:selected="scope === 'stashed'"
value="stashed"
>Stashed</option>
<option
v-if="filters.search"
:selected="scope === 'results'"
value="results"
>Relevance</option>
<option value="latest">Latest</option> <option value="latest">Latest</option>
<option value="upcoming">Upcoming</option> <option value="upcoming">Upcoming</option>
<option value="new">New</option> <option value="new">New</option>
<option <option value="likes">Likes</option>
value="results"
:disabled="!filters.search"
>Relevance</option>
</select> </select>
</div> </div>
@ -160,6 +169,8 @@ const scope = ref(routeParams.scope || props.defaultScope);
const total = ref(Number(pageProps.total)); const total = ref(Number(pageProps.total));
const loading = ref(false); const loading = ref(false);
console.log('SCOPE', routeParams.scope, scope.value);
const actorIds = urlParsed.search.actors?.split(',').map((identifier) => parseActorIdentifier(identifier)?.id).filter(Boolean) || []; const actorIds = urlParsed.search.actors?.split(',').map((identifier) => parseActorIdentifier(identifier)?.id).filter(Boolean) || [];
const queryActors = actorIds.map((urlActorId) => aggActors.value.find((aggActor) => aggActor.id === urlActorId)).filter(Boolean); const queryActors = actorIds.map((urlActorId) => aggActors.value.find((aggActor) => aggActor.id === urlActorId)).filter(Boolean);

View File

@ -11,7 +11,7 @@ export async function onBeforeRender(pageContext) {
const stashScenes = await fetchScenes(await curateScenesQuery({ const stashScenes = await fetchScenes(await curateScenesQuery({
...pageContext.urlQuery, ...pageContext.urlQuery,
scope: pageContext.routeParams.scope || 'latest', scope: pageContext.routeParams.scope || 'stashed',
stashId: stash.id, stashId: stash.id,
}), { }), {
page: Number(pageContext.routeParams.page) || 1, page: Number(pageContext.routeParams.page) || 1,

View File

@ -12,7 +12,7 @@ export default (pageContext) => {
routeParams: { routeParams: {
username: matched.params.username, username: matched.params.username,
stashSlug: matched.params.stashSlug, stashSlug: matched.params.stashSlug,
scope: matched.params.scope || 'latest', scope: matched.params.scope || 'stashed',
page: matched.params.page || '1', page: matched.params.page || '1',
path, path,
}, },

View File

@ -359,7 +359,7 @@ async function queryManticoreJson(filters, options, _reqUser) {
} }
async function queryManticoreSql(filters, options, _reqUser) { async function queryManticoreSql(filters, options, _reqUser) {
const aggSize = 10 || config.database.manticore.maxAggregateSize; const aggSize = config.database.manticore.maxAggregateSize;
const sqlQuery = knexManticore.raw(` const sqlQuery = knexManticore.raw(`
:query: :query:
@ -379,13 +379,27 @@ async function queryManticoreSql(filters, options, _reqUser) {
:tagsFacet: :tagsFacet:
:channelsFacet: :channelsFacet:
`, { `, {
query: knexManticore('scenes') query: knexManticore(filters.stashId ? 'scenes_stashed' : 'scenes')
.select(knex.raw('*, weight() as _score'))
.modify((builder) => { .modify((builder) => {
if (filters.stashId) { if (filters.stashId) {
builder.select(knex.raw(`
scenes.id as id,
scenes.title as title,
scenes.actor_ids as actor_ids,
scenes.tag_ids as tag_ids,
scenes.channel_id as channel_id,
scenes.network_id as network_id,
scenes.effective_date as effective_date,
scenes.created_at,
created_at as stashed_at,
weight() as _score
`));
builder builder
.innerJoin('scenes_stashed', 'scenes.id', 'scenes_stashed.scene_id') .innerJoin('scenes', 'scenes.id', 'scenes_stashed.scene_id')
.where('scenes_stashed.stash_id', filters.stashId); .where('stash_id', filters.stashId);
} else {
builder.select(knex.raw('*, weight() as _score'));
} }
if (filters.query) { if (filters.query) {
@ -411,57 +425,67 @@ async function queryManticoreSql(filters, options, _reqUser) {
if (!filters.scope || filters.scope === 'latest') { if (!filters.scope || filters.scope === 'latest') {
builder builder
.where('effective_date', '<=', Math.round(Date.now() / 1000)) .where('effective_date', '<=', Math.round(Date.now() / 1000))
.orderBy('effective_date', 'desc'); .orderBy('scenes.effective_date', 'desc'); // can't seem to use alias if it matches column-name? behavior not fully understand, but this works
} else if (filters.scope === 'upcoming') { } else if (filters.scope === 'upcoming') {
builder builder
.where('effective_date', '>', Math.round(Date.now() / 1000)) .where('effective_date', '>', Math.round(Date.now() / 1000))
.orderBy('effective_date', 'asc'); .orderBy('scenes.effective_date', 'asc');
} else if (filters.scope === 'new') { } else if (filters.scope === 'new') {
builder.orderBy([ builder.orderBy([
{ column: 'created_at', order: 'desc' }, { column: 'scenes.created_at', order: 'desc' },
{ column: 'effective_date', order: 'asc' }, { column: 'scenes.effective_date', order: 'asc' },
]); ]);
} else if (filters.scope === 'likes') { } else if (filters.scope === 'likes') {
builder.orderBy([ builder.orderBy([
{ column: 'stashed', order: 'desc' }, { column: 'scenes.stashed', order: 'desc' },
{ column: 'effective_date', order: 'desc' }, { column: 'scenes.effective_date', order: 'desc' },
]); ]);
} else if (filters.scope === 'results') { } else if (filters.scope === 'results') {
builder.orderBy([ builder.orderBy([
{ column: '_score', order: 'desc' }, { column: 'scenes._score', order: 'desc' },
{ column: 'effective_date', order: 'desc' }, { column: 'scenes.effective_date', order: 'desc' },
]);
} else if (filters.scope === 'stashed' && filters.stashId) {
builder.orderBy([
{ column: 'stashed_at', order: 'desc' },
{ column: 'scenes.effective_date', order: 'desc' },
]); ]);
} else { } else {
builder.orderBy('effective_date', 'desc'); builder.orderBy('scenes.effective_date', 'desc');
} }
}) })
.limit(options.limit) .limit(options.limit)
.toString(), .toString(),
// option threads=1 fixes actors, but drastically slows down performance, wait for fix // option threads=1 fixes actors, but drastically slows down performance, wait for fix
actorsFacet: options.aggregateActors ? knex.raw('facet actor_ids order by count(*) desc limit ?', [aggSize]) : null, actorsFacet: options.aggregateActors ? knex.raw('facet scenes.actor_ids order by count(*) desc limit ?', [aggSize]) : null,
tagsFacet: options.aggregateTags ? knex.raw('facet tag_ids order by count(*) desc limit ?', [aggSize]) : null, tagsFacet: options.aggregateTags ? knex.raw('facet scenes.tag_ids order by count(*) desc limit ?', [aggSize]) : null,
channelsFacet: options.aggregateChannels ? knex.raw('facet channel_id order by count(*) desc limit ?', [aggSize]) : null, channelsFacet: options.aggregateChannels ? knex.raw('facet scenes.channel_id order by count(*) desc limit ?', [aggSize]) : null,
maxMatches: config.database.manticore.maxMatches, maxMatches: config.database.manticore.maxMatches,
maxQueryTime: config.database.manticore.maxQueryTime, maxQueryTime: config.database.manticore.maxQueryTime,
}).toString(); }).toString();
console.log(sqlQuery); // manticore does not seem to accept table.column syntax if 'table' is primary (yet), crude work-around
const curatedSqlQuery = filters.stashId
? sqlQuery
: sqlQuery.replace(/scenes\./g, '');
const results = await utilsApi.sql(sqlQuery); const results = await utilsApi.sql(curatedSqlQuery);
// console.log(results);
const actorIds = results const actorIds = results
.find((result) => result.columns[0].actor_ids && result.columns[1]['count(*)']) .find((result) => (result.columns[0].actor_ids || result.columns[0]['scenes.actor_ids']) && result.columns[1]['count(*)'])
?.data.map((row) => ({ key: row.actor_ids, doc_count: row['count(*)'] })) ?.data.map((row) => ({ key: row.actor_ids || row['scenes.actor_ids'], doc_count: row['count(*)'] }))
|| []; || [];
const tagIds = results const tagIds = results
.find((result) => result.columns[0].tag_ids && result.columns[1]['count(*)']) .find((result) => (result.columns[0].tag_ids || result.columns[0]['scenes.tag_ids']) && result.columns[1]['count(*)'])
?.data.map((row) => ({ key: row.tag_ids, doc_count: row['count(*)'] })) ?.data.map((row) => ({ key: row.tag_ids || row['scenes.tag_ids'], doc_count: row['count(*)'] }))
|| []; || [];
const channelIds = results const channelIds = results
.find((result) => result.columns[0].channel_id && result.columns[1]['count(*)']) .find((result) => (result.columns[0].channel_id || result.columns[0]['scenes.channel_id']) && result.columns[1]['count(*)'])
?.data.map((row) => ({ key: row.channel_id, doc_count: row['count(*)'] })) ?.data.map((row) => ({ key: row.channel_id || row['scenes.channel_id'], doc_count: row['count(*)'] }))
|| []; || [];
return { return {

View File

@ -6,7 +6,7 @@ async function fetchMovies() {
.filter((movie) => movie.cast.length > 0 .filter((movie) => movie.cast.length > 0
&& movie.genres.length > 0 && movie.genres.length > 0
&& movie.cast.every((actor) => actor.charCodeAt(0) >= 65)) // throw out movies with non-alphanumerical actor names && movie.cast.every((actor) => actor.charCodeAt(0) >= 65)) // throw out movies with non-alphanumerical actor names
.map((movie, index) => ({ id: index, ...movie })); .map((movie, index) => ({ id: index + 1, ...movie }));
const actors = Array.from(new Set(movies.flatMap((movie) => movie.cast))).sort(); const actors = Array.from(new Set(movies.flatMap((movie) => movie.cast))).sort();
const genres = Array.from(new Set(movies.flatMap((movie) => movie.genres))); const genres = Array.from(new Set(movies.flatMap((movie) => movie.genres)));

2
static

@ -1 +1 @@
Subproject commit a501e96b89785229e447456bfaf2b17954139121 Subproject commit 826861601ee96218504c71526080b6b2ca815dc3