@@ -19,6 +19,16 @@ module.exports = {
19
19
async init ( ) {
20
20
WIKI . logger . info ( `(SEARCH/ELASTICSEARCH) Initializing...` )
21
21
switch ( this . config . apiVersion ) {
22
+ case '8.x' :
23
+ const { Client : Client8 } = require ( 'elasticsearch8' )
24
+ this . client = new Client8 ( {
25
+ nodes : this . config . hosts . split ( ',' ) . map ( _ . trim ) ,
26
+ sniffOnStart : this . config . sniffOnStart ,
27
+ sniffInterval : ( this . config . sniffInterval > 0 ) ? this . config . sniffInterval : false ,
28
+ tls : getTlsOptions ( this . config ) ,
29
+ name : 'wiki-js'
30
+ } )
31
+ break
22
32
case '7.x' :
23
33
const { Client : Client7 } = require ( 'elasticsearch7' )
24
34
this . client = new Client7 ( {
@@ -54,7 +64,8 @@ module.exports = {
54
64
async createIndex ( ) {
55
65
try {
56
66
const indexExists = await this . client . indices . exists ( { index : this . config . indexName } )
57
- if ( ! indexExists . body ) {
67
+ // Elasticsearch 6.x / 7.x
68
+ if ( this . config . apiVersion !== '8.x' && ! indexExists . body ) {
58
69
WIKI . logger . info ( `(SEARCH/ELASTICSEARCH) Creating index...` )
59
70
try {
60
71
const idxBody = {
@@ -68,6 +79,7 @@ module.exports = {
68
79
tags : { type : 'text' , boost : 8.0 }
69
80
}
70
81
}
82
+
71
83
await this . client . indices . create ( {
72
84
index : this . config . indexName ,
73
85
body : {
@@ -88,7 +100,42 @@ module.exports = {
88
100
} catch ( err ) {
89
101
WIKI . logger . error ( `(SEARCH/ELASTICSEARCH) Create Index Error: ` , _ . get ( err , 'meta.body.error' , err ) )
90
102
}
91
- }
103
+ // Elasticsearch 8.x
104
+ } else if ( this . config . apiVersion === '8.x' && ! indexExists ) {
105
+ WIKI . logger . info ( `(SEARCH/ELASTICSEARCH) Creating index...` )
106
+ try {
107
+ // 8.x Doesn't support boost in mappings, so we will need to boost at query time.
108
+ const idxBody = {
109
+ properties : {
110
+ suggest : { type : 'completion' } ,
111
+ title : { type : 'text' } ,
112
+ description : { type : 'text' } ,
113
+ content : { type : 'text' } ,
114
+ locale : { type : 'keyword' } ,
115
+ path : { type : 'text' } ,
116
+ tags : { type : 'text' }
117
+ }
118
+ }
119
+
120
+ await this . client . indices . create ( {
121
+ index : this . config . indexName ,
122
+ body : {
123
+ mappings : idxBody ,
124
+ settings : {
125
+ analysis : {
126
+ analyzer : {
127
+ default : {
128
+ type : this . config . analyzer
129
+ }
130
+ }
131
+ }
132
+ }
133
+ }
134
+ } )
135
+ } catch ( err ) {
136
+ WIKI . logger . error ( `(SEARCH/ELASTICSEARCH) Create Index Error: ` , _ . get ( err , 'meta.body.error' , err ) )
137
+ }
138
+ }
92
139
} catch ( err ) {
93
140
WIKI . logger . error ( `(SEARCH/ELASTICSEARCH) Index Check Error: ` , _ . get ( err , 'meta.body.error' , err ) )
94
141
}
@@ -129,15 +176,15 @@ module.exports = {
129
176
}
130
177
} )
131
178
return {
132
- results : _ . get ( results , 'body.hits.hits' , [ ] ) . map ( r => ( {
179
+ results : _ . get ( results , this . config . apiVersion === '8.x' ? 'hits.hits' : 'body.hits.hits' , [ ] ) . map ( r => ( {
133
180
id : r . _id ,
134
181
locale : r . _source . locale ,
135
182
path : r . _source . path ,
136
183
title : r . _source . title ,
137
184
description : r . _source . description
138
185
} ) ) ,
139
186
suggestions : _ . reject ( _ . get ( results , 'suggest.suggestions' , [ ] ) . map ( s => _ . get ( s , 'options[0].text' , false ) ) , s => ! s ) ,
140
- totalHits : _ . get ( results , ' body.hits.total.value', _ . get ( results , 'body.hits.total' , 0 ) )
187
+ totalHits : _ . get ( results , this . config . apiVersion === '8.x' ? 'hits.total.value' : ' body.hits.total.value', _ . get ( results , this . config . apiVersion === '8.x' ? 'hits.total' : 'body.hits.total' , 0 ) )
141
188
}
142
189
} catch ( err ) {
143
190
WIKI . logger . warn ( 'Search Engine Error: ' , _ . get ( err , 'meta.body.error' , err ) )
@@ -182,7 +229,7 @@ module.exports = {
182
229
async created ( page ) {
183
230
await this . client . index ( {
184
231
index : this . config . indexName ,
185
- type : '_doc' ,
232
+ ... ( this . config . apiVersion !== '8.x' && { type : '_doc' } ) ,
186
233
id : page . hash ,
187
234
body : {
188
235
suggest : this . buildSuggest ( page ) ,
@@ -204,7 +251,7 @@ module.exports = {
204
251
async updated ( page ) {
205
252
await this . client . index ( {
206
253
index : this . config . indexName ,
207
- type : '_doc' ,
254
+ ... ( this . config . apiVersion !== '8.x' && { type : '_doc' } ) ,
208
255
id : page . hash ,
209
256
body : {
210
257
suggest : this . buildSuggest ( page ) ,
@@ -226,7 +273,7 @@ module.exports = {
226
273
async deleted ( page ) {
227
274
await this . client . delete ( {
228
275
index : this . config . indexName ,
229
- type : '_doc' ,
276
+ ... ( this . config . apiVersion !== '8.x' && { type : '_doc' } ) ,
230
277
id : page . hash ,
231
278
refresh : true
232
279
} )
@@ -239,13 +286,13 @@ module.exports = {
239
286
async renamed ( page ) {
240
287
await this . client . delete ( {
241
288
index : this . config . indexName ,
242
- type : '_doc' ,
289
+ ... ( this . config . apiVersion !== '8.x' && { type : '_doc' } ) ,
243
290
id : page . hash ,
244
291
refresh : true
245
292
} )
246
293
await this . client . index ( {
247
294
index : this . config . indexName ,
248
- type : '_doc' ,
295
+ ... ( this . config . apiVersion !== '8.x' && { type : '_doc' } ) ,
249
296
id : page . destinationHash ,
250
297
body : {
251
298
suggest : this . buildSuggest ( page ) ,
@@ -314,8 +361,8 @@ module.exports = {
314
361
result . push ( {
315
362
index : {
316
363
_index : this . config . indexName ,
317
- _type : '_doc' ,
318
- _id : doc . id
364
+ _id : doc . id ,
365
+ ... ( this . config . apiVersion !== '8.x' && { _type : '_doc' } )
319
366
}
320
367
} )
321
368
doc . safeContent = WIKI . models . pages . cleanHTML ( doc . render )
0 commit comments