Skip to content

Commit aa6f4e7

Browse files
committed
refactor: clean the algolia service
1 parent 457414e commit aa6f4e7

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

src/app/core/algolia/algolia.service.ts

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,37 @@
1-
import { Observable, Subject } from 'rxjs';
2-
import { Injectable, Inject } from '@angular/core';
1+
import { Inject, Injectable } from '@angular/core';
32
import algoliasearchHelper from 'algoliasearch-helper';
4-
import { ALGOLIA_APPLICATION_ID, ALGOLIA_SEARCH_API_KEY, ALGOLIA_INDEX } from './injection-tokens';
3+
import { Observable } from 'rxjs';
4+
import { ALGOLIA_APPLICATION_ID, ALGOLIA_INDEX, ALGOLIA_SEARCH_API_KEY } from './injection-tokens';
5+
import { SearchResult } from 'src/app/search/search-result/search-result.component';
56

67
export interface SearchState {
78
search$: Observable<any>;
8-
result$: Observable<any>;
9+
result$: Observable<SearchResult>;
910
change$: Observable<any>;
1011
error$: Observable<any>;
1112
}
1213

13-
/**
14-
* The Algolia search strategy. This implementtion uses three indices based on the sort options:
15-
* - `applications_by_price_asc`: used for retreiving entries sorted by price (in ascending order), ie. Free apps first.
16-
* - `applications_by_rating_desc`: used for retreiving entries sorted by rating (in descending order), ie. Popular apps first.
17-
* - `applications` (default): used for retreiving entries sorted by the default Algolia ranking algorithm, ie. Relevance.
18-
*/
1914
@Injectable({
2015
providedIn: 'root'
2116
})
2217
export class AlgoliaService {
2318
indices = {
24-
applications: algoliasearchHelper,
25-
applications_by_rating_desc: algoliasearchHelper,
26-
applications_by_price_asc: algoliasearchHelper
19+
'npm-search': algoliasearchHelper
2720
};
28-
client;
21+
22+
client: any;
2923

3024
/**
3125
* A map of the different search states: `search$`, `result$`, `change$`, `error$`.
3226
*/
3327
searchState: SearchState;
3428

3529
constructor(
36-
@Inject(ALGOLIA_APPLICATION_ID) private applicationID,
37-
@Inject(ALGOLIA_SEARCH_API_KEY) private searchApiKey,
38-
@Inject(ALGOLIA_INDEX) private indexName
30+
@Inject(ALGOLIA_APPLICATION_ID) private applicationID: string,
31+
@Inject(ALGOLIA_SEARCH_API_KEY) private searchApiKey: string,
32+
@Inject(ALGOLIA_INDEX) private indexName: string
3933
) {
40-
this.client = window['algoliasearch'](applicationID, searchApiKey);
34+
this.client = window['algoliasearch'](this.applicationID, this.searchApiKey);
4135

4236
this.searchState = {} as any;
4337

@@ -51,7 +45,12 @@ export class AlgoliaService {
5145
*/
5246
private configureMasterIndex(indexName: string) {
5347
this.indices[indexName] = algoliasearchHelper(this.client, indexName, {
54-
disjunctiveFacets: []
48+
facets: ['downloadsLast30Days'],
49+
hierarchicalFacets: [{
50+
name: 'products',
51+
attributes: ['downloadsLast30Days'],
52+
sortBy: ['count:desc', 'name:asc'] // first show the most common values, then sort by name
53+
}]
5554
});
5655

5756
this.searchState = {} as SearchState;
@@ -103,15 +102,15 @@ export class AlgoliaService {
103102
}
104103

105104
/**
106-
* Search the next page (used for the inifinite scroll feature)
105+
* Search the next page (used for the infinite scrolling feature)
107106
*/
108107
nextPage() {
109108
// request the next page only if the current query is not empty!
110109
if (this.indices[this.indexName].state.query !== '') {
111110
this.indices[this.indexName].nextPage().search();
112111
}
113112
}
114-
sortByRelevance(query: string) {
113+
filterByRelevance(query: string) {
115114
// query = query || this.indices[this.indexName].state.query;
116115
this.search(this.indexName, query);
117116
}
@@ -120,4 +119,12 @@ export class AlgoliaService {
120119
// query = query || this.indices[this.indexName].state.query;
121120
this.search(this.indexName, query, 'AND (computedKeywords:angular-cli-schematic)');
122121
}
122+
123+
sortByBestMatch(query: string) {
124+
this.search(this.indexName, query);
125+
}
126+
127+
sortByMostDownloaded(query: string) {
128+
this.search(this.indexName, query);
129+
}
123130
}

0 commit comments

Comments
 (0)