Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fancy-rings-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gitbook": patch
---

Sort results by score to mix records and pages.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ import type {
import type { IconName } from '@gitbook/icons';
import { type NextRequest, NextResponse } from 'next/server';

type SearchResultGroup = {
score: number;
items: OrderedComputedResult[];
};

export async function POST(request: NextRequest) {
const [context, { organization, site, shareKey }] = await Promise.all([
getServerActionBaseContext(),
Expand Down Expand Up @@ -58,16 +63,22 @@ export async function POST(request: NextRequest) {
]);

const results = searchResults
.map((resultItem) => {
.flatMap((resultItem): SearchResultGroup[] => {
if (resultItem.type === 'record') {
const result: OrderedComputedResult = {
type: 'record',
id: resultItem.id,
title: resultItem.title,
description: resultItem.description,
href: resultItem.url,
score: resultItem.score,
};
return result;
return [
{
score: resultItem.score,
items: [result],
},
];
}

const found = findSiteSpaceBy(
Expand All @@ -77,19 +88,21 @@ export async function POST(request: NextRequest) {
const siteSection = found?.siteSection;
const siteSectionGroup = found?.siteSectionGroup;

return resultItem.pages.map((pageItem) =>
transformSitePageResult(context, {
return resultItem.pages.map((pageItem) => ({
score: pageItem.score,
items: transformSitePageResult(context, {
pageItem,
spaceItem: resultItem,
siteSpace: found?.siteSpace,
space: found?.siteSpace.space,
spaceURL: found?.siteSpace.urls.published,
siteSection: siteSection ?? undefined,
siteSectionGroup: (siteSectionGroup as SiteSectionGroup) ?? undefined,
})
);
}),
}));
})
.flat(2);
.sort((a, b) => b.score - a.score)
.flatMap((group) => group.items);

return NextResponse.json(results);
}
Expand Down Expand Up @@ -119,6 +132,7 @@ function transformSitePageResult(
: linker.toPathInSpace(pageItem.path),
pageId: pageItem.id,
spaceId: spaceItem.id,
score: pageItem.score,
breadcrumbs: [
siteSectionGroup && {
icon: siteSectionGroup?.icon as IconName,
Expand Down Expand Up @@ -157,6 +171,7 @@ function transformSitePageResult(
body: section.body,
pageId: pageItem.id,
spaceId: spaceItem.id,
score: section.score,
})) ?? [];

return [page, ...pageSections];
Expand Down
28 changes: 11 additions & 17 deletions packages/gitbook/src/components/Search/search-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,31 @@ export type OrderedComputedResult =
| ComputedSectionResult
| ComputedRecordResult;

export interface ComputedSectionResult {
type: 'section';
export type BaseComputedResult = {
id: string;
title: string;
body: string;
href: string;
score: number;
};

export type ComputedSectionResult = BaseComputedResult & {
type: 'section';
body: string;
pageId: string;
spaceId: string;
}
};

export interface ComputedPageResult {
export type ComputedPageResult = BaseComputedResult & {
type: 'page';
id: string;
title: string;

href: string;

pageId: string;
spaceId: string;

breadcrumbs?: Array<{ icon?: IconName; label: string }>;
}
};

export interface ComputedRecordResult {
export type ComputedRecordResult = BaseComputedResult & {
type: 'record';
id: string;
title: string;
description: string | undefined;
href: string;
}
};

export type SearchSiteContentScope =
| { mode: 'all' }
Expand Down
Loading