Skip to content

Commit 108c41c

Browse files
committed
refactor: enhance search filtering logic to include excluded doctypes and improve doctype detection
1 parent 9f3038b commit 108c41c

4 files changed

Lines changed: 23 additions & 8 deletions

File tree

dist/index.mjs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11028,7 +11028,9 @@ var StateResults = connectStateResults2(
1102811028
if (!searchResults)
1102911029
return;
1103011030
const results = searchResults;
11031-
const isFilteringByDoctype = typeof results?._state.filters === "string" && results._state.filters.includes("doctype:");
11031+
const stateFilters = typeof results?._state.filters === "string" ? results._state.filters : "";
11032+
const positiveFilters = stateFilters.replace(/NOT doctype:"[^"]*"/g, "");
11033+
const isFilteringByDoctype = positiveFilters.includes('doctype:"');
1103211034
const facets = results?.facets;
1103311035
const doctypeFacet = facets?.find((facet) => facet.name === "doctype");
1103411036
const nbHits = results?.nbHits ?? 0;
@@ -11152,10 +11154,12 @@ import { jsx as jsx58, jsxs as jsxs46 } from "react/jsx-runtime";
1115211154
var SearchResults = () => {
1115311155
const router = useRouter7();
1115411156
const { filterSelectedSection, ocurrenceCount } = useContext16(SearchContext);
11155-
const { locale } = useContext16(LibraryContext);
11157+
const { locale, sidebarSections } = useContext16(LibraryContext);
11158+
const excludedDoctypesFilter = sidebarSections.flat().filter((section) => section.excludeFromSearch).map((section) => `NOT doctype:"${section.id}"`).join(" AND ");
1115611159
const filters = [
1115711160
`language:${locale}`,
11158-
filterSelectedSection ? `doctype:"${filterSelectedSection}"` : ""
11161+
filterSelectedSection ? `doctype:"${filterSelectedSection}"` : "",
11162+
excludedDoctypesFilter
1115911163
].filter(Boolean).join(" AND ");
1116011164
const [prevFilter, setPrevFilter] = useState14("");
1116111165
const [searchState, setSearchState] = useState14({});

dist/index.mjs.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/search-results/index.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@ import { searchClient, searchIndex } from 'utils/config/search-config'
1616
const SearchResults = () => {
1717
const router = useRouter()
1818
const { filterSelectedSection, ocurrenceCount } = useContext(SearchContext)
19-
const { locale } = useContext(LibraryContext)
19+
const { locale, sidebarSections } = useContext(LibraryContext)
20+
const excludedDoctypesFilter = sidebarSections
21+
.flat()
22+
.filter((section) => section.excludeFromSearch)
23+
.map((section) => `NOT doctype:"${section.id}"`)
24+
.join(' AND ')
2025
const filters = [
2126
`language:${locale}`,
2227
filterSelectedSection ? `doctype:"${filterSelectedSection}"` : '',
28+
excludedDoctypesFilter,
2329
]
2430
.filter(Boolean)
2531
.join(' AND ')

src/components/search-results/infiniteHits.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,14 @@ const StateResults = connectStateResults(
6161

6262
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6363
const results = searchResults as any
64-
const isFilteringByDoctype =
65-
typeof results?._state.filters === 'string' &&
66-
results._state.filters.includes('doctype:')
64+
const stateFilters =
65+
typeof results?._state.filters === 'string'
66+
? results._state.filters
67+
: ''
68+
// Ignore the always-present `NOT doctype:"..."` exclusion clauses so we
69+
// only detect a positive doctype selection made by the user.
70+
const positiveFilters = stateFilters.replace(/NOT doctype:"[^"]*"/g, '')
71+
const isFilteringByDoctype = positiveFilters.includes('doctype:"')
6772

6873
const facets = results?.facets as
6974
| Array<{

0 commit comments

Comments
 (0)