Skip to content

Commit

Permalink
Custom ranking based on version (latest, not-latest) for search results
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-heyer committed Oct 4, 2023
1 parent 162eacb commit e9818e1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
10 changes: 10 additions & 0 deletions gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/components/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ const SearchBar = ({ searchProduct }) => {
<InstantSearch
searchClient={searchClient}
indexName={algoliaIndex}
insights={true}
className="dropdown"
>
<Configure {...searchConfig} />
Expand Down
10 changes: 8 additions & 2 deletions src/constants/algolia-indexing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Expand Down Expand Up @@ -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
Expand All @@ -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;

Expand Down

0 comments on commit e9818e1

Please sign in to comment.