Skip to content

Commit bdbcc20

Browse files
authored
Add blog search results to docs site (#14609)
Adds blog results to docs site and but prioritizes other site sections in the result set when not under /blog. The behavior under blog section of our site remains the same.
1 parent 0fc413e commit bdbcc20

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

assets/js/bundle.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

layouts/partials/docs/search.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
id="search"
88
data-app-id="{{ getenv "ALGOLIA_APP_ID" }}"
99
data-search-key="{{ getenv "ALGOLIA_APP_SEARCH_KEY" }}"
10-
data-facets="Docs,Registry,Tutorials"
10+
data-facets="Docs,Registry,Tutorials,Blog"
1111
data-index="production"
1212
></div>
1313
</div>

theme/src/ts/algolia/autocomplete.ts

+33
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import algoliasearch, { SearchClient } from "algoliasearch/lite";
22
import { autocomplete, getAlgoliaResults, getAlgoliaFacets } from "@algolia/autocomplete-js";
33
import { getTags, getTagsPlugin, setTags, Tag, iconForTag, labelForTag, mapTagsToFilters, groupBy, formatCount, debounce, listenForEvents } from "./utils";
44

5+
interface SearchResultHit {
6+
objectID: string;
7+
section: string;
8+
href: string;
9+
ancestors?: string[];
10+
h1?: string;
11+
title: string;
12+
}
13+
514
// CSS selector of the element to convert into an autocomplete control.
615
const autocompleteContainer = "#search";
716

@@ -109,6 +118,30 @@ function initAutocomplete(el: HTMLElement) {
109118
},
110119
},
111120
],
121+
// transformResponse is called for each set of results, allowing us to operate
122+
// on the result set before it is returned.
123+
transformResponse(results) {
124+
// If on the blog page, return the results as is.
125+
if (window.location.pathname.startsWith("/blog")) {
126+
return results.hits;
127+
}
128+
// Enables us to deprioritize blog results. We down rank them since we do not
129+
// want them competing with docs results in this portion of the site, but should
130+
// be shown in results as it is helpful when there are no results returned from
131+
// the other sections of the site.
132+
return results.hits.map(set => {
133+
// Sort results set and push blog results to the bottom.
134+
return set.sort((a: SearchResultHit, b: SearchResultHit) => {
135+
if (a.section === "Blog" && b.section !== "Blog") {
136+
return 1;
137+
}
138+
if (a.section !== "Blog" && b.section === "Blog") {
139+
return -1;
140+
}
141+
return 0;
142+
});
143+
});
144+
},
112145
})
113146
},
114147

0 commit comments

Comments
 (0)