2023-05-25 07:53:04 +00:00
|
|
|
import {Health, MeiliSearch, Stats } from 'meilisearch';
|
2023-05-24 22:55:33 +00:00
|
|
|
import { dbLogger } from "./logger.js";
|
|
|
|
|
|
|
|
import config from "@/config/index.js";
|
|
|
|
import {Note} from "@/models/entities/note";
|
|
|
|
import {normalizeForSearch} from "@/misc/normalize-for-search";
|
|
|
|
|
|
|
|
const logger = dbLogger.createSubLogger("meilisearch", "gray", false);
|
|
|
|
|
|
|
|
logger.info("Connecting to MeiliSearch");
|
|
|
|
|
|
|
|
const hasConfig =
|
|
|
|
config.meilisearch && (config.meilisearch.host || config.meilisearch.port || config.meilisearch.apiKey);
|
|
|
|
|
|
|
|
const host = hasConfig ? config.meilisearch.host ?? "localhost" : "";
|
|
|
|
const port = hasConfig ? config.meilisearch.port ?? 7700 : 0;
|
|
|
|
const auth = hasConfig ? config.meilisearch.apiKey ?? "" : "";
|
|
|
|
|
2023-05-25 07:53:04 +00:00
|
|
|
const client : MeiliSearch = new MeiliSearch({
|
2023-05-25 00:19:42 +00:00
|
|
|
host: `http://${host}:${port}`,
|
|
|
|
apiKey: auth,
|
2023-05-24 22:55:33 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
const posts = client.index('posts');
|
|
|
|
|
|
|
|
posts.updateSearchableAttributes(['text']);
|
|
|
|
|
|
|
|
logger.info("Connected to MeiliSearch");
|
|
|
|
|
|
|
|
|
|
|
|
export type MeilisearchNote = {
|
|
|
|
id: string;
|
|
|
|
text: string;
|
|
|
|
userId: string;
|
|
|
|
userHost: string;
|
|
|
|
channelId: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default hasConfig ? {
|
|
|
|
search: (query : string, limit : number, offset : number) => {
|
|
|
|
logger.info(`Searching for ${query}`);
|
2023-05-25 00:19:42 +00:00
|
|
|
logger.info(`Limit: ${limit}`);
|
|
|
|
logger.info(`Offset: ${offset}`);
|
2023-05-24 22:55:33 +00:00
|
|
|
|
|
|
|
return posts.search(query, {
|
|
|
|
limit: limit,
|
|
|
|
offset: offset,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
ingestNote: (note : Note) => {
|
|
|
|
logger.info("Indexing note in MeiliSearch: " + note.id);
|
|
|
|
|
|
|
|
return posts.addDocuments([
|
|
|
|
{
|
|
|
|
id: note.id.toString(),
|
|
|
|
text: note.text,
|
|
|
|
userId: note.userId,
|
|
|
|
userHost: note.userHost,
|
|
|
|
channelId: note.channelId,
|
|
|
|
}
|
2023-05-25 07:53:04 +00:00
|
|
|
]);
|
2023-05-24 22:55:33 +00:00
|
|
|
},
|
2023-05-25 07:53:04 +00:00
|
|
|
serverStats: async () => {
|
|
|
|
let health : Health = await client.health();
|
|
|
|
let stats: Stats = await client.getStats();
|
|
|
|
|
|
|
|
return {
|
|
|
|
health: health.status,
|
|
|
|
size: stats.databaseSize,
|
|
|
|
indexed_count: stats.indexes["posts"].numberOfDocuments
|
|
|
|
}
|
|
|
|
}
|
2023-05-24 22:55:33 +00:00
|
|
|
} : null;
|