From 20f4fdbd397b90b2547da4c75ab0983273aaa5ab Mon Sep 17 00:00:00 2001 From: "Maxence G. de Montauzan" Date: Sat, 23 Sep 2023 00:21:15 +0200 Subject: [PATCH] (typesense) Suggester --- dashboard/src/app/ts-service/ts.service.ts | 68 +++++++++++----------- 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/dashboard/src/app/ts-service/ts.service.ts b/dashboard/src/app/ts-service/ts.service.ts index 3cb397d..424ddb2 100644 --- a/dashboard/src/app/ts-service/ts.service.ts +++ b/dashboard/src/app/ts-service/ts.service.ts @@ -22,6 +22,7 @@ export class TsService { protected static readonly ACTION_COUNT = "/_count"; protected tsUrl = "http://localhost:8108/collections"; + protected tsUrlPure = "http://localhost:8108/"; protected headers = new HttpHeaders({ "Content-Type": "application/json", "X-TYPESENSE-API-KEY": "toto", @@ -300,37 +301,34 @@ export class TsService { } getSuggest(text: string): Observable { + let queryParameters = new HttpParams(); + queryParameters = queryParameters.append("query_by", "Name"); + queryParameters = queryParameters.append( + "sort_by", + "_text_match:desc,Play Count:desc" + ); + queryParameters = queryParameters.append("limit", 5); console.log("search sugget: " + text); + return this.http .post( - this.tsUrl + TsService.SUGGEST_INDEX_NAME + TsService.ACTION_SEARCH, + this.tsUrlPure + "multi_search", JSON.stringify({ - _source: ["album", "artist"], - suggest: { - "album-suggest": { - prefix: text, - completion: { - field: "album_suggest", - }, + searches: [ + { + collection: "albums", + q: text, }, - "artist-suggest": { - prefix: text, - completion: { - field: "artist_suggest", - }, + { + collection: "artists", + q: text, }, - }, + ], }), - { headers: this.headers } + { headers: this.headers, params: queryParameters } ) .pipe( - map((res) => - this.responseSuggesterToSuggested( - res, - "album-suggest", - "artist-suggest" - ) - ), + map((res) => this.responseSuggesterToSuggested(res)), catchError((error) => this.handleError(error, "getSuggest(" + text + ")") ) @@ -410,21 +408,21 @@ export class TsService { return result; } - protected responseSuggesterToSuggested( - res: any, - ...suggestName: string[] - ): Suggested[] { - const result: Array = []; - suggestName.forEach((sname) => { - res["suggest"][sname][0]["options"].forEach((option) => { - let suggest = new Suggested(); - // TODO If more than one key, raise exception - suggest.type = String(Object.keys(option["_source"])); - suggest.name = option["_source"][suggest.type]; - result.push(suggest); + protected responseSuggesterToSuggested(res: any): Suggested[] { + const suggesteds: Array = []; + + res.results.forEach((result) => { + let type = result.request_params.collection_name; + type = type.slice(0, type.length - 1); + + result.hits.forEach((hit) => { + let suggested = new Suggested(); + suggested.type = type; + suggested.name = hit.document.Name; + suggesteds.push(suggested); }); }); - return result; + return suggesteds; } protected handleError(error: any, origin: string): Promise {