@@ -67,22 +67,24 @@ export async function getGeneralSearchResults(
6767 } ,
6868 } )
6969
70- const matchQuery : Record < string , any > = {
71- bool : {
72- should : matchQueries ,
73- // This allows filtering by toplevel later.
74- minimum_should_match : 1 ,
75- } ,
70+ const matchBool : estypes . QueryDslBoolQuery = {
71+ should : matchQueries ,
72+ // This allows filtering by toplevel later.
73+ minimum_should_match : 1 ,
74+ }
75+ const matchQuery : estypes . QueryDslQueryContainer = {
76+ bool : matchBool ,
7677 }
7778
7879 const toplevelArray = toplevel || [ ]
7980 if ( toplevelArray . length ) {
80- matchQuery . bool . filter = matchQuery . bool . filter || [ ]
81- matchQuery . bool . filter . push ( {
81+ const filters = Array . isArray ( matchBool . filter ) ? matchBool . filter : [ ]
82+ filters . push ( {
8283 terms : {
8384 toplevel : toplevelArray ,
8485 } ,
8586 } )
87+ matchBool . filter = filters
8688 }
8789
8890 const highlightFields = Array . from ( highlights || DEFAULT_HIGHLIGHT_FIELDS )
@@ -152,7 +154,7 @@ export async function getGeneralSearchResults(
152154 throw new Error ( `Unrecognized sort enum '${ sort } '` )
153155 }
154156
155- const result = await client . search ( searchQuery )
157+ const result = await client . search < GeneralSearchSource > ( searchQuery )
156158
157159 const hitsAll = result . hits
158160 const hits = getHits ( hitsAll . hits , {
@@ -177,10 +179,12 @@ export async function getGeneralSearchResults(
177179 return { meta, hits, aggregations : aggregationsResult }
178180}
179181
180- function getAggregations ( aggregate ?: string [ ] ) : Record < string , any > | undefined {
182+ function getAggregations (
183+ aggregate ?: string [ ] ,
184+ ) : Record < string , estypes . AggregationsAggregationContainer > | undefined {
181185 if ( ! aggregate || ! aggregate . length ) return undefined
182186
183- const aggs : Record < string , any > = { }
187+ const aggs : Record < string , estypes . AggregationsAggregationContainer > = { }
184188 for ( const key of aggregate ) {
185189 aggs [ key ] = {
186190 terms : {
@@ -194,18 +198,19 @@ function getAggregations(aggregate?: string[]): Record<string, any> | undefined
194198
195199function getAggregationsResult (
196200 aggregate ?: string [ ] ,
197- result ?: Record < string , any > ,
201+ result ?: Record < string , estypes . AggregationsAggregate > ,
198202) : Record < string , SearchAggregation [ ] > | undefined {
199203 if ( ! aggregate || ! aggregate . length || ! result ) return undefined
200204 const aggregations : Record < string , SearchAggregation [ ] > = { }
201205 for ( const key of aggregate ) {
202- if ( result [ key ] ?. buckets ) {
203- aggregations [ key ] = result [ key ] . buckets
204- . map ( ( bucket : any ) => ( {
206+ const agg = result [ key ] as { buckets ?: Array < { key : string ; doc_count : number } > } | undefined
207+ if ( agg ?. buckets ) {
208+ aggregations [ key ] = agg . buckets
209+ . map ( ( bucket ) => ( {
205210 key : bucket . key as string ,
206211 count : bucket . doc_count as number ,
207212 } ) )
208- . sort ( ( a : { key : string } , b : { key : string } ) => a . key . localeCompare ( b . key ) )
213+ . sort ( ( a , b ) => a . key . localeCompare ( b . key ) )
209214 }
210215 }
211216 return aggregations
@@ -417,8 +422,16 @@ interface GetHitsOptions {
417422 include : AdditionalIncludes [ ]
418423}
419424
425+ interface GeneralSearchSource {
426+ url : string
427+ title : string
428+ breadcrumbs : string
429+ popularity ?: number
430+ [ key : string ] : unknown
431+ }
432+
420433function getHits (
421- hits : estypes . SearchHit < any > [ ] ,
434+ hits : estypes . SearchHit < GeneralSearchSource > [ ] ,
422435 { indexName, debug = false , highlightFields, include } : GetHitsOptions ,
423436) : GeneralSearchHit [ ] {
424437 return hits . map ( ( hit ) => {
@@ -435,22 +448,23 @@ function getHits(
435448 hitHighlights [ key ] = ( hit . highlight && hit . highlight [ key ] ) || [ ]
436449 }
437450
451+ const source = hit . _source !
438452 const result : GeneralSearchHit = {
439453 id : hit . _id ! ,
440- url : hit . _source . url ,
441- title : hit . _source . title ,
442- breadcrumbs : hit . _source . breadcrumbs ,
454+ url : source . url ,
455+ title : source . title ,
456+ breadcrumbs : source . breadcrumbs ,
443457 highlights : hitHighlights ,
444458 }
445459 if ( debug ) {
446460 result . score = hit . _score ?? 0.0
447- result . popularity = hit . _source . popularity ?? 0.0
461+ result . popularity = source . popularity ?? 0.0
448462 if ( isDevMode ) {
449463 result . es_url = `http://localhost:9200/${ indexName } /_doc/${ hit . _id } `
450464 }
451465 }
452466 for ( const field of include ) {
453- result [ field ] = hit . _source [ field ]
467+ result [ field ] = source [ field ] as string
454468 }
455469 return result
456470 } )
0 commit comments