Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(@dpc-sdp/ripple-tide-search): allow address search function to be customised via JSON #1278

Merged
merged 1 commit into from
Jul 26, 2024
Merged
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
32 changes: 26 additions & 6 deletions packages/ripple-tide-search/utils/rplAddressSuggestionsFn.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { capitalCase } from 'change-case'

const getSuburbSuggestions = async (query) => {
const getSuburbSuggestions = async (query, args) => {
const suggestionsIndex = 'vicpol-postcode-localities'

const searchUrl = `/api/tide/app-search/${suggestionsIndex}/elasticsearch/_search`
Expand All @@ -17,6 +17,14 @@ const getSuburbSuggestions = async (query) => {
}
}
},
{
prefix: {
name: {
value: query,
case_insensitive: true
}
}
},
{
term: {
postcode: {
Expand All @@ -33,7 +41,7 @@ const getSuburbSuggestions = async (query) => {
method: 'POST',
body: {
...queryDSL,
size: 1
size: args.maxSuburbSuggestions
}
})

Expand All @@ -50,14 +58,14 @@ const getSuburbSuggestions = async (query) => {
})
}

const getAddressSuggestions = async (query) => {
const getAddressSuggestions = async (query, args) => {
const geocodeServerUrl =
'https://corp-geo.mapshare.vic.gov.au/arcgis/rest/services/Geocoder/VMAddressEZIAdd/GeocodeServer'

const suggestions = await $fetch(`${geocodeServerUrl}/suggest`, {
params: {
text: query,
maxSuggestions: 10,
maxSuggestions: args.maxAddressSuggestions,
f: 'json'
}
})
Expand All @@ -72,8 +80,20 @@ const getAddressSuggestions = async (query) => {
}

export default async (query, args) => {
const defaultArgs = {
maxSuburbSuggestions: 0,
maxAddressSuggestions: 10
}

const argsWithDefaults = {
...defaultArgs,
...(args || {})
}

return [
...(args?.includeSuburbs ? await getSuburbSuggestions(query, args) : []),
...(await getAddressSuggestions(query, args))
...(args.maxSuburbSuggestions > 0
? await getSuburbSuggestions(query, argsWithDefaults)
: []),
...(await getAddressSuggestions(query, argsWithDefaults))
]
}