(typesense) Suggester

This commit is contained in:
2023-09-23 00:21:15 +02:00
parent b789c925c4
commit 20f4fdbd39

View File

@@ -22,6 +22,7 @@ export class TsService {
protected static readonly ACTION_COUNT = "/_count"; protected static readonly ACTION_COUNT = "/_count";
protected tsUrl = "http://localhost:8108/collections"; protected tsUrl = "http://localhost:8108/collections";
protected tsUrlPure = "http://localhost:8108/";
protected headers = new HttpHeaders({ protected headers = new HttpHeaders({
"Content-Type": "application/json", "Content-Type": "application/json",
"X-TYPESENSE-API-KEY": "toto", "X-TYPESENSE-API-KEY": "toto",
@@ -300,37 +301,34 @@ export class TsService {
} }
getSuggest(text: string): Observable<Suggested[]> { getSuggest(text: string): Observable<Suggested[]> {
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); console.log("search sugget: " + text);
return this.http return this.http
.post<any>( .post<any>(
this.tsUrl + TsService.SUGGEST_INDEX_NAME + TsService.ACTION_SEARCH, this.tsUrlPure + "multi_search",
JSON.stringify({ JSON.stringify({
_source: ["album", "artist"], searches: [
suggest: { {
"album-suggest": { collection: "albums",
prefix: text, q: text,
completion: {
field: "album_suggest",
},
},
"artist-suggest": {
prefix: text,
completion: {
field: "artist_suggest",
},
}, },
{
collection: "artists",
q: text,
}, },
],
}), }),
{ headers: this.headers } { headers: this.headers, params: queryParameters }
) )
.pipe( .pipe(
map((res) => map((res) => this.responseSuggesterToSuggested(res)),
this.responseSuggesterToSuggested(
res,
"album-suggest",
"artist-suggest"
)
),
catchError((error) => catchError((error) =>
this.handleError(error, "getSuggest(" + text + ")") this.handleError(error, "getSuggest(" + text + ")")
) )
@@ -410,21 +408,21 @@ export class TsService {
return result; return result;
} }
protected responseSuggesterToSuggested( protected responseSuggesterToSuggested(res: any): Suggested[] {
res: any, const suggesteds: Array<Suggested> = [];
...suggestName: string[]
): Suggested[] { res.results.forEach((result) => {
const result: Array<Suggested> = []; let type = result.request_params.collection_name;
suggestName.forEach((sname) => { type = type.slice(0, type.length - 1);
res["suggest"][sname][0]["options"].forEach((option) => {
let suggest = new Suggested(); result.hits.forEach((hit) => {
// TODO If more than one key, raise exception let suggested = new Suggested();
suggest.type = String(Object.keys(option["_source"])); suggested.type = type;
suggest.name = option["_source"][suggest.type]; suggested.name = hit.document.Name;
result.push(suggest); suggesteds.push(suggested);
}); });
}); });
return result; return suggesteds;
} }
protected handleError(error: any, origin: string): Promise<any> { protected handleError(error: any, origin: string): Promise<any> {