2023-12-03 05:11:30 +01:00
|
|
|
import { getConfig } from "@config";
|
|
|
|
|
import chalk from "chalk";
|
|
|
|
|
import { client } from "~database/datasource";
|
|
|
|
|
import { Meilisearch } from "meilisearch";
|
|
|
|
|
|
|
|
|
|
const config = getConfig();
|
|
|
|
|
|
|
|
|
|
export const meilisearch = new Meilisearch({
|
|
|
|
|
host: `${config.meilisearch.host}:${config.meilisearch.port}`,
|
|
|
|
|
apiKey: config.meilisearch.api_key,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const connectMeili = async () => {
|
|
|
|
|
if (!config.meilisearch.enabled) return;
|
|
|
|
|
|
|
|
|
|
if (await meilisearch.isHealthy()) {
|
2023-12-03 05:40:10 +01:00
|
|
|
await meilisearch
|
|
|
|
|
.index(MeiliIndexType.Accounts)
|
|
|
|
|
.updateSortableAttributes(["createdAt"]);
|
|
|
|
|
|
|
|
|
|
await meilisearch
|
|
|
|
|
.index(MeiliIndexType.Accounts)
|
|
|
|
|
.updateSearchableAttributes(["username", "displayName", "note"]);
|
|
|
|
|
|
|
|
|
|
await meilisearch
|
|
|
|
|
.index(MeiliIndexType.Statuses)
|
|
|
|
|
.updateSortableAttributes(["createdAt"]);
|
|
|
|
|
|
|
|
|
|
await meilisearch
|
|
|
|
|
.index(MeiliIndexType.Statuses)
|
|
|
|
|
.updateSearchableAttributes(["content"]);
|
|
|
|
|
|
2023-12-03 05:11:30 +01:00
|
|
|
console.log(
|
|
|
|
|
`${chalk.green(`✓`)} ${chalk.bold(`Connected to Meilisearch`)}`
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
console.error(
|
|
|
|
|
`${chalk.red(`✗`)} ${chalk.bold(
|
|
|
|
|
`Error while connecting to Meilisearch`
|
|
|
|
|
)}`
|
|
|
|
|
);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-12-03 05:40:10 +01:00
|
|
|
export enum MeiliIndexType {
|
2023-12-03 05:11:30 +01:00
|
|
|
Accounts = "accounts",
|
|
|
|
|
Statuses = "statuses",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const getNthDatabaseAccountBatch = (
|
|
|
|
|
n: number,
|
|
|
|
|
batchSize = 1000
|
2023-12-03 05:40:10 +01:00
|
|
|
): Promise<Record<string, string | Date>[]> => {
|
2023-12-03 05:11:30 +01:00
|
|
|
return client.user.findMany({
|
|
|
|
|
skip: n * batchSize,
|
|
|
|
|
take: batchSize,
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
username: true,
|
|
|
|
|
displayName: true,
|
|
|
|
|
note: true,
|
2023-12-03 05:40:10 +01:00
|
|
|
createdAt: true,
|
|
|
|
|
},
|
|
|
|
|
orderBy: {
|
|
|
|
|
createdAt: "asc",
|
2023-12-03 05:11:30 +01:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getNthDatabaseStatusBatch = (
|
|
|
|
|
n: number,
|
|
|
|
|
batchSize = 1000
|
2023-12-03 05:40:10 +01:00
|
|
|
): Promise<Record<string, string | Date>[]> => {
|
2023-12-03 05:11:30 +01:00
|
|
|
return client.status.findMany({
|
|
|
|
|
skip: n * batchSize,
|
|
|
|
|
take: batchSize,
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
content: true,
|
2023-12-03 05:40:10 +01:00
|
|
|
createdAt: true,
|
|
|
|
|
},
|
|
|
|
|
orderBy: {
|
|
|
|
|
createdAt: "asc",
|
2023-12-03 05:11:30 +01:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const rebuildSearchIndexes = async (
|
2023-12-03 05:40:10 +01:00
|
|
|
indexes: MeiliIndexType[],
|
2023-12-03 05:11:30 +01:00
|
|
|
batchSize = 100
|
|
|
|
|
) => {
|
2023-12-03 05:40:10 +01:00
|
|
|
if (indexes.includes(MeiliIndexType.Accounts)) {
|
2023-12-03 05:11:30 +01:00
|
|
|
const accountCount = await client.user.count();
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < accountCount / batchSize; i++) {
|
|
|
|
|
const accounts = await getNthDatabaseAccountBatch(i, batchSize);
|
|
|
|
|
|
|
|
|
|
const progress = Math.round((i / (accountCount / batchSize)) * 100);
|
|
|
|
|
|
|
|
|
|
console.log(`${chalk.green(`✓`)} ${progress}%`);
|
|
|
|
|
|
|
|
|
|
// Sync with Meilisearch
|
|
|
|
|
await meilisearch
|
2023-12-03 05:40:10 +01:00
|
|
|
.index(MeiliIndexType.Accounts)
|
2023-12-03 05:11:30 +01:00
|
|
|
.addDocuments(accounts);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-03 05:40:10 +01:00
|
|
|
const meiliAccountCount = (
|
|
|
|
|
await meilisearch.index(MeiliIndexType.Accounts).getStats()
|
|
|
|
|
).numberOfDocuments;
|
2023-12-03 05:11:30 +01:00
|
|
|
|
2023-12-03 05:40:10 +01:00
|
|
|
console.log(
|
|
|
|
|
`${chalk.green(`✓`)} ${chalk.bold(
|
|
|
|
|
`Done! ${meiliAccountCount} accounts indexed`
|
|
|
|
|
)}`
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-12-03 05:11:30 +01:00
|
|
|
|
2023-12-03 05:40:10 +01:00
|
|
|
if (indexes.includes(MeiliIndexType.Statuses)) {
|
2023-12-03 05:11:30 +01:00
|
|
|
const statusCount = await client.status.count();
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < statusCount / batchSize; i++) {
|
|
|
|
|
const statuses = await getNthDatabaseStatusBatch(i, batchSize);
|
|
|
|
|
|
|
|
|
|
const progress = Math.round((i / (statusCount / batchSize)) * 100);
|
|
|
|
|
|
|
|
|
|
console.log(`${chalk.green(`✓`)} ${progress}%`);
|
|
|
|
|
|
|
|
|
|
// Sync with Meilisearch
|
|
|
|
|
await meilisearch
|
2023-12-03 05:40:10 +01:00
|
|
|
.index(MeiliIndexType.Statuses)
|
2023-12-03 05:11:30 +01:00
|
|
|
.addDocuments(statuses);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-03 05:40:10 +01:00
|
|
|
const meiliStatusCount = (
|
|
|
|
|
await meilisearch.index(MeiliIndexType.Statuses).getStats()
|
|
|
|
|
).numberOfDocuments;
|
|
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
|
`${chalk.green(`✓`)} ${chalk.bold(
|
|
|
|
|
`Done! ${meiliStatusCount} statuses indexed`
|
|
|
|
|
)}`
|
|
|
|
|
);
|
2023-12-03 05:11:30 +01:00
|
|
|
}
|
|
|
|
|
};
|