Add basic advanced search + attachment metadata hints

This commit is contained in:
PrivateGER 2023-05-25 14:15:13 +02:00 committed by PrivateGER
parent c7c21f5c16
commit 03cd2de8ec
1 changed files with 44 additions and 2 deletions

View File

@ -3,7 +3,6 @@ import { dbLogger } from "./logger.js";
import config from "@/config/index.js"; import config from "@/config/index.js";
import {Note} from "@/models/entities/note"; import {Note} from "@/models/entities/note";
import {normalizeForSearch} from "@/misc/normalize-for-search";
const logger = dbLogger.createSubLogger("meilisearch", "gray", false); const logger = dbLogger.createSubLogger("meilisearch", "gray", false);
@ -16,7 +15,7 @@ const host = hasConfig ? config.meilisearch.host ?? "localhost" : "";
const port = hasConfig ? config.meilisearch.port ?? 7700 : 0; const port = hasConfig ? config.meilisearch.port ?? 7700 : 0;
const auth = hasConfig ? config.meilisearch.apiKey ?? "" : ""; const auth = hasConfig ? config.meilisearch.apiKey ?? "" : "";
const client : MeiliSearch = new MeiliSearch({ const client: MeiliSearch = new MeiliSearch({
host: `http://${host}:${port}`, host: `http://${host}:${port}`,
apiKey: auth, apiKey: auth,
}) })
@ -25,6 +24,8 @@ const posts = client.index('posts');
posts.updateSearchableAttributes(['text']); posts.updateSearchableAttributes(['text']);
posts.updateFilterableAttributes(["userId", "userHost", "mediaAttachment"])
logger.info("Connected to MeiliSearch"); logger.info("Connected to MeiliSearch");
@ -34,6 +35,7 @@ export type MeilisearchNote = {
userId: string; userId: string;
userHost: string; userHost: string;
channelId: string; channelId: string;
mediaAttachment: string;
} }
export default hasConfig ? { export default hasConfig ? {
@ -42,14 +44,53 @@ export default hasConfig ? {
logger.info(`Limit: ${limit}`); logger.info(`Limit: ${limit}`);
logger.info(`Offset: ${offset}`); logger.info(`Offset: ${offset}`);
/// Advanced search syntax
/// from:user => filter by user + optional domain
/// has:image/video/audio/text/file => filter by attachment types
/// domain:domain.com => filter by domain
let constructedFilters: string[] = [];
let splitSearch = query.split(" ");
splitSearch.forEach(term => {
if (term.startsWith("has:")) {
let fileType = term.slice(4);
constructedFilters.push(`mediaAttachment = "${fileType}"`)
}
if (term.startsWith("from:")) {
let user = term.slice(5);
constructedFilters.push(`userId = ${user}`)
}
if (term.startsWith("domain:")) {
let domain = term.slice(7);
constructedFilters.push(`userHost = ${domain}`)
}
})
return posts.search(query, { return posts.search(query, {
limit: limit, limit: limit,
offset: offset, offset: offset,
filter: constructedFilters
}); });
}, },
ingestNote: (note : Note) => { ingestNote: (note : Note) => {
logger.info("Indexing note in MeiliSearch: " + note.id); logger.info("Indexing note in MeiliSearch: " + note.id);
let attachmentType = "";
if (note.attachedFileTypes.length > 0) {
attachmentType = note.attachedFileTypes[0].split("/")[0];
switch (attachmentType) {
case "image":
case "video":
case "audio":
case "text":
break;
default:
attachmentType = "file"
break
}
}
return posts.addDocuments([ return posts.addDocuments([
{ {
id: note.id.toString(), id: note.id.toString(),
@ -57,6 +98,7 @@ export default hasConfig ? {
userId: note.userId, userId: note.userId,
userHost: note.userHost, userHost: note.userHost,
channelId: note.channelId, channelId: note.channelId,
mediaAttachment: attachmentType
} }
]); ]);
}, },