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,
|
||||
};
|
||||
|
||||
15
src/countries.js
Normal file
15
src/countries.js
Normal file
@@ -0,0 +1,15 @@
|
||||
import knex from './knex.js';
|
||||
|
||||
function curateCountry(countryEntry) {
|
||||
return {
|
||||
name: countryEntry.name,
|
||||
alpha2: countryEntry.alpha2,
|
||||
code: countryEntry.code,
|
||||
};
|
||||
}
|
||||
|
||||
export async function fetchCountriesByAlpha2(alpha2s) {
|
||||
const entries = await knex('countries').whereIn('alpha2', alpha2s);
|
||||
|
||||
return entries.map((entry) => curateCountry(entry));
|
||||
}
|
||||
@@ -1,19 +1,34 @@
|
||||
import { fetchActors } from '../actors.js';
|
||||
|
||||
export function curateActorsQuery(query) {
|
||||
console.log('input query', query);
|
||||
|
||||
return {
|
||||
query: query.q,
|
||||
gender: query.gender,
|
||||
age: query.age?.split(',').map((age) => Number(age)),
|
||||
cup: query.cup?.split(','),
|
||||
naturalBoobs: query.nb,
|
||||
height: query.height?.split(',').map((height) => Number(height)),
|
||||
weight: query.weight?.split(',').map((weight) => Number(weight)),
|
||||
requireAvatar: query.avatar,
|
||||
};
|
||||
}
|
||||
|
||||
export async function fetchActorsApi(req, res) {
|
||||
const { actors, limit, total } = await fetchActors({
|
||||
query: req.query.q,
|
||||
requireAvatar: Object.hasOwn(req.query, 'avatar'),
|
||||
age: req.query.age?.split(',').map((age) => Number(age)),
|
||||
height: req.query.height?.split(',').map((height) => Number(height)),
|
||||
weight: req.query.weight?.split(',').map((weight) => Number(weight)),
|
||||
}, {
|
||||
const {
|
||||
actors,
|
||||
countries,
|
||||
limit,
|
||||
total,
|
||||
} = await fetchActors(curateActorsQuery(req.query), {
|
||||
page: Number(req.query.page) || 1,
|
||||
limit: Number(req.query.limit) || 50,
|
||||
});
|
||||
|
||||
res.send({
|
||||
actors,
|
||||
countries,
|
||||
limit,
|
||||
total,
|
||||
});
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
import config from 'config';
|
||||
import express from 'express';
|
||||
import boolParser from 'express-query-boolean';
|
||||
import Router from 'express-promise-router';
|
||||
import compression from 'compression';
|
||||
import { renderPage } from 'vike/server'; // eslint-disable-line import/extensions
|
||||
@@ -30,6 +31,8 @@ async function startServer() {
|
||||
app.use(compression());
|
||||
app.disable('x-powered-by');
|
||||
|
||||
router.use(boolParser());
|
||||
|
||||
router.use('/', express.static('public'));
|
||||
router.use('/', express.static('static'));
|
||||
router.use('/media', express.static(config.media.path));
|
||||
@@ -66,6 +69,7 @@ async function startServer() {
|
||||
router.get('*', async (req, res, next) => {
|
||||
const pageContextInit = {
|
||||
urlOriginal: req.originalUrl,
|
||||
urlQuery: req.query, // vike's own query does not apply boolean parser
|
||||
};
|
||||
|
||||
const pageContext = await renderPage(pageContextInit);
|
||||
|
||||
Reference in New Issue
Block a user