1
- import { Observable , Subject } from 'rxjs' ;
2
- import { Injectable , Inject } from '@angular/core' ;
1
+ import { Inject , Injectable } from '@angular/core' ;
3
2
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' ;
5
6
6
7
export interface SearchState {
7
8
search$ : Observable < any > ;
8
- result$ : Observable < any > ;
9
+ result$ : Observable < SearchResult > ;
9
10
change$ : Observable < any > ;
10
11
error$ : Observable < any > ;
11
12
}
12
13
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
- */
19
14
@Injectable ( {
20
15
providedIn : 'root'
21
16
} )
22
17
export class AlgoliaService {
23
18
indices = {
24
- applications : algoliasearchHelper ,
25
- applications_by_rating_desc : algoliasearchHelper ,
26
- applications_by_price_asc : algoliasearchHelper
19
+ 'npm-search' : algoliasearchHelper
27
20
} ;
28
- client ;
21
+
22
+ client : any ;
29
23
30
24
/**
31
25
* A map of the different search states: `search$`, `result$`, `change$`, `error$`.
32
26
*/
33
27
searchState : SearchState ;
34
28
35
29
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
39
33
) {
40
- this . client = window [ 'algoliasearch' ] ( applicationID , searchApiKey ) ;
34
+ this . client = window [ 'algoliasearch' ] ( this . applicationID , this . searchApiKey ) ;
41
35
42
36
this . searchState = { } as any ;
43
37
@@ -51,7 +45,12 @@ export class AlgoliaService {
51
45
*/
52
46
private configureMasterIndex ( indexName : string ) {
53
47
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
+ } ]
55
54
} ) ;
56
55
57
56
this . searchState = { } as SearchState ;
@@ -103,15 +102,15 @@ export class AlgoliaService {
103
102
}
104
103
105
104
/**
106
- * Search the next page (used for the inifinite scroll feature)
105
+ * Search the next page (used for the infinite scrolling feature)
107
106
*/
108
107
nextPage ( ) {
109
108
// request the next page only if the current query is not empty!
110
109
if ( this . indices [ this . indexName ] . state . query !== '' ) {
111
110
this . indices [ this . indexName ] . nextPage ( ) . search ( ) ;
112
111
}
113
112
}
114
- sortByRelevance ( query : string ) {
113
+ filterByRelevance ( query : string ) {
115
114
// query = query || this.indices[this.indexName].state.query;
116
115
this . search ( this . indexName , query ) ;
117
116
}
@@ -120,4 +119,12 @@ export class AlgoliaService {
120
119
// query = query || this.indices[this.indexName].state.query;
121
120
this . search ( this . indexName , query , 'AND (computedKeywords:angular-cli-schematic)' ) ;
122
121
}
122
+
123
+ sortByBestMatch ( query : string ) {
124
+ this . search ( this . indexName , query ) ;
125
+ }
126
+
127
+ sortByMostDownloaded ( query : string ) {
128
+ this . search ( this . indexName , query ) ;
129
+ }
123
130
}
0 commit comments