Skip to content

Commit

Permalink
fix(core): deduplicate search results
Browse files Browse the repository at this point in the history
  • Loading branch information
juice49 committed Mar 1, 2024
1 parent 258c819 commit 53079e7
Showing 1 changed file with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {type CurrentUser} from '@sanity/types'

import {type SearchableType, type SearchHit, type SearchTerms} from '../../../../../../search'
import {getPublishedId} from '../../../../../../util'
import {type RecentSearch} from '../../datastores/recentSearches'
import {type SearchFieldDefinitionDictionary} from '../../definitions/fields'
import {type SearchFilterDefinitionDictionary} from '../../definitions/filters'
Expand Down Expand Up @@ -221,8 +222,9 @@ export function searchReducer(state: SearchReducerState, action: SearchAction):
...state.result,
error: null,
hasLocal: true,
// TODO: Deduplicate.
hits: state.result.hasLocal ? [...state.result.hits, ...action.hits] : action.hits,
hits: state.result.hasLocal
? deduplicate([...state.result.hits, ...action.hits])
: action.hits,
loaded: true,
loading: false,
},
Expand Down Expand Up @@ -556,3 +558,20 @@ function stripRecent(terms: RecentSearch | SearchTerms) {
}
return terms
}

/**
* At page boundaries, the Text Search API may sometimes produce duplicate results. This function
* deduplicates an array of results based on their ids.
*/
function deduplicate(hits: SearchHit[]): SearchHit[] {
const hitsById = hits.reduce((map, hit) => {
const id = getPublishedId(hit.hit._id)

return {
...map,
[id]: hit,
}
}, {})

return Object.values(hitsById)
}

0 comments on commit 53079e7

Please sign in to comment.