diff --git a/src/scenes.js b/src/scenes.js index 5781329..f3e5d1b 100644 --- a/src/scenes.js +++ b/src/scenes.js @@ -563,16 +563,16 @@ async function queryManticoreSql(filters, options, _reqUser) { .limit(options.limit) .offset((options.page - 1) * options.limit), // option threads=1 fixes actors, but drastically slows down performance, wait for fix - yearsFacet: options.aggregateYears ? knex.raw('facet effective_year as years order by effective_year desc limit ?', [aggSize]) : null, - actorsFacet: options.aggregateActors ? knex.raw('facet scenes.actor_ids distinct id order by count(distinct id) desc limit ?', [aggSize]) : null, + yearsFacet: options.aggregateYears ? knex.raw('facet effective_year as years_facet order by effective_year desc limit ?', [aggSize]) : null, + actorsFacet: options.aggregateActors ? knex.raw('facet scenes.actor_ids as actors_facet distinct id order by count(distinct id) desc limit ?', [aggSize]) : null, // don't facet tags associated to other actors, actor ID 0 means global tagsFacet: options.aggregateTags // eslint-disable-line no-nested-ternary - ? (filters.stashId // we can't join the tags table as well as the stashes table - ? knex.raw('facet scenes.tag_ids order by count(*) desc limit ?', [aggSize]) - : knex.raw(`facet IF(IN(scenes_tags.actor_id, ${[0, ...filters?.actorIds || []]}), scenes_tags.tag_id, 0) as tags_facet distinct id order by count(distinct id) desc limit ?`, [aggSize])) + ? (filters.stashId || !filters?.actorIds || filters.actorIds.length === 0 // we can't join the tags table as well as the stashes table + ? knex.raw('facet scenes.tag_ids as tags_facet order by count(distinct id) desc limit ?', [aggSize]) + : knex.raw(`facet IF(IN(scenes_tags.actor_id, ${[0, ...filters?.actorIds || []]}), scenes_tags.tag_id, 0) tags_facet distinct id order by count(distinct id) desc limit ?`, [aggSize])) : null, - channelsFacet: options.aggregateChannels ? knex.raw('facet scenes.channel_id distinct id order by count(distinct id) desc limit ?', [aggSize]) : null, - studiosFacet: options.aggregateChannels ? knex.raw('facet scenes.studio_id distinct id order by count(distinct id) desc limit ?', [aggSize]) : null, + channelsFacet: options.aggregateChannels ? knex.raw('facet scenes.channel_id as channels_facet distinct id order by count(distinct id) desc limit ?', [aggSize]) : null, + studiosFacet: options.aggregateChannels ? knex.raw('facet scenes.studio_id as studios_facet distinct id order by count(distinct id) desc limit ?', [aggSize]) : null, maxMatches: config.database.manticore.maxMatches, maxQueryTime: config.database.manticore.maxQueryTime, }).toString(); @@ -593,30 +593,28 @@ async function queryManticoreSql(filters, options, _reqUser) { // console.log(util.inspect(results, null, Infinity)); const years = results - .find((result) => (result.columns[0].years || result.columns[0]['scenes.years']) && result.columns[1]['count(*)']) - ?.data.map((row) => ({ key: row.years || row['scenes.years'], doc_count: row['count(*)'] })) + .find((result) => result.columns[0].years_facet && result.columns[1]['count(*)']) + ?.data.map((row) => ({ key: row.years_facet, doc_count: row['count(*)'] })) || []; const actorIds = results - .find((result) => (result.columns[0].actor_ids || result.columns[0]['scenes.actor_ids']) && result.columns[1]['count(distinct id)']) - ?.data.map((row) => ({ key: row.actor_ids || row['scenes.actor_ids'], doc_count: row['count(distinct id)'] })) + .find((result) => result.columns[0].actors_facet && result.columns[1]['count(distinct id)']) + ?.data.map((row) => ({ key: row.actors_facet, doc_count: row['count(distinct id)'] })) || []; const tagIds = results - .find((result) => (result.columns[0].tag_id || result.columns[0].tags_facet || result.columns[0]['scenes.tag_ids']) && (result.columns[1]['count(distinct id)'] || result.columns[1]['count(*)'])) - ?.data.map((row) => ({ key: row.tag_id || row.tags_facet || row['scenes.tag_ids'], doc_count: row['count(distinct id)'] || row['count(*)'] })) + .find((result) => result.columns[0].tags_facet && result.columns[1]['count(distinct id)']) + ?.data.map((row) => ({ key: row.tags_facet, doc_count: row['count(distinct id)'] || row['count(*)'] })) || []; const channelIds = results - .find((result) => (result.columns[0].channel_id || result.columns[0]['scenes.channel_id']) && result.columns[1]['count(distinct id)']) - ?.data.map((row) => ({ key: row.channel_id || row['scenes.channel_id'], doc_count: row['count(distinct id)'] })) + .find((result) => result.columns[0].channels_facet && result.columns[1]['count(distinct id)']) + ?.data.map((row) => ({ key: row.channels_facet || row['scenes.channel_id'], doc_count: row['count(distinct id)'] })) || []; const studioIds = results - .find((result) => (result.columns[0].studio_id || result.columns[0]['scenes.studio_id']) && result.columns[1]['count(distinct id)']) - ?.data - .map((row) => ({ key: row.studio_id || row['scenes.studio_id'], doc_count: row['count(distinct id)'] })) - .filter((row) => !!row.key) + .find((result) => result.columns[0].studios_facet && result.columns[1]['count(distinct id)']) + ?.data.map((row) => ({ key: row.studios_facet, doc_count: row['count(distinct id)'] })).filter((row) => !!row.key) || []; const total = Number(results.at(-1).data.find((entry) => entry.Variable_name === 'total_found')?.Value) || 0;