-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.ts
93 lines (85 loc) · 1.76 KB
/
search.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { client } from "./client";
interface Search {
query?: string;
year?: number;
nsfw?: boolean;
type?: string[];
origin?: string[];
priceRange?: {
min: number;
max: number;
};
}
async function searchCard(query: Search) {
const q = `
with
query := <optional str>$query,
res := (
select fts::search(Meme, query ?? "", language := 'eng')
)
select res.object.shortId
order by res.score desc;
`;
const res = await client.query<number>(
`
${q}`,
{
query: query.query,
},
);
const bigQuery = `
select BinAuction {
price,
card: {
meme: {
shortId,
name,
description
}
}
} filter ${opGenerator(query, res)}
`;
let opts: Record<string, any> | undefined = undefined;
if (query.origin) {
if (!opts) opts = {};
opts.origin = query.origin;
}
if (query.type) {
if (!opts) opts = {};
opts.type = query.type;
}
console.log(await client.query(bigQuery, opts));
}
await searchCard({
query: "meme",
origin: ["Caio Slikta"],
priceRange: {
min: 20,
max: 1000,
},
});
function opGenerator(search: Search, shortIds?: number[]) {
let p = `.card.meme.`;
const ops = [];
if (search.nsfw) {
ops.push(`${p}nsfw = ${search.nsfw}`);
}
if (search.origin) {
ops.push(`contains(<array<str>>$origin, ${p}origin)`);
}
if (search.type) {
ops.push(`contains(<array<str>>$type, ${p}type)`);
}
if (search.year) {
ops.push(`${p}year = ${search.year}`);
}
if (search.query) {
ops.push(`contains(<array<int64>>[${shortIds?.join(",")}], ${p}shortId)`);
}
if (search.priceRange) {
ops.push(
`.price >= ${search.priceRange.min} AND .price <= ${search.priceRange.max}`,
);
}
return ops.join(" AND ");
}