Added scene tags filter.
This commit is contained in:
@@ -2,6 +2,7 @@ import knex from './knex.js';
|
||||
import { searchApi } from './manticore.js';
|
||||
import { HttpError } from './errors.js';
|
||||
import { fetchActorsById, curateActor, sortActorsByGender } from './actors.js';
|
||||
import { fetchTagsById } from './tags.js';
|
||||
|
||||
function curateMedia(media) {
|
||||
if (!media) {
|
||||
@@ -148,6 +149,8 @@ function curateOptions(options) {
|
||||
limit: options?.limit || 30,
|
||||
page: Number(options?.page) || 1,
|
||||
aggregate: options.aggregate ?? true,
|
||||
aggregateActors: (options.aggregate ?? true) && (options.aggregateActors ?? true),
|
||||
aggregateTags: (options.aggregate ?? true) && (options.aggregateTags ?? true),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -187,8 +190,6 @@ function buildQuery(filters = {}) {
|
||||
}
|
||||
|
||||
if (filters.actorIds) {
|
||||
console.log('actor ids', filters.actorIds);
|
||||
|
||||
filters.actorIds.forEach((actorId) => {
|
||||
query.bool.must.push({
|
||||
equals: {
|
||||
@@ -198,6 +199,16 @@ function buildQuery(filters = {}) {
|
||||
});
|
||||
}
|
||||
|
||||
if (filters.tagIds) {
|
||||
filters.tagIds.forEach((tagId) => {
|
||||
query.bool.must.push({
|
||||
equals: {
|
||||
'any(tag_ids)': tagId,
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/* tag filter
|
||||
must_not: [
|
||||
{
|
||||
@@ -225,8 +236,8 @@ export async function fetchScenes(filters, rawOptions) {
|
||||
limit: options.limit,
|
||||
offset: (options.page - 1) * options.limit,
|
||||
sort,
|
||||
...(options.aggregate && {
|
||||
aggs: {
|
||||
aggs: {
|
||||
...(options.aggregateActors && {
|
||||
actorIds: {
|
||||
terms: {
|
||||
field: 'actor_ids',
|
||||
@@ -234,14 +245,25 @@ export async function fetchScenes(filters, rawOptions) {
|
||||
},
|
||||
// sort: [{ doc_count: { order: 'asc' } }],
|
||||
},
|
||||
},
|
||||
}),
|
||||
}),
|
||||
...(options.aggregateTags && {
|
||||
tagIds: {
|
||||
terms: {
|
||||
field: 'tag_ids',
|
||||
size: 1000,
|
||||
},
|
||||
// sort: [{ doc_count: { order: 'asc' } }],
|
||||
},
|
||||
}),
|
||||
},
|
||||
});
|
||||
|
||||
const actorCounts = result.aggregations?.actorIds && Object.fromEntries(result.aggregations?.actorIds?.buckets.map((bucket) => [bucket.key, { count: bucket.doc_count }]));
|
||||
const actorCounts = options.aggregateActors && Object.fromEntries(result.aggregations?.actorIds?.buckets.map((bucket) => [bucket.key, { count: bucket.doc_count }]));
|
||||
const tagCounts = options.aggregateTags && Object.fromEntries(result.aggregations?.tagIds?.buckets.map((bucket) => [bucket.key, { count: bucket.doc_count }]));
|
||||
|
||||
const [actors] = await Promise.all([
|
||||
result.aggregations?.actorIds ? fetchActorsById(result.aggregations.actorIds.buckets.map((bucket) => bucket.key), { order: ['name', 'asc'], append: actorCounts }) : [],
|
||||
const [aggActors, aggTags] = await Promise.all([
|
||||
options.aggregateActors ? fetchActorsById(result.aggregations.actorIds.buckets.map((bucket) => bucket.key), { order: ['name', 'asc'], append: actorCounts }) : [],
|
||||
options.aggregateTags ? fetchTagsById(result.aggregations.tagIds.buckets.map((bucket) => bucket.key), { order: ['name', 'asc'], append: tagCounts }) : [],
|
||||
]);
|
||||
|
||||
const sceneIds = result.hits.hits.map((hit) => Number(hit._id));
|
||||
@@ -249,7 +271,8 @@ export async function fetchScenes(filters, rawOptions) {
|
||||
|
||||
return {
|
||||
scenes,
|
||||
actors,
|
||||
aggActors,
|
||||
aggTags,
|
||||
total: result.hits.total,
|
||||
limit: options.limit,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user