Skip to content

Commit df6986d

Browse files
heiskrCopilot
andauthored
Remove no-explicit-any violations from search files (#61663)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 5111f82 commit df6986d

8 files changed

Lines changed: 74 additions & 50 deletions

File tree

eslint.config.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,6 @@ export default [
239239
'src/frame/components/context/MainContext.tsx',
240240
'src/landings/components/CookBookFilter.tsx',
241241
'src/languages/lib/correct-translation-content.ts',
242-
'src/search/components/hooks/useAISearchAutocomplete.ts',
243-
'src/search/components/hooks/useAISearchLocalStorageCache.ts',
244-
'src/search/components/input/SearchOverlay.tsx',
245-
'src/search/lib/get-elasticsearch-results/ai-search-autocomplete.ts',
246-
'src/search/lib/get-elasticsearch-results/general-search.ts',
247-
'src/search/scripts/index/index-cli.ts',
248-
'src/search/scripts/index/utils/indexing-elasticsearch-utils.ts',
249242
],
250243
rules: {
251244
'@typescript-eslint/no-explicit-any': 'off',

src/search/components/hooks/useAISearchAutocomplete.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,15 @@ export function useCombinedSearchResults({
125125
// Update state with fetched results
126126
setSearchOptions(results)
127127
setSearchLoading(false)
128-
} catch (error: any) {
129-
if (error.name === 'AbortError') {
128+
} catch (error: unknown) {
129+
// Aborted fetch() requests reject with a DOMException (not always an
130+
// Error instance), so match on the name rather than the prototype.
131+
if (
132+
typeof error === 'object' &&
133+
error !== null &&
134+
'name' in error &&
135+
(error as { name?: unknown }).name === 'AbortError'
136+
) {
130137
return
131138
}
132139
console.error(error)

src/search/components/hooks/useAISearchLocalStorageCache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface CacheIndexEntry {
1616
*
1717
* Cached items are cached under a prefix, for a fixed number of days
1818
*/
19-
export function useAISearchLocalStorageCache<T = any>(
19+
export function useAISearchLocalStorageCache<T = unknown>(
2020
cacheKeyPrefix: string = 'ai-query-cache',
2121
maxEntries: number = 1000,
2222
expirationDays: number = 30,

src/search/components/input/SearchOverlay.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export function SearchOverlay({
190190
generalWithView.push({
191191
title: t('search.overlay.view_all_search_results'),
192192
isViewAllResults: true,
193-
} as any)
193+
} as unknown as GeneralSearchHitWithOptions)
194194
} else if (autoCompleteSearchError) {
195195
if (urlSearchInputQuery.trim() !== '') {
196196
generalWithView.push({
@@ -203,7 +203,7 @@ export function SearchOverlay({
203203
generalWithView.push({
204204
title: t('search.overlay.no_results_found'),
205205
isNoResultsFound: true,
206-
} as any)
206+
} as unknown as GeneralSearchHitWithOptions)
207207
} else {
208208
generalWithView = []
209209
}
@@ -423,7 +423,7 @@ export function SearchOverlay({
423423
// If it's the "no results found" option, skip it
424424
if (
425425
newIndex >= selectedIndex &&
426-
(combinedOptions[newIndex]?.option as any)?.isNoResultsFound
426+
(combinedOptions[newIndex]?.option as GeneralSearchHitWithOptions)?.isNoResultsFound
427427
) {
428428
newIndex += 1
429429
}
@@ -454,7 +454,7 @@ export function SearchOverlay({
454454
// If it's the "no results found" option, skip it
455455
if (
456456
newIndex <= selectedIndex &&
457-
(combinedOptions[newIndex]?.option as any)?.isNoResultsFound
457+
(combinedOptions[newIndex]?.option as GeneralSearchHitWithOptions)?.isNoResultsFound
458458
) {
459459
newIndex -= 1
460460
}

src/search/lib/get-elasticsearch-results/ai-search-autocomplete.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export async function getAISearchAutocompleteResults({
1919
const t0 = new Date()
2020
const client = getElasticsearchClient() as Client
2121

22-
const searchQuery: any = {
22+
const searchQuery: estypes.SearchRequest = {
2323
index: indexName,
2424
size,
2525
// Send absolutely minimal from Elasticsearch to here. Less data => faster.

src/search/lib/get-elasticsearch-results/general-search.ts

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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

195199
function 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+
420433
function 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
})

src/search/scripts/index/index-cli.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,18 @@ const generalSearchCommand = new Command('general-search')
7373
.action(async (sourceDirectory, options) => {
7474
try {
7575
await indexGeneralSearch(sourceDirectory, options)
76-
} catch (error: any) {
77-
if (error instanceof errors.ElasticsearchClientError) {
78-
if ((error as any)?.meta) {
79-
console.error('Error meta: %O', (error as any).meta)
80-
}
76+
} catch (error: unknown) {
77+
if (
78+
error instanceof errors.ElasticsearchClientError &&
79+
'meta' in error &&
80+
(error as { meta?: unknown }).meta
81+
) {
82+
console.error('Error meta: %O', (error as { meta?: unknown }).meta)
8183
}
82-
console.error('general-search indexing error:', error.message)
84+
console.error(
85+
'general-search indexing error:',
86+
error instanceof Error ? error.message : error,
87+
)
8388
process.exit(1)
8489
}
8590
})
@@ -123,13 +128,18 @@ const aiSearchAutocompleteCommand = new Command('ai-search-autocomplete')
123128
versions,
124129
indexPrefix,
125130
})
126-
} catch (error: any) {
127-
if (error instanceof errors.ElasticsearchClientError) {
128-
if ((error as any)?.meta) {
129-
console.error('Error meta: %O', (error as any).meta)
130-
}
131+
} catch (error: unknown) {
132+
if (
133+
error instanceof errors.ElasticsearchClientError &&
134+
'meta' in error &&
135+
(error as { meta?: unknown }).meta
136+
) {
137+
console.error('Error meta: %O', (error as { meta?: unknown }).meta)
131138
}
132-
console.error('ai-search-autocomplete indexing error:', error.message)
139+
console.error(
140+
'ai-search-autocomplete indexing error:',
141+
error instanceof Error ? error.message : error,
142+
)
133143
process.exit(1)
134144
}
135145
})

src/search/scripts/index/utils/indexing-elasticsearch-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export async function populateIndex(
3636
client: Client,
3737
indexAlias: string,
3838
indexName: string,
39-
records: any[],
39+
records: Record<string, unknown>[],
4040
options: Options,
4141
) {
4242
console.log(chalk.yellow(`\nIndexing ${chalk.bold(indexName)}`))

0 commit comments

Comments
 (0)