From e9818e182c290f299b5ead8640dbc0baecaccf78 Mon Sep 17 00:00:00 2001 From: Josh Heyer Date: Wed, 4 Oct 2023 20:21:10 +0000 Subject: [PATCH] Custom ranking based on version (latest, not-latest) for search results --- gatsby-config.js | 10 ++++++++++ src/components/search/index.js | 1 + src/constants/algolia-indexing.js | 10 ++++++++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/gatsby-config.js b/gatsby-config.js index 3a13673d006..c11683f59ce 100644 --- a/gatsby-config.js +++ b/gatsby-config.js @@ -380,6 +380,16 @@ module.exports = { indexName: algoliaIndex, }, ], + mergeSettings: true, + settings: { + advancedSyntax: true, + distinct: true, + attributeForDistinct: "pagePath", + attributesToSnippet: ["excerpt"], + attributesForFaceting: ["product", "type", "version", "isLatest"], + customRanking: ["desc(isLatest)", "asc(navDepth)"], + searchableAttributes: ["title", "excerpt", "path"], + }, chunkSize: 1000, enablePartialUpdates: false, skipIndexing: process.env.INDEX_ON_BUILD !== "true", diff --git a/src/components/search/index.js b/src/components/search/index.js index ba8ff823299..d96401f4aeb 100644 --- a/src/components/search/index.js +++ b/src/components/search/index.js @@ -231,6 +231,7 @@ const SearchBar = ({ searchProduct }) => { diff --git a/src/constants/algolia-indexing.js b/src/constants/algolia-indexing.js index 4c0a3af1cca..4141c0122f4 100644 --- a/src/constants/algolia-indexing.js +++ b/src/constants/algolia-indexing.js @@ -39,12 +39,14 @@ const mdxNodeToAlgoliaNode = (node, productVersions) => { // switch path to latest (if applicable) to avoid redirects const isLatest = productVersions[node.fields.product][0] === node.fields.version; + newNode["isLatest"] = isLatest; if (isLatest) { const latestPath = replacePathVersion(node.fields.path); newNode["path"] = latestPath; newNode["pagePath"] = latestPath; } } else { + newNode["isLatest"] = true; newNode["type"] = "guide"; } @@ -231,6 +233,7 @@ const algoliaTransformer = ({ data }) => { while (navStack.length > 0) { curr = navStack.pop(); let parentId = curr.mdxNode?.algoliaId; + let parentDepth = curr.mdxNode?.navDepth || 0; for (let child of curr.children) if (child.mdxNode) child.mdxNode.algoliaId = child.path @@ -251,12 +254,15 @@ const algoliaTransformer = ({ data }) => { ), ); // used to set fallback sort in algolia to navigation order - for (let i = 0; i < navigation.length; ++i) - if (navigation[i].mdxNode) + for (let i = 0; i < navigation.length; ++i) { + if (navigation[i].mdxNode) { navigation[i].mdxNode.algoliaId = (parentId || navigation[i].path.split("/").slice(0, -2).join("")) + (navigation.length - i).toString().padStart(3, "0") + navigation[i].mdxNode.algoliaId; + navigation[i].mdxNode.navDepth = parentDepth + 1; + } + } navStack.push(...navigation); if (!curr.mdxNode) continue;