Added boob and country aggregation to actors page.
This commit is contained in:
@@ -3,6 +3,7 @@ import { differenceInYears } from 'date-fns';
|
||||
import knex from './knex.js';
|
||||
import { searchApi } from './manticore.js';
|
||||
import { HttpError } from './errors.js';
|
||||
import { fetchCountriesByAlpha2 } from './countries.js';
|
||||
|
||||
export function curateActor(actor, context = {}) {
|
||||
return {
|
||||
@@ -85,7 +86,7 @@ function curateOptions(options) {
|
||||
}
|
||||
|
||||
function buildQuery(filters) {
|
||||
console.log(filters);
|
||||
console.log('filters', filters);
|
||||
|
||||
const query = {
|
||||
bool: {
|
||||
@@ -93,6 +94,10 @@ function buildQuery(filters) {
|
||||
},
|
||||
};
|
||||
|
||||
const expressions = {
|
||||
age: 'if(date_of_birth, floor((now() - date_of_birth) / 31556952), 0)',
|
||||
};
|
||||
|
||||
if (filters.query) {
|
||||
query.bool.must.push({
|
||||
match: {
|
||||
@@ -101,6 +106,14 @@ function buildQuery(filters) {
|
||||
});
|
||||
}
|
||||
|
||||
if (filters.gender) {
|
||||
query.bool.must.push({
|
||||
equals: {
|
||||
gender: filters.gender,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
['age', 'height', 'weight'].forEach((attribute) => {
|
||||
if (filters[attribute]) {
|
||||
query.bool.must.push({
|
||||
@@ -114,6 +127,24 @@ function buildQuery(filters) {
|
||||
}
|
||||
});
|
||||
|
||||
if (filters.cup) {
|
||||
expressions.cup_in_range = `regex(cup, '^[${filters.cup[0]}-${filters.cup[1]}]')`;
|
||||
|
||||
query.bool.must.push({
|
||||
equals: {
|
||||
cup_in_range: 1,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (typeof filters.naturalBoobs === 'boolean') {
|
||||
query.bool.must.push({
|
||||
equals: {
|
||||
natural_boobs: filters.naturalBoobs ? 2 : 1, // manticore boolean does not support null, so 0 = null, 1 = false (enhanced), 2 = true (natural)
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (filters.requireAvatar) {
|
||||
query.bool.must.push({
|
||||
equals: {
|
||||
@@ -122,29 +153,40 @@ function buildQuery(filters) {
|
||||
});
|
||||
}
|
||||
|
||||
return query;
|
||||
return { query, expressions };
|
||||
}
|
||||
|
||||
export async function fetchActors(filters, rawOptions) {
|
||||
const options = curateOptions(rawOptions);
|
||||
const query = buildQuery(filters);
|
||||
const { query, expressions } = buildQuery(filters);
|
||||
|
||||
const result = await searchApi.search({
|
||||
index: 'actors',
|
||||
query,
|
||||
expressions: {
|
||||
age: 'if(date_of_birth, floor((now() - date_of_birth) / 31556952), 0)',
|
||||
},
|
||||
expressions,
|
||||
limit: options.limit,
|
||||
offset: (options.page - 1) * options.limit,
|
||||
sort: [{ slug: 'asc' }],
|
||||
aggs: {
|
||||
countries: {
|
||||
terms: {
|
||||
field: 'country',
|
||||
size: 300,
|
||||
},
|
||||
sort: [{ country: { order: 'asc' } }],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const actorIds = result.hits.hits.map((hit) => Number(hit._id));
|
||||
const actors = await fetchActorsById(actorIds);
|
||||
const [actors, countries] = await Promise.all([
|
||||
fetchActorsById(actorIds),
|
||||
fetchCountriesByAlpha2(result.aggregations.countries.buckets.map((bucket) => bucket.key)),
|
||||
]);
|
||||
|
||||
return {
|
||||
actors,
|
||||
countries,
|
||||
total: result.hits.total,
|
||||
limit: options.limit,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user