-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
440 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Inject, Injectable } from '@nestjs/common' | ||
import { ConfigType } from '@nestjs/config' | ||
import meilisearchConfig from '../config/meilisearch.config' | ||
import { Index, MeiliSearch, Settings } from 'meilisearch' | ||
import { MessageIndex } from 'src/types/indexes' | ||
import deepEqual from 'deep-equal' | ||
|
||
@Injectable() | ||
export class MeiliSearchService { | ||
private client: MeiliSearch | ||
private indexPrefix: string | ||
private messagesIndex: Index<MessageIndex> | ||
|
||
constructor( | ||
@Inject(meilisearchConfig.KEY) | ||
msConfig: ConfigType<typeof meilisearchConfig>, | ||
) { | ||
this.client = new MeiliSearch(msConfig) | ||
this.indexPrefix = msConfig.indexPrefix | ||
this.messagesIndex = this.client.index<MessageIndex>( | ||
`${this.indexPrefix}messages`, | ||
) | ||
} | ||
|
||
async migrate(): Promise<void> { | ||
const settings: Settings = { | ||
searchableAttributes: ['text'], | ||
filterableAttributes: ['chatId', 'senderId'], | ||
} | ||
const sortableAttributes = [ | ||
'words', | ||
'sort', | ||
'typo', | ||
'proximity', | ||
'exactness', | ||
'timestamp:desc', | ||
] | ||
|
||
const currentSettings = await this.messagesIndex.getSettings() | ||
for (const key of Object.keys(settings)) { | ||
if (!deepEqual(currentSettings[key], settings[key])) { | ||
await this.messagesIndex.updateSettings(settings) | ||
break | ||
} | ||
} | ||
|
||
const currentSortableAttributes = | ||
await this.messagesIndex.getSortableAttributes() | ||
if (!deepEqual(currentSortableAttributes, sortableAttributes)) { | ||
await this.messagesIndex.updateSortableAttributes(sortableAttributes) | ||
} | ||
} | ||
|
||
async importMessages(messages: MessageIndex[]): Promise<void> { | ||
await this.messagesIndex.addDocuments(messages) | ||
} | ||
|
||
async search(query: string, chatId: number, senderId?: number) { | ||
const result = await this.messagesIndex.search<MessageIndex>(query, { | ||
filter: [ | ||
`chatId = ${chatId}`, | ||
...[senderId == null ? [] : [`senderId = ${senderId}`]], | ||
], | ||
}) | ||
return result | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { Module } from '@nestjs/common' | ||
import { MeiliSearchService } from './meili-search/meili-search.service'; | ||
import { MeiliSearchService } from './meili-search.service' | ||
|
||
@Module({ | ||
providers: [MeiliSearchService] | ||
providers: [MeiliSearchService], | ||
}) | ||
export class SearchModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export type MessageIndex = { | ||
id: string | ||
chatId: number | ||
senderId: number | ||
senderName: string | ||
/** searchable text */ | ||
text: string | ||
type: 'text' | 'image' | 'sticker' | 'document' | 'video' | 'service' | string | ||
raw: string | ||
from: 'import' | 'bot' | ||
timestamp: number | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.