-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* first working stream * player and elastic for playlists releases and tracks Co-authored-by: abdelillah <aghomari.professionnel@gmail.com>
- Loading branch information
1 parent
3fe4f7b
commit 10517ad
Showing
37 changed files
with
892 additions
and
108 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
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
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
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,6 @@ | ||
interface IReleaseSearchBody { | ||
id: string; | ||
title: string; | ||
} | ||
|
||
export default IReleaseSearchBody; |
12 changes: 12 additions & 0 deletions
12
src/playlists/interfaces/playlist-search-result.interface.ts
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 @@ | ||
import IPlaylistSearchBody from '../../releases/interfaces/release-search-body.interface'; | ||
|
||
interface IPlaylistSearchResult { | ||
hits: { | ||
total: number; | ||
hits: Array<{ | ||
_source: IPlaylistSearchBody; | ||
}>; | ||
}; | ||
} | ||
|
||
export default IPlaylistSearchResult; |
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,98 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { ElasticsearchService } from '@nestjs/elasticsearch'; | ||
import IPlaylistSearchBody from '../releases/interfaces/release-search-body.interface'; | ||
import { ISearchService } from '../search/interfaces/search.service.interface'; | ||
import { PlaylistDocument } from './schemas/playlist.schema'; | ||
|
||
@Injectable() | ||
export default class PlaylistsSearchService | ||
implements ISearchService<PlaylistDocument> | ||
{ | ||
index = 'playlist'; | ||
private readonly logger: Logger = new Logger(PlaylistsSearchService.name); | ||
|
||
constructor(private readonly elasticsearchService: ElasticsearchService) {} | ||
|
||
async insertIndex(playlist: PlaylistDocument): Promise<any> { | ||
this.logger.log(`Inserting user ID "${playlist._id}"`); | ||
return await this.elasticsearchService.index<IPlaylistSearchBody>({ | ||
index: this.index, | ||
body: { | ||
id: playlist._id, | ||
title: playlist.title, | ||
}, | ||
}); | ||
} | ||
async searchIndex(text: string): Promise<any[]> { | ||
this.logger.log(`Searching "${text}"`); | ||
|
||
const { hits } = | ||
await this.elasticsearchService.search<IPlaylistSearchBody>({ | ||
index: this.index, | ||
body: { | ||
query: { | ||
bool: { | ||
should: [ | ||
{ | ||
match: { | ||
title: { | ||
query: text, | ||
fuzziness: 'AUTO', | ||
}, | ||
}, | ||
}, | ||
{ | ||
match_phrase_prefix: { | ||
title: { | ||
query: text, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}); | ||
return hits.hits.map((item) => item._source); | ||
} | ||
updateIndex(playlist: PlaylistDocument): Promise<any> { | ||
this.logger.log(`Searching "${playlist._id}"`); | ||
|
||
const newBody: IPlaylistSearchBody = { | ||
id: playlist._id, | ||
title: playlist.title, | ||
}; | ||
|
||
const script: string = Object.entries(newBody).reduce( | ||
(result, [key, value]) => { | ||
return `${result} ctx._source.${key}='${value}';`; | ||
}, | ||
'', | ||
); | ||
|
||
return this.elasticsearchService.updateByQuery({ | ||
index: this.index, | ||
body: { | ||
query: { | ||
match: { | ||
id: playlist._id, | ||
}, | ||
}, | ||
script: script, | ||
}, | ||
}); | ||
} | ||
deleteIndex(id: string): void { | ||
this.logger.log(`Deleting user "${id}" index `); | ||
this.elasticsearchService.deleteByQuery({ | ||
index: this.index, | ||
body: { | ||
query: { | ||
match: { | ||
id: id, | ||
}, | ||
}, | ||
}, | ||
}); | ||
} | ||
} |
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
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.