Skip to content

Commit

Permalink
PI-1449 Fix search suggestions for multi-term queries
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-bcl committed Sep 21, 2023
1 parent 4f6decc commit 5a74891
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
Did you mean
{%- set comma = joiner() %}
{%- for suggestion in params.results.suggestions %}{{ comma() }}
<a href="?q={{ suggestion }}" class="govuk-link govuk-link--no-visited-state"
title="Search again using {{ suggestion }}">{{ suggestion }}</a>
<a href="{{ suggestion.url }}" class="govuk-link govuk-link--no-visited-state"
title="Search again using {{ suggestion.text }}">{{ suggestion.text }}</a>
{%- endfor %}?
{% endif %}
</p>
Expand All @@ -46,7 +46,8 @@
document.getElementById("{{ params.id }}").addEventListener('input', function() {
clearTimeout(timeoutId)
const url = new URL(location.href)
url.searchParams.forEach((value, name) => url.searchParams.delete(name))
url.searchParams.delete("matchAllTerms")
url.searchParams.delete("providers[]")
url.searchParams.set('q', this.value)
timeoutId = setTimeout(() => fetch(url).then(async response => {
if (response.status === 200) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ export interface ProbationSearchResult {

export interface Suggestion {
text: string
offset: number
length: number
options: {
text: string
freq: number
Expand Down
2 changes: 2 additions & 0 deletions packages/probation-search-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
"dependencies": {
"date-fns": "^2.30.0",
"express": "^4.18.2",
"parseurl": "^1.3.3",
"superagent": "^8.1.2"
},
"devDependencies": {
"@types/express": "^4.17.17",
"@types/parseurl": "^1.3.1",
"@types/superagent": "^4.1.18",
"@typescript-eslint/eslint-plugin": "^6.7.0",
"@typescript-eslint/parser": "^6.7.0",
Expand Down
14 changes: 11 additions & 3 deletions packages/probation-search-frontend/routes/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ProbationSearchClient, {
ProbationSearchResult,
} from '../data/probationSearchClient'
import OAuthClient from '../data/oauthClient'
import parseurl from 'parseurl'

export interface ProbationSearchRouteOptions {
environment: 'local' | 'dev' | 'preprod' | 'prod'
Expand Down Expand Up @@ -113,10 +114,17 @@ export default function probationSearchRoutes({
results,
response,
suggestions: Object.values(response?.suggestions?.suggest || {})
.flatMap(suggestions => suggestions.flatMap(suggestion => suggestion.options))
.flatMap(suggestions =>
suggestions.flatMap(s =>
s.options.map(opts => {
const params = new URLSearchParams(parseurl(req).search)
params.set('q', query.slice(0, s.offset) + opts.text + query.slice(s.offset + s.length))
return { url: `${path}?${params.toString()}`, ...opts }
}),
),
)
.sort((a, b) => b.score - a.score || b.freq - a.freq)
.slice(0, 3)
.map(suggestion => suggestion.text),
.slice(0, 3),
page: calculatePagination(
currentPage,
response.totalPages,
Expand Down

0 comments on commit 5a74891

Please sign in to comment.