Skip to content

Commit

Permalink
feat: add simple search
Browse files Browse the repository at this point in the history
  • Loading branch information
oott123 committed Oct 12, 2021
1 parent 33a929b commit e9096c9
Show file tree
Hide file tree
Showing 8 changed files with 440 additions and 32 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"@nestjs/config": "^1.0.2",
"@nestjs/core": "^8.0.0",
"@nestjs/platform-express": "^8.0.0",
"deep-equal": "^2.0.5",
"meilisearch": "^0.22.0",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.2.0"
Expand All @@ -33,6 +35,7 @@
"@nestjs/cli": "^8.0.0",
"@nestjs/schematics": "^8.0.0",
"@nestjs/testing": "^8.0.0",
"@types/deep-equal": "^1.0.1",
"@types/express": "^4.17.13",
"@types/jest": "^27.0.1",
"@types/node": "^16.0.0",
Expand Down
67 changes: 67 additions & 0 deletions src/search/meili-search.service.ts
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
}
}
18 changes: 0 additions & 18 deletions src/search/meili-search/meili-search.service.spec.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/search/meili-search/meili-search.service.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/search/search.module.ts
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 {}
12 changes: 12 additions & 0 deletions src/types/indexes.ts
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
}
7 changes: 4 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"strict": true,
"skipLibCheck": true,
"strictNullChecks": false,
"strictNullChecks": true,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"strictBindCallApply": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": false
}
}
Loading

0 comments on commit e9096c9

Please sign in to comment.