Skip to content

Commit

Permalink
✨ further explorer view ranking enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
ikesau committed Nov 8, 2024
1 parent 294596b commit bc9e10d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion baker/algolia/configureAlgolia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,10 @@ export const configureAlgolia = async () => {
],
ranking: ["typo", "words", "exact", "attribute", "custom", "proximity"],
customRanking: [
"desc(score)",
// For multiple explorer views with the same title, we want to avoid surfacing duplicates.
// So, rank a result with viewTitleIndexWithinExplorer=0 way more highly than one with 1, 2, etc.
"asc(viewTitleIndexWithinExplorer)",
"desc(score)",
"asc(titleLength)",
],
attributesToSnippet: ["subtitle:24"],
Expand Down
12 changes: 8 additions & 4 deletions baker/algolia/indexExplorerViewsAndChartsToAlgolia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ const indexExplorerViewsAndChartsToAlgolia = async () => {
}
}, db.TransactionCloseMode.Close)

// Scale grapher scores between 0 and 10000, and explorer scores between 0 and 500
// (Except for the first view of each explorer, which we set to 10000)
// This is because Graphers are generally higher quality than Explorers
const scaledGrapherViews = scaleRecordScores(grapherViews)
// Scale grapher records and the default explorer views between 1000 and 10000,
// Scale the remaining explorer views between 0 and 1000.
// This is because Graphers are generally higher quality than Explorers and we don't want
// the data catalog to smother Grapher results with hundreds of low-quality Explorer results.
const scaledGrapherViews = scaleRecordScores(
grapherViews,
[1000, 10000]
)
const scaledExplorerViews = adaptExplorerViews(explorerViews)

const records = [...scaledGrapherViews, ...scaledExplorerViews]
Expand Down
6 changes: 3 additions & 3 deletions baker/algolia/utils/explorerViews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function explorerViewRecordToChartRecord(
/**
* Scale explorer record scores then convert them to ChartRecords.
* Each explorer has a default view (whichever is defined first in the decision matrix)
* We scale these default view scores between 0 and MAX_SCORE, but the rest we scale between 0 and 500
* We scale these default view scores between 0 and 10000, but the rest we scale between 0 and 1000
* to bury them under the (higher quality) grapher views in the data catalog.
*/
export function adaptExplorerViews(
Expand All @@ -90,8 +90,8 @@ export function adaptExplorerViews(
(view) => view.isFirstExplorerView
)
return [
...scaleRecordScores(firstViews),
...scaleRecordScores(rest, 500),
...scaleRecordScores(firstViews, [1000, 10000]),
...scaleRecordScores(rest, [0, 1000]),
].map(explorerViewRecordToChartRecord)
}

Expand Down
9 changes: 4 additions & 5 deletions baker/algolia/utils/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,23 @@ export const processAvailableEntities = (
)
}

export const MAX_SCORE = 10000

/**
* Scale records' positive scores to be between 0 and MAX_SCORE.
* Scale records' positive scores to be between two numbers.
*/
export function scaleRecordScores<T extends { score: number }>(
records: T[],
max = MAX_SCORE
range: [number, number]
): T[] {
const scores = records.map((r) => r.score)
const [min, max] = range
const maxScore = Math.max(...scores)
return records.map((record): T => {
// For ExplorerView records, we want to keep negative scores,
// because they're intentionally downranked as near-duplicates of existing views
if (record.score < 0) return record
// A value between 0 and 1
const normalized = record.score / maxScore
const scaled = Math.round(normalized * max)
const scaled = Math.round(normalized * (max - min) + min)
return {
...record,
score: scaled,
Expand Down

0 comments on commit bc9e10d

Please sign in to comment.