@@ -3,18 +3,26 @@ import {
3
3
SearchRequest ,
4
4
SearchSort ,
5
5
} from '@opensearch-project/opensearch/api/types' ;
6
- import { ContentSearchParams , ContentSearchSort } from '../../../../common/contentSearch' ;
6
+ import {
7
+ ContentSearchParams ,
8
+ ContentSearchSort ,
9
+ ContentSearchType ,
10
+ } from '../../../../common/contentSearch' ;
7
11
import { Request } from 'express' ;
8
12
import { parseNumberParam , parseToStringArray } from '../../utils/queryParams' ;
9
13
import { CmsArchiveCategoriesService } from '../../cms/CmsArchiveCategoriesService' ;
10
14
11
15
const SIZE_DEFAULT = 50 ;
12
16
13
17
const sortParamsSet : ReadonlySet < ContentSearchSort > = new Set ( [ 'name' , 'datetime' , 'score' ] ) ;
18
+ const typeParamsSet : ReadonlySet < ContentSearchType > = new Set ( [ 'all' , 'locations' , 'titles' ] ) ;
14
19
15
20
const isValidSort = ( sort : string ) : sort is ContentSearchSort =>
16
21
sortParamsSet . has ( sort as ContentSearchSort ) ;
17
22
23
+ const isValidType = ( type : string ) : type is ContentSearchType =>
24
+ typeParamsSet . has ( type as ContentSearchType ) ;
25
+
18
26
const sortParams : Record < ContentSearchSort , SearchSort > = {
19
27
score : {
20
28
_score : 'desc' ,
@@ -28,12 +36,12 @@ const sortParams: Record<ContentSearchSort, SearchSort> = {
28
36
} as const ;
29
37
30
38
export const transformQueryToContentSearchParams = ( req : Request ) : ContentSearchParams | null => {
31
- const { query, withContent , size, sort, from, categoryKeys, withChildCategories } =
39
+ const { query, type , size, sort, from, categoryKeys, withChildCategories } =
32
40
req . query as Record < keyof ContentSearchParams , string > ;
33
41
34
42
return {
35
43
query,
36
- withContent : withContent === 'true' ,
44
+ type : isValidType ( type ) ? type : undefined ,
37
45
categoryKeys : parseToStringArray ( categoryKeys ) ,
38
46
withChildCategories : withChildCategories === 'true' ,
39
47
sort : isValidSort ( sort ) ? sort : 'score' ,
@@ -53,7 +61,7 @@ export const buildContentSearchParams = (
53
61
from,
54
62
size,
55
63
sort = 'score' ,
56
- withContent ,
64
+ type ,
57
65
categoryKeys,
58
66
withChildCategories,
59
67
} = contentSearchParams ;
@@ -69,19 +77,33 @@ export const buildContentSearchParams = (
69
77
] ;
70
78
71
79
if ( query ) {
72
- const fields = [ 'displayName^10' ] ;
73
-
74
- if ( withContent ) {
75
- fields . push ( 'xmlAsString' ) ;
80
+ if ( type === 'locations' ) {
81
+ const { pathname } = new URL ( query , process . env . APP_ORIGIN ) ;
82
+
83
+ must . push ( {
84
+ prefix : {
85
+ 'locations.menuItemPath' : {
86
+ value : decodeURIComponent ( pathname ) ,
87
+ // @ts -expect-error (this is a valid field, not yet part of the library type def)
88
+ case_insensitive : true ,
89
+ } ,
90
+ } ,
91
+ } ) ;
92
+ } else {
93
+ const fields = [ 'displayName^5' ] ;
94
+
95
+ if ( type === 'all' ) {
96
+ fields . push ( 'xmlAsString' ) ;
97
+ }
98
+
99
+ must . push ( {
100
+ multi_match : {
101
+ query,
102
+ fields,
103
+ type : 'phrase_prefix' ,
104
+ } ,
105
+ } ) ;
76
106
}
77
-
78
- must . push ( {
79
- multi_match : {
80
- query,
81
- fields,
82
- type : 'phrase_prefix' ,
83
- } ,
84
- } ) ;
85
107
}
86
108
87
109
if ( categoryKeys ) {
0 commit comments