Aggregating channels, filter inoperable.

This commit is contained in:
2024-01-09 02:26:32 +01:00
parent 58f7ca0d89
commit d242eb3b73
17 changed files with 381 additions and 68 deletions

View File

@@ -3,6 +3,7 @@ import { searchApi } from './manticore.js';
import { HttpError } from './errors.js';
import { fetchActorsById, curateActor, sortActorsByGender } from './actors.js';
import { fetchTagsById } from './tags.js';
import { fetchEntitiesById } from './entities.js';
function curateMedia(media) {
if (!media) {
@@ -151,6 +152,7 @@ function curateOptions(options) {
aggregate: options.aggregate ?? true,
aggregateActors: (options.aggregate ?? true) && (options.aggregateActors ?? true),
aggregateTags: (options.aggregate ?? true) && (options.aggregateTags ?? true),
aggregateChannels: (options.aggregate ?? true) && (options.aggregateChannels ?? true),
};
}
@@ -224,11 +226,54 @@ function buildQuery(filters = {}) {
return { query, sort };
}
function buildAggregates(options) {
const aggregates = {};
if (options.aggregateActors) {
aggregates.actorIds = {
terms: {
field: 'actor_ids',
size: 5000,
},
// sort: [{ doc_count: { order: 'asc' } }],
};
}
if (options.aggregateTags) {
aggregates.tagIds = {
terms: {
field: 'tag_ids',
size: 1000,
},
};
}
if (options.aggregateChannels) {
aggregates.channelIds = {
terms: {
field: 'channel_id',
size: 1000,
},
};
}
return aggregates;
}
function countAggregations(buckets) {
if (!buckets) {
return null;
}
return Object.fromEntries(buckets.map((bucket) => [bucket.key, { count: bucket.doc_count }]));
}
export async function fetchScenes(filters, rawOptions) {
const options = curateOptions(rawOptions);
const { query, sort } = buildQuery(filters);
console.log(filters);
console.log('filters', filters);
console.log('options', options);
const result = await searchApi.search({
index: 'scenes',
@@ -236,34 +281,17 @@ export async function fetchScenes(filters, rawOptions) {
limit: options.limit,
offset: (options.page - 1) * options.limit,
sort,
aggs: {
...(options.aggregateActors && {
actorIds: {
terms: {
field: 'actor_ids',
size: 5000,
},
// sort: [{ doc_count: { order: 'asc' } }],
},
}),
...(options.aggregateTags && {
tagIds: {
terms: {
field: 'tag_ids',
size: 1000,
},
// sort: [{ doc_count: { order: 'asc' } }],
},
}),
},
aggs: buildAggregates(options),
});
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 actorCounts = options.aggregateActors && countAggregations(result.aggregations?.actorIds?.buckets);
const tagCounts = options.aggregateTags && countAggregations(result.aggregations?.tagIds?.buckets);
const channelCounts = options.aggregateChannels && countAggregations(result.aggregations?.channelIds?.buckets);
const [aggActors, aggTags] = await Promise.all([
const [aggActors, aggTags, aggChannels] = 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 }) : [],
options.aggregateChannels ? fetchEntitiesById(result.aggregations.channelIds.buckets.map((bucket) => bucket.key), { order: ['name', 'asc'], append: channelCounts }) : [],
]);
const sceneIds = result.hits.hits.map((hit) => Number(hit._id));
@@ -273,6 +301,7 @@ export async function fetchScenes(filters, rawOptions) {
scenes,
aggActors,
aggTags,
aggChannels,
total: result.hits.total,
limit: options.limit,
};