Skip to content

Commit

Permalink
Merge pull request #1429 from dpc-sdp/feature/search-listing-source-c…
Browse files Browse the repository at this point in the history
…ustomisation

[SD-676] allow elastic response source to be customised
  • Loading branch information
lambry authored Jan 24, 2025
2 parents dec415c + 5d523f0 commit 4857cda
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,31 @@ Feature: Result items
| Accessibility guidelines | tide-search-result-card |
| Small business grant | tide-grant-search-result |
| GovHack 2022 is coming | tide-search-result |

@mockserver
Example: Result contents from elasticsearch can be customised to exclude fields
Given I load the page fixture with "/search-listing/result-items/page"
And the search listing config has the following excludes added to source
| key |
| nid |
| field_node_site |
Then the page endpoint for path "/search-results" returns the loaded fixture
And the search network request is stubbed with fixture "/search-listing/result-items/response" and status 200

When I visit the page "/search-results"
Then the search listing page should have 4 results
And the search network request should be called with the "/search-listing/result-items/request-source-exclude" fixture

@mockserver
Example: Result contents from elasticsearch can be customised to include fields
Given I load the page fixture with "/search-listing/result-items/page"
And the search listing config has the following includes added to source
| key |
| title |
| field_summary |
Then the page endpoint for path "/search-results" returns the loaded fixture
And the search network request is stubbed with fixture "/search-listing/result-items/response" and status 200

When I visit the page "/search-results"
Then the search listing page should have 4 results
And the search network request should be called with the "/search-listing/result-items/request-source-include" fixture
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"filter": [
{
"terms": {
"type": [
"news",
"grant",
"event",
"landing_page"
]
}
},
{
"terms": {
"field_node_site": [
8888
]
}
}
]
}
},
"size": 20,
"from": 0,
"sort": [
{
"title.keyword": "asc"
}
],
"_source": {
"exclude": ["nid", "field_node_site"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"filter": [
{
"terms": {
"type": [
"news",
"grant",
"event",
"landing_page"
]
}
},
{
"terms": {
"field_node_site": [
8888
]
}
}
]
}
},
"size": 20,
"from": 0,
"sort": [
{
"title.keyword": "asc"
}
],
"_source": {
"include": [
"title",
"field_summary"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -592,3 +592,35 @@ Then(
})
}
)

Then(
'the search listing config has the following excludes added to source',
(dataTable: DataTable) => {
const table = dataTable.hashes()
const excludes = table.map((row) => row.key)

cy.get('@pageFixture').then((response) => {
set(
response,
`config.searchListingConfig.responseSource.exclude`,
excludes
)
})
}
)

Then(
'the search listing config has the following includes added to source',
(dataTable: DataTable) => {
const table = dataTable.hashes()
const includes = table.map((row) => row.key)

cy.get('@pageFixture').then((response) => {
set(
response,
`config.searchListingConfig.responseSource.include`,
includes
)
})
}
)
31 changes: 29 additions & 2 deletions packages/ripple-tide-search/composables/useTideSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,19 @@ export default ({
]
}

const getSourceOptions = () => {
let source: { include?: string[]; exclude?: string[] } = {}

if (searchListingConfig?.responseSource?.include) {
source.include = searchListingConfig?.responseSource?.include
}
if (searchListingConfig?.responseSource?.exclude) {
source.exclude = searchListingConfig?.responseSource?.exclude
}

return Object.keys(source).length ? source : false
}

const getUserFilters = (forAggregations = false) => {
const filterValues: Record<string, any> = {
...filterForm.value,
Expand Down Expand Up @@ -510,13 +523,20 @@ export default ({
const getQueryDSL = async () => {
const locationFilters = await getLocationFilterClause('listing')
const query = getQueryClause([...getUserFilterClause(), ...locationFilters])
const source = getSourceOptions()

return {
let queryDSL = {
query,
size: pageSize.value,
from: pagingStart.value,
sort: getSortClause()
}

if (source) {
queryDSL._source = source
}

return queryDSL
}

const getQueryDSLForDynamicAggregations = async () => {
Expand Down Expand Up @@ -553,14 +573,21 @@ export default ({
const getQueryDSLForMaps = async () => {
const locationFilters = await getLocationFilterClause('map')
const query = getQueryClause([...getUserFilterClause(), ...locationFilters])
const source = getSourceOptions()

return {
let queryDSL = {
query,
// ES queries have a 10k result limit, maps struggle drawing more than this anyway. If you need more you will need to implement a loading strategy see : https://openlayers.org/en/latest/apidoc/module-ol_loadingstrategy.html
size: 10000,
from: 0,
sort: getSortClause()
}

if (source) {
queryDSL._source = source
}

return queryDSL
}

const getSearchResults = async (isFirstRun: boolean) => {
Expand Down
7 changes: 7 additions & 0 deletions packages/ripple-tide-search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,13 @@ export type TideSearchListingConfig = {
* @description show filters in the sidebar?
*/
filtersInSidebar?: boolean
/**
* @description modify the elasticsearch response source
*/
responseSource?: {
include?: string[]
exclude?: string[]
}
}
/**
* @description Tabs to display, key needs to be one of TideSearchListingTabKey
Expand Down

0 comments on commit 4857cda

Please sign in to comment.