@@ -21,23 +21,53 @@ function syncAlgolia() {
21
21
const index = client . initIndex ( 'icons' ) ;
22
22
const indexTmp = client . initIndex ( 'icons_tmp' ) ;
23
23
24
- indexTmp . setSettings ( {
25
- searchableAttributes : [ 'unordered(name)' , 'unordered(tags)' ] ,
26
- customRanking : [ 'asc(name)' ] ,
27
- } ) ;
24
+ console . log (
25
+ "Copying target index's settings, synonyms and rules into temporary index..." ,
26
+ ) ;
27
+ scopedCopyIndex ( client , index . indexName , indexTmp . indexName )
28
+ . then ( ( ) => {
29
+ const objects = Object . keys ( icons ) . map ( name => ( {
30
+ name,
31
+ tags : tags [ name ] || [ ] ,
32
+ } ) ) ;
28
33
29
- const records = Object . keys ( icons ) . map ( name => ( {
30
- name,
31
- tags : tags [ name ] || [ ] ,
32
- } ) ) ;
34
+ console . log ( 'Adding objects to the temporary index...' ) ;
35
+ return addObjects ( indexTmp , objects ) ;
36
+ } )
37
+ . then ( ( ) => {
38
+ console . log ( 'Moving temporary index to target index...' ) ;
39
+ return moveIndex ( client , indexTmp . indexName , index . indexName ) ;
40
+ } ) ;
41
+ }
33
42
34
- console . log ( 'Pushing data to the temporary index...' ) ;
35
- indexTmp . addObjects ( records , err => {
36
- if ( err ) throw err ;
43
+ function scopedCopyIndex (
44
+ client ,
45
+ indexNameSrc ,
46
+ indexNameDest ,
47
+ scope = [ 'settings' , 'synonyms' , 'rules' ] ,
48
+ ) {
49
+ return new Promise ( ( resolve , reject ) => {
50
+ client . copyIndex ( indexNameSrc , indexNameDest , scope , ( error , contents ) => {
51
+ if ( error ) reject ( error ) ;
52
+ resolve ( contents ) ;
53
+ } ) ;
37
54
} ) ;
55
+ }
56
+
57
+ function addObjects ( index , objects ) {
58
+ return new Promise ( ( resolve , reject ) => {
59
+ index . addObjects ( objects , ( error , contents ) => {
60
+ if ( error ) reject ( error ) ;
61
+ resolve ( contents ) ;
62
+ } ) ;
63
+ } ) ;
64
+ }
38
65
39
- console . log ( 'Moving temporary index to target index...' ) ;
40
- client . moveIndex ( indexTmp . indexName , index . indexName , err => {
41
- if ( err ) throw err ;
66
+ function moveIndex ( client , indexNameSrc , indexNameDest ) {
67
+ return new Promise ( ( resolve , reject ) => {
68
+ client . moveIndex ( indexNameSrc , indexNameDest , ( error , contents ) => {
69
+ if ( error ) reject ( error ) ;
70
+ resolve ( contents ) ;
71
+ } ) ;
42
72
} ) ;
43
73
}
0 commit comments