diff --git a/src/scenes.js b/src/scenes.js index 0ea5fde..5781329 100644 --- a/src/scenes.js +++ b/src/scenes.js @@ -564,15 +564,15 @@ async function queryManticoreSql(filters, options, _reqUser) { .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 order by count(*) desc limit ?', [aggSize]) : null, + actorsFacet: options.aggregateActors ? knex.raw('facet scenes.actor_ids 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])) : null, - channelsFacet: options.aggregateChannels ? knex.raw('facet scenes.channel_id order by count(*) desc limit ?', [aggSize]) : null, - studiosFacet: options.aggregateChannels ? knex.raw('facet scenes.studio_id order by count(*) 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, maxMatches: config.database.manticore.maxMatches, maxQueryTime: config.database.manticore.maxQueryTime, }).toString(); @@ -598,8 +598,8 @@ async function queryManticoreSql(filters, options, _reqUser) { || []; const actorIds = results - .find((result) => (result.columns[0].actor_ids || result.columns[0]['scenes.actor_ids']) && result.columns[1]['count(*)']) - ?.data.map((row) => ({ key: row.actor_ids || row['scenes.actor_ids'], doc_count: row['count(*)'] })) + .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)'] })) || []; const tagIds = results @@ -608,14 +608,14 @@ async function queryManticoreSql(filters, options, _reqUser) { || []; const channelIds = results - .find((result) => (result.columns[0].channel_id || result.columns[0]['scenes.channel_id']) && result.columns[1]['count(*)']) - ?.data.map((row) => ({ key: row.channel_id || row['scenes.channel_id'], doc_count: row['count(*)'] })) + .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)'] })) || []; const studioIds = results - .find((result) => (result.columns[0].studio_id || result.columns[0]['scenes.studio_id']) && result.columns[1]['count(*)']) + .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(*)'] })) + .map((row) => ({ key: row.studio_id || row['scenes.studio_id'], doc_count: row['count(distinct id)'] })) .filter((row) => !!row.key) || [];