Skip to content

Commit

Permalink
feat(search): disable fulltext pages search if we detect a country …
Browse files Browse the repository at this point in the history
…name in query
  • Loading branch information
marcelgerber committed Apr 11, 2024
1 parent 7ced993 commit cb9fc63
Showing 1 changed file with 54 additions and 31 deletions.
85 changes: 54 additions & 31 deletions site/search/SearchPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import ReactDOM from "react-dom"
import React, { useCallback, useEffect, useState } from "react"
import React, { useCallback, useEffect, useMemo, useState } from "react"
import cx from "classnames"
import {
keyBy,
getWindowQueryParams,
get,
mapValues,
isElementHidden,
sortBy,
} from "@ourworldindata/utils"
import {
InstantSearch,
Expand Down Expand Up @@ -36,6 +37,7 @@ import {
searchCategoryFilters,
IPageHit,
pageTypeDisplayNames,
PageRecord,
} from "./searchTypes.js"
import { EXPLORERS_ROUTE_FOLDER } from "../../explorer/ExplorerConstants.js"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome/index.js"
Expand All @@ -54,6 +56,7 @@ import {
DEFAULT_GRAPHER_WIDTH,
} from "@ourworldindata/grapher"
import { SiteAnalytics } from "../SiteAnalytics.js"
import { extractRegionNamesFromSearchQuery } from "./SearchUtils.js"

const siteAnalytics = new SiteAnalytics()

Expand Down Expand Up @@ -261,6 +264,13 @@ interface SearchResultsProps {
query: string
}

const PAGES_ATTRIBUTES_TO_SEARCH_NO_FULLTEXT: (keyof PageRecord)[] = [
"title",
"excerpt",
"tags",
"authors",
] // Should be a subset of the `searchableAttributes` set up in `configureAlgolia` for the `pages` index; minus the "content" attribute

const SearchResults = (props: SearchResultsProps) => {
const {
results: { queryID },
Expand Down Expand Up @@ -333,6 +343,12 @@ const SearchResults = (props: SearchResultsProps) => {
document.addEventListener("click", handleHitClick)
return () => document.removeEventListener("click", handleHitClick)
}, [queryID, handleHitClick])

const searchQueryRegionsMatches = useMemo(() => {
const extractedRegions = extractRegionNamesFromSearchQuery(props.query)
if (!extractedRegions) return undefined
return sortBy(extractedRegions, (r) => r.name) // For some deterministic order
}, [props.query])
if (isHidden) return null

const hasClickAnalyticsConsent = getPreferenceValue(
Expand All @@ -343,37 +359,44 @@ const SearchResults = (props: SearchResultsProps) => {
className="search-results"
data-active-filter={activeCategoryFilter}
>
{/* This is using the InstantSearch index specified in InstantSearchContainer */}
<Configure
hitsPerPage={40}
distinct
clickAnalytics={hasClickAnalyticsConsent}
/>
<NoResultsBoundary>
<section className="search-results__pages">
<header className="search-results__header">
<h2 className="h2-bold search-results__section-title">
Research & Writing
</h2>
<ShowMore
category={SearchIndexName.Pages}
cutoffNumber={4}
activeCategoryFilter={activeCategoryFilter}
handleCategoryFilterClick={
handleCategoryFilterClick
}
<Index indexName={getIndexName(SearchIndexName.Pages)}>
<Configure
hitsPerPage={40}
distinct
clickAnalytics={hasClickAnalyticsConsent}
// If we detect a country/region name in the query, we don't run a fulltext search
restrictSearchableAttributes={
searchQueryRegionsMatches
? PAGES_ATTRIBUTES_TO_SEARCH_NO_FULLTEXT
: undefined
}
/>
<NoResultsBoundary>
<section className="search-results__pages">
<header className="search-results__header">
<h2 className="h2-bold search-results__section-title">
Research & Writing
</h2>
<ShowMore
category={SearchIndexName.Pages}
cutoffNumber={4}
activeCategoryFilter={activeCategoryFilter}
handleCategoryFilterClick={
handleCategoryFilterClick
}
/>
</header>
<Hits
classNames={{
root: "search-results__list-container",
list: "search-results__pages-list grid grid-cols-2 grid-sm-cols-1",
item: "search-results__page-hit",
}}
hitComponent={PagesHit}
/>
</header>
<Hits
classNames={{
root: "search-results__list-container",
list: "search-results__pages-list grid grid-cols-2 grid-sm-cols-1",
item: "search-results__page-hit",
}}
hitComponent={PagesHit}
/>
</section>
</NoResultsBoundary>
</section>
</NoResultsBoundary>
</Index>
<Index indexName={getIndexName(SearchIndexName.Explorers)}>
<Configure
hitsPerPage={10}
Expand Down

0 comments on commit cb9fc63

Please sign in to comment.