1
1
import { CmsCategoryListItem } from '../../../common/cms-documents/category' ;
2
- import {
3
- CategoryContentsResponse ,
4
- CmsContent ,
5
- CmsContentDocument ,
6
- CmsContentListItem ,
7
- } from '../../../common/cms-documents/content' ;
8
- import { CmsArchiveDbClient } from '../opensearch/CmsArchiveDbClient' ;
2
+ import { CmsContent , CmsContentDocument } from '../../../common/cms-documents/content' ;
3
+ import { CmsArchiveOpenSearchClient } from '../opensearch/CmsArchiveOpenSearchClient' ;
9
4
import { CmsBinaryDocument } from '../../../common/cms-documents/binary' ;
10
5
import { CmsArchiveSiteConfig } from './CmsArchiveSite' ;
11
6
import { sortVersions } from '../utils/sort' ;
12
7
import { AssetDocument , CmsCategoryDocument } from '../opensearch/types' ;
13
8
import { transformToCategoriesList } from './utils/transformToCategoriesList' ;
14
- import { QueryDslQueryContainer } from '@opensearch-project/opensearch/api/types ' ;
15
- import { CmsCategoryRef } from '../../../common/cms-documents/_common ' ;
16
- import { ContentSearchResult } from '../../../common/contentSearchResult ' ;
9
+ import { CmsCategoryPath } from '../../../common/cms-documents/_common ' ;
10
+ import { ContentSearchParams , ContentSearchResult } from '../../../common/contentSearch ' ;
11
+ import { buildContentSearchParams } from '../opensearch/queries/contentSearch ' ;
17
12
18
13
type ConstructorProps = {
19
- client : CmsArchiveDbClient ;
14
+ client : CmsArchiveOpenSearchClient ;
20
15
siteConfig : CmsArchiveSiteConfig ;
21
16
} ;
22
17
23
18
export class CmsArchiveService {
24
- private readonly client : CmsArchiveDbClient ;
19
+ private readonly client : CmsArchiveOpenSearchClient ;
25
20
private readonly siteConfig : CmsArchiveSiteConfig ;
26
21
27
22
private readonly categoriesIndex : string ;
@@ -82,114 +77,36 @@ export class CmsArchiveService {
82
77
. then ( ( res ) => transformToCategoriesList ( res , this ) ) ;
83
78
}
84
79
85
- public async getContentsForCategory (
86
- categoryKey : string ,
87
- from : number = 0 ,
88
- size : number = 50 ,
89
- query ?: string
90
- ) : Promise < CategoryContentsResponse | null > {
91
- const must : QueryDslQueryContainer [ ] = [
92
- {
93
- term : {
94
- isCurrentVersion : true ,
95
- } ,
96
- } ,
97
- {
98
- term : {
99
- 'category.key' : categoryKey ,
100
- } ,
101
- } ,
102
- ] ;
103
-
104
- if ( query ) {
105
- must . push ( {
106
- multi_match : {
107
- query,
108
- fields : [ 'displayName^10' , 'xmlAsString' ] ,
109
- type : 'phrase_prefix' ,
110
- } ,
111
- } ) ;
112
- }
113
-
114
- return this . client . search < CmsContentListItem > ( {
115
- index : this . contentsIndex ,
116
- from,
117
- size,
118
- _source_includes : [ 'contentKey' , 'versionKey' , 'displayName' ] ,
119
- body : {
120
- sort : {
121
- name : 'asc' ,
122
- } ,
123
- query : {
124
- bool : {
125
- must,
126
- } ,
127
- } ,
128
- track_total_hits : true ,
129
- } ,
130
- } ) ;
131
- }
132
-
133
- public async contentSearch (
134
- query : string ,
135
- from : number = 0 ,
136
- size : number = 50
137
- ) : Promise < ContentSearchResult > {
80
+ public async contentSearch ( params : ContentSearchParams ) : Promise < ContentSearchResult > {
138
81
const result = await this . client . search < CmsContentDocument > ( {
82
+ ...buildContentSearchParams ( params ) ,
139
83
index : this . contentsIndex ,
140
- from,
141
- size,
142
- _source_excludes : [ 'xmlAsString' , 'html' , 'versions' ] ,
143
- body : {
144
- sort : {
145
- _score : {
146
- order : 'desc' ,
147
- } ,
148
- } ,
149
- query : {
150
- bool : {
151
- must : [
152
- {
153
- term : {
154
- isCurrentVersion : {
155
- value : true ,
156
- } ,
157
- } ,
158
- } ,
159
- {
160
- multi_match : {
161
- query,
162
- fields : [ 'displayName^10' , 'xmlAsString' ] ,
163
- type : 'phrase_prefix' ,
164
- } ,
165
- } ,
166
- ] ,
167
- } ,
168
- } ,
169
- } ,
170
84
} ) ;
171
85
172
86
if ( ! result ) {
173
87
return {
174
- query ,
88
+ params ,
175
89
total : 0 ,
176
- error : 'Søket feilet, prøv igjen ' ,
90
+ status : 'error ' ,
177
91
hits : [ ] ,
178
92
} ;
179
93
}
180
94
95
+ const hits = await Promise . all (
96
+ result . hits . map ( async ( hit ) => ( {
97
+ contentKey : hit . contentKey ,
98
+ versionKey : hit . versionKey ,
99
+ displayName : hit . displayName ,
100
+ score : hit . _score || 0 ,
101
+ path : await this . resolveCategoryPath ( hit . category . key ) ,
102
+ } ) )
103
+ ) ;
104
+
181
105
return {
182
- query ,
106
+ params ,
183
107
total : result . total ,
184
- hits : await Promise . all (
185
- result . hits . map ( async ( hit ) => ( {
186
- contentKey : hit . contentKey ,
187
- versionKey : hit . versionKey ,
188
- displayName : hit . displayName ,
189
- score : hit . _score || 0 ,
190
- path : await this . resolveCategoriesPath ( hit . category . key ) ,
191
- } ) )
192
- ) ,
108
+ status : 'success' ,
109
+ hits,
193
110
} ;
194
111
}
195
112
@@ -273,6 +190,26 @@ export class CmsArchiveService {
273
190
return result . hits [ 0 ] ;
274
191
}
275
192
193
+ async resolveCategoryPath ( categoryKey : string ) : Promise < CmsCategoryPath > {
194
+ const category = await this . getCategory ( categoryKey ) ;
195
+ if ( ! category ) {
196
+ return [ ] ;
197
+ }
198
+
199
+ const { key, superKey, title } = category ;
200
+
201
+ const item = {
202
+ key,
203
+ name : title ,
204
+ } ;
205
+
206
+ if ( ! superKey ) {
207
+ return [ item ] ;
208
+ }
209
+
210
+ return [ ...( await this . resolveCategoryPath ( superKey ) ) , item ] ;
211
+ }
212
+
276
213
private async fixContent (
277
214
contentDocument : CmsContentDocument | null
278
215
) : Promise < CmsContent | null > {
@@ -284,7 +221,7 @@ export class CmsArchiveService {
284
221
...contentDocument ,
285
222
versions : sortVersions ( contentDocument ) ,
286
223
html : this . fixHtml ( contentDocument . html ) ,
287
- path : await this . resolveCategoriesPath ( contentDocument . category . key ) ,
224
+ path : await this . resolveCategoryPath ( contentDocument . category . key ) ,
288
225
} ;
289
226
}
290
227
@@ -297,24 +234,4 @@ export class CmsArchiveService {
297
234
. replace ( / ( \r \n | \r | \n ) / , '' )
298
235
. replace ( / = " \/ ( \d ) + \/ / g, `="${ this . siteConfig . basePath } /` ) ;
299
236
}
300
-
301
- async resolveCategoriesPath ( categoryKey : string ) : Promise < CmsCategoryRef [ ] > {
302
- const category = await this . getCategory ( categoryKey ) ;
303
- if ( ! category ) {
304
- return [ ] ;
305
- }
306
-
307
- const { key, superKey, title } = category ;
308
-
309
- const item = {
310
- key,
311
- name : title ,
312
- } ;
313
-
314
- if ( ! superKey ) {
315
- return [ item ] ;
316
- }
317
-
318
- return [ ...( await this . resolveCategoriesPath ( superKey ) ) , item ] ;
319
- }
320
237
}
0 commit comments