ehsan-dashboards/dashboards/getElasticSearch.ts

955 lines
22 KiB
TypeScript

import { Client } from "@elastic/elasticsearch";
import { sequelize } from "models/connectionString";
import { QueryTypes } from "sequelize";
import { getNumeralVariants } from "utils/numeralNormalizer";
import {
InferType,
ObjectSchema,
array,
boolean,
number,
object,
string,
} from "yup";
type Props = InferType<ReturnType<typeof queryValidator>>;
export const searchAttribute = [
"id",
"name",
"brand_name",
"rate",
"number_of_comments",
// "base_discount",
// "base_base_price",
"route",
// "available_count",
"city",
"district",
"image_url",
// "date",
// "start_time",
"deals",
"fear_level",
"max_number_of_players",
"min_number_of_players",
"number_of_comments",
"genre",
"min_price",
"max_price",
"hardship_level",
"play_duration",
"status",
"sans",
"base_price",
"base_discount",
"sans_price",
"has_snapp",
"has_full_payment",
"options",
"media",
"meta_data",
"reasons_to_try",
"why_try",
"scenario",
"description",
"address",
"google_location_url",
"balad_location_url",
"neshan_location_url",
"mobility_level",
"opened_at",
"brand_route",
"min_ideal_number_of_players",
"max_ideal_number_of_players",
"cancel_seller_rate",
"suggestion_rate",
"comments",
"minimum_players_age",
"value_rate",
"real_number_of_comments",
"city_direction",
"free_sans",
"free_sans_count",
"is_full",
"is_deals",
"coordinates",
"latitude",
"longitude",
];
export const compactSearchAttribute = [
"available_count",
"free_sans_count",
"is_full",
"base_discount",
"base_price",
"brand_name",
"city",
"discount_percent",
"district",
"fear_level",
"fear_range",
"has_snapp",
"id",
"image_url",
"max_number_of_players",
"min_number_of_players",
"name",
"number_of_comments",
"options",
"play_duration",
"price",
"rate",
"route",
"sans",
"status",
"start_time",
"genre",
"real_number_of_comments",
"coordinates",
"latitude",
"longitude",
"max_price",
"min_price",
"why_try",
"scenario",
"reasons_to_try",
"description",
]
const durationMap = {
less_than_75_mins: { lt: 75 },
"75_100_mins": {
gte: 75,
lte: 100,
},
more_than_100_mins: {
gt: 100,
},
};
const numberOfPlayerMap = {
"2_3": [{ lte: 2 }, { lte: 3 }],
"4": [{ lte: 4 }],
more_than_5: [
{
lte: 5,
},
],
more_than_8: [{ lte: 8 }],
};
const room_reserve_rate = "";
// Drop params whose value is empty or the sentinel `all` BEFORE validation, so
// `?fear_level=all`, `?suitablefor=`, `?is_thrill=` etc. mean "no filter for
// this field" instead of failing validation and erroring the whole search.
export const sanitizeSearchQuery = (query: any) => {
if (!query || typeof query !== "object") return query;
const out: any = {};
for (const [key, value] of Object.entries(query)) {
if (value === "all" || value === "" || value == null) continue;
if (Array.isArray(value)) {
const filtered = value.filter(
(v) => v !== "all" && v !== "" && v != null
);
if (filtered.length) out[key] = filtered;
} else {
out[key] = value;
}
}
return out;
};
// A flag sent with no value (e.g. `?is_thrill=`) must be treated as "not set"
// instead of 400/500-ing the whole search. The frontend sometimes appends the
// param with an empty value when its toggle is off.
const optionalBool = () =>
boolean()
.typeError("yupBoolean")
.transform((value, original) =>
original === "" || original === null || original === undefined
? undefined
: value
);
const queryValidator = (rest: object) =>
object({
...rest,
sort_by: number().typeError("yupNumber").optional().default(2),
limit: number().typeError("yupNumber").optional().default(2),
page: number()
.typeError("yupNumber")
.default(1)
.transform((value: number) => Math.max(0, value - 1)), // page=0,1 → offset=0 (first page); page<0 clamps to 0
q: string(),
compact: boolean().typeError("yupBoolean").optional().default(true),
sans: string().oneOf(["30", "60", "120", "180", "360"]),
price: array(number().typeError("yupNumber").required())
.max(2)
.transform((value, originalValue: any | number[]) => {
if (!Array.isArray(originalValue) || !!Number(originalValue))
return [originalValue, Infinity];
return originalValue.sort((a, b) => a - b);
}),
age: string().oneOf(["under_12_yo", "12_18_yo", "more_than_18_yo"]),
number_of_players: array(
string()
.typeError("yupNumber")
.oneOf(Object.keys(numberOfPlayerMap))
.required()
).transform((value, originalValue: any | number[]) =>
!Array.isArray(originalValue) ? [originalValue] : originalValue
),
duration: array(
string().oneOf(Object.keys(durationMap)).required()
).transform((value, originalValue: any | number[]) =>
!Array.isArray(originalValue) ? [originalValue] : originalValue
),
hardship_level: array(number().typeError("yupNumber").required())
.max(2)
.transform((value, originalValue: any | number[]) => {
if (!Array.isArray(originalValue) || !!Number(originalValue))
return [originalValue, Infinity];
return originalValue.sort((a, b) => a - b);
}),
fear_range: array(number().typeError("yupNumber"))
.max(2)
.transform((value, originalValue: any | number[]) => {
if (!Array.isArray(originalValue) || !!Number(originalValue))
return [originalValue, Infinity];
return originalValue.sort((a, b) => a - b);
}),
has_touch: optionalBool(),
city: string(),
deals: optionalBool(),
has_free_sans: optionalBool(),
rid: array(
number().typeError("yupNumber").required("yupRequired")
).transform((value, originalValue: any | number[]) =>
!Array.isArray(originalValue) ? [originalValue] : originalValue
),
bid: array(
number().typeError("yupNumber").required("yupRequired")
).transform((value, originalValue: any | number[]) =>
!Array.isArray(originalValue) ? [originalValue] : originalValue
),
city_direction: string(),
is_deals: optionalBool(),
is_thrill: optionalBool(),
is_experience: optionalBool(),
status: number()
.typeError("yupNumber").oneOf([0, 1, 2, 3, 4]),
latitude: number().typeError("yupNumber"),
longitude: number().typeError("yupNumber"),
distance: string().oneOf(["1km", "2km", "5km", "10km", "15km", "20km", "30km", "50km", "100km"]).default("10km"),
// Bounding box search parameters
north_lat: number().typeError("yupNumber"),
south_lat: number().typeError("yupNumber"),
east_lng: number().typeError("yupNumber"),
west_lng: number().typeError("yupNumber"),
// no_ads: boolean().optional().default(false),
});
const handleSans = (props: Props["sans"]) => {
if (!props) return [];
return [
{
range: {
"sans.sans": {
gt: `now+${props}m`,
},
},
},
{
term: {
"sans.is_free": true,
},
},
];
};
const handlePrice = (props: Props["price"]) => {
if (!props) return [];
const [_min, _max] = props;
return [
{
range: {
min_price: {
gte: _min,
},
},
},
{
range: {
max_price: {
lte: _max,
},
},
},
];
};
const handleAge = (props: Props["age"]) => {
if (!props) return [];
const map = {
under_12_yo: { lt: 12 },
"12_18_yo": {
gte: 12,
lt: 18,
},
more_than_18_yo: {
gte: 18,
},
};
const _val = Array.isArray(props) ? (props as string[]) : [props as string];
const should: any = [];
type Map = keyof typeof map;
_val.map((value) => {
if (!!map[value as Map])
should.push({
range: {
minimum_players_age: map[value as Map],
},
});
});
return [
{
bool: {
should,
},
},
];
};
const handleNumberPlayers = (props: Props["number_of_players"]) => {
if (!props) return [];
type Map = keyof typeof numberOfPlayerMap;
return [
{
bool: {
should: props.flatMap((item) =>
numberOfPlayerMap[item as Map].map((item) => ({
range: { min_number_of_players: item },
}))
),
},
},
];
};
const handleDuration = (props: Props["duration"]) => {
if (!props) return [];
type Map = keyof typeof durationMap;
return [
{
bool: {
should: props.map((item) => ({
range: {
play_duration: durationMap[item as Map],
},
})),
},
},
];
};
const handleHardship = (props: Props["hardship_level"]) => {
if (!props) return [];
return [
{
range: {
hardship_level: {
gte: props[0],
lte: props[1],
},
},
},
];
};
const fearLevel = (props: Props["fear_range"]) => {
if (!props) return [];
return [
{
range: {
fear_level: {
gte: props[0],
lte: props[1],
},
},
},
];
};
const handleTouch = (props: Props["has_touch"]) => {
if (props === undefined) return [];
if (props) {
return [
{
range: {
touch_status: {
gte: 1,
lte: 2,
},
},
},
];
} else
return [
{
range: {
touch_status: {
gte: 3,
lte: 3,
},
},
},
];
};
const handleCity = (value: Props["city"]) => {
if (value === undefined) return [];
return [
{
term: {
city_en: {
value,
},
},
},
];
};
// handle city_direction
const handleCityDirection = (value: Props["city_direction"]) => {
if (value === undefined) return [];
return [
{
term: {
city_direction: {
value,
},
},
},
];
}
const handleQ = (value: Props["q"]) => {
if (value === undefined) return [];
const variants = getNumeralVariants(value);
const should: any[] = [];
for (const q of variants) {
should.push(
{
// exact / full phrase match on the room name — highest relevance
match_phrase: {
name: {
query: q,
boost: 10,
},
},
},
{
match_phrase_prefix: {
name: {
query: q,
max_expansions: 50,
boost: 5,
},
},
},
{
fuzzy: {
name: {
value: q,
fuzziness: "AUTO",
boost: 2,
},
},
},
{
fuzzy: {
brand_name: {
value: q,
fuzziness: "AUTO",
},
},
},
{
match: {
city: q,
},
},
{
match: {
district: q,
},
},
{
match: {
city_direction: q,
},
},
{
match: {
why_try: q,
},
},
{
match: {
scenario: q,
},
},
{
match: {
reasons_to_try: q,
},
},
{
match: {
description: q,
},
}
);
}
return [
{
bool: {
should,
minimum_should_match: 1,
},
},
];
};
const handleRid = (value: Props["rid"]) => {
if (value === undefined) return [];
return [
{
bool: {
should: value.map((id) => ({ term: { id } })),
minimum_should_match: 1,
},
},
];
};
const handleBid = (value: Props["bid"]) => {
if (value === undefined) return [];
return [
{
bool: {
should: value.map((provider_id) => ({ term: { provider_id } })),
},
},
];
};
const handleFreeSans = (props: Props["has_free_sans"]) => {
if (!props) return [];
return [
{
term: {
"sans.is_free": true,
},
},
{
bool: {
must: [{ term: { status: 2 } }],
},
},
];
};
const handleDeals = (props: Props["deals"]) => {
if (!props) return [];
return [
{
term: {
"sans.is_deals": true,
},
},
{
bool: {
must: [{ term: { status: 2 } }],
},
},
];
};
const handleIsDeals = (props: Props["is_deals"]) => {
if (!props) return [];
return [
{
term: {
"is_deals": true,
},
},
{
bool: {
must: [{ term: { status: 2 } }],
},
},
];
};
const handleIsThrill = (props: Props["is_thrill"]) => {
if (!props) return [];
return [
{
term: {
is_thrill: true,
},
},
];
};
const handleIsExperience = (props: Props["is_experience"]) => {
if (!props) return [];
return [
{
term: {
is_experience: true,
},
},
];
};
const handleStatus = (props: Props["status"]) => {
if (props === undefined) return [];
return [
{
term: {
status: props
}
},
{
bool: {
must: [{ term: { status: props } }],
},
},
];
};
const handleLocation = (latitude: Props["latitude"], longitude: Props["longitude"], distance: Props["distance"]) => {
if (latitude === undefined || longitude === undefined) return [];
return [
{
geo_distance: {
distance: distance || "10km",
coordinates: {
lat: latitude,
lon: longitude
}
}
}
];
};
const handleBoundingBox = (north_lat: Props["north_lat"], south_lat: Props["south_lat"], east_lng: Props["east_lng"], west_lng: Props["west_lng"]) => {
if (north_lat === undefined || south_lat === undefined || east_lng === undefined || west_lng === undefined) return [];
return [
{
geo_bounding_box: {
coordinates: {
top_left: {
lat: north_lat,
lon: west_lng
},
bottom_right: {
lat: south_lat,
lon: east_lng
}
}
}
}
];
};
// تابع برای افزودن اعتبارسنجی داینامیک
const addDynamicSchema = async (
schema: ObjectSchema<any>,
data: object
): Promise<ObjectSchema<InferType<typeof schema>>> => {
const shape = schema.fields;
const tags = await sequelize.query<{ name: string; items: string[] }>(
`SELECT
tag_types.filter_name NAME,
json_agg ( tags.english_name ) items
FROM
tags
JOIN tag_types ON tags.tag_type = tag_types.ID
GROUP BY
tag_types.filter_name`,
{
type: QueryTypes.SELECT,
}
);
for (const key in data) {
if (key.startsWith("tg_")) {
shape[key] = array(
string().required(`Field ${key} is required and must be a string`)
).transform((value, originalValue: any | number[]) =>
!Array.isArray(originalValue) ? [originalValue] : originalValue
);
}
}
return object().shape(shape);
};
const filterOrders = [
{
id: 1,
label: "پرفروش ترین",
cmd: {
sell_count: {
order: "desc",
},
},
},
{
id: 2,
label: "رندوم",
cmd: {
random: {
order: "desc",
},
},
},
// { id: 2, label: "پربازدید ترین" },
{
id: 3,
label: "پیشنهاد خریداران",
cmd: {
suggestion_rate: {
order: "desc",
},
},
},
{
id: 4,
label: "ترند",
cmd: {
trend_percentage: {
order: "desc",
},
},
},
{
id: 5,
label: "جدیدترین",
cmd: {
created_at: {
order: "desc",
},
},
},
{
id: 6,
label: "بالاترین امتیاز",
cmd: {
rate: {
order: "desc",
},
},
},
{
id: 7,
label: "وضعیت فعال اول",
cmd: {
_script: {
type: "number",
script: {
source: "params._source.status == 2 ? 1 : 0"
},
order: "desc"
},
},
},
];
export const getElasticSearch = async (
elasticsearchClient: Client,
query: Object
) => {
try {
const allTags = await sequelize.query<{ name: string; items: string[] }>(
`SELECT
tag_types.filter_name NAME,
json_agg ( tags.english_name ) items
FROM
tags
JOIN tag_types ON tags.tag_type = tag_types.ID
WHERE tag_types.is_active IS TRUE AND tag_types.deleted_at IS NULL
GROUP BY
tag_types.filter_name`,
{
type: QueryTypes.SELECT,
}
);
// `all` and empty values mean "no filter for this field" — strip them so
// e.g. `?fear_level=all` returns everything instead of failing oneOf and
// erroring the whole search.
const cleanTagValues = (originalValue: any) => {
const arr = !Array.isArray(originalValue) ? [originalValue] : originalValue;
const cleaned = arr.filter(
(v: any) => v != null && v !== "" && v !== "all"
);
return cleaned.length ? cleaned : undefined;
};
const tagsValidator = allTags.reduce((prev: any, { name, items }) => {
prev[name] = array(string().required().oneOf(items)).transform(
(value, originalValue: any | number[]) => cleanTagValues(originalValue)
);
prev[`ex_${name}`] = array(string().required().oneOf(items)).transform(
(value, originalValue: any | number[]) => cleanTagValues(originalValue)
);
return prev;
}, {});
const {
limit,
page,
sort_by,
age,
bid,
city,
deals,
duration,
fear_range,
hardship_level,
has_free_sans,
has_touch,
number_of_players,
price,
q,
rid,
sans,
city_direction,
is_deals,
is_thrill,
is_experience,
status,
compact,
latitude,
longitude,
distance,
north_lat,
south_lat,
east_lng,
west_lng,
// no_ads,
...rest
} = queryValidator(tagsValidator).validateSync(sanitizeSearchQuery(query), {
stripUnknown: true,
});
const hasTagValue = (value: any) =>
value != null && (!Array.isArray(value) || value.length > 0);
const includeTags = Object.entries(rest)
.filter(([key, value]) => !key.startsWith("ex_") && hasTagValue(value))
.map(([key, value]) => ({
terms: {
["tags." + key]: value,
},
}));
const excludeTags = Object.entries(rest)
.filter(([key, value]) => key.startsWith("ex_") && hasTagValue(value))
.map(([key, value]) => ({
terms: {
["tags." + key.substring(3)]: value, // Remove the "ex_" prefix
},
}));
const body: any = {
from: page * limit,
size: limit,
_source: compact ? compactSearchAttribute : searchAttribute,
query: {
bool: {
must: [
{
bool: {
must_not: [
{ term: { status: 0 } },
{ term: { status: 1 } },
{
exists: {
field: "deleted_at",
},
},
],
},
},
...handlePrice(price),
...handleAge(age),
...handleSans(sans),
...handleNumberPlayers(number_of_players),
...handleDuration(duration),
...handleHardship(hardship_level),
...fearLevel(fear_range),
...handleTouch(has_touch),
...handleCity(city),
...handleCityDirection(city_direction),
...handleQ(q),
...handleRid(rid),
...handleBid(bid),
...handleFreeSans(has_free_sans),
...handleDeals(deals),
...handleIsDeals(is_deals),
...handleIsThrill(is_thrill),
...handleIsExperience(is_experience),
...handleStatus(status),
...handleLocation(latitude, longitude, distance),
...handleBoundingBox(north_lat, south_lat, east_lng, west_lng),
...includeTags,
],
must_not: [...excludeTags],
},
},
sort: [
{
_script: {
type: "number",
script: {
source: "params._source.status == 2 ? 1 : 0"
},
order: "desc"
}
},
// Text search: relevance (_score) must dominate distance so the exact
// name match ranks first. When there's no `q` (browsing/filtering),
// keep distance-first and score later (unchanged behavior).
...(q ? [{ _score: { order: "desc" } }] : []),
...(latitude && longitude ? [{
_geo_distance: {
coordinates: {
lat: latitude,
lon: longitude
},
order: "asc",
unit: "km",
mode: "min",
distance_type: "arc",
ignore_unmapped: true
}
}] : []),
...(q ? [] : [{ _score: { order: "desc" } }]),
...(!!sort_by
? filterOrders
.filter(({ id }) => id === sort_by)
.map(({ cmd }) => cmd)
: []),
{
rate: {
order: "desc",
missing: "_last",
},
},
{
number_of_comments: {
order: "desc",
missing: "_last",
},
},
],
};
const {
hits: { hits, total },
} = await elasticsearchClient.search({
index: "v_rooms",
body,
});
const _total = typeof total === "object" ? total.value : total || 0;
// const adsItem = no_ads === true ? [] : await getAds({ page });
return Promise.resolve({
total: _total,
items: [
// ...adsItem.map((item) => ({ ...item, isAds: true })),
...hits.map(({ _source }) => _source),
],
page: page + 1,
limit,
});
} catch (error: any) {
return Promise.reject(error);
}
};