Skip to content
Draft
Show file tree
Hide file tree
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
44 changes: 43 additions & 1 deletion src/backend/bigquery/bigquery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('[Function] bigQuery', () => {
name: 'name',
locale: 'locale',
taxon: 'taxon',
person: 'person',
organisation: 'organisation',
link: 'link',
}
Expand All @@ -59,6 +60,7 @@ describe('[Function] bigQuery', () => {
name: 'name',
locale: 'locale',
taxon: 'taxon',
person: 'person',
organisation: 'organisation',
link: 'link',
},
Expand All @@ -75,6 +77,9 @@ describe('[Function] sendInitQuery', () => {
.mockResolvedValueOnce([
[{ name: 'taxon1' }, { name: 'taxon2' }, { name: 'taxon3' }],
])
.mockResolvedValueOnce([
[{ name: 'person1' }, { name: 'person2' }, { name: 'person3' }],
])
.mockResolvedValueOnce([
[{ title: 'org1' }, { title: 'org2' }, { title: 'org3' }],
])
Expand All @@ -90,7 +95,7 @@ describe('[Function] sendInitQuery', () => {
])
const result = await sendInitQuery()

expect(BigQuery.prototype.query).toHaveBeenCalledTimes(5)
expect(BigQuery.prototype.query).toHaveBeenCalledTimes(6)

expect(BigQuery.prototype.query).toHaveBeenNthCalledWith(1, {
query: `
Expand All @@ -108,6 +113,14 @@ describe('[Function] sendInitQuery', () => {
location: 'europe-west2',
params: {},
})
expect(BigQuery.prototype.query).toHaveBeenNthCalledWith(2, {
query: `
SELECT DISTINCT title
FROM \`search.person\`
`,
location: 'europe-west2',
params: {},
})
expect(BigQuery.prototype.query).toHaveBeenNthCalledWith(3, {
query: `
SELECT DISTINCT title
Expand Down Expand Up @@ -136,6 +149,7 @@ describe('[Function] sendInitQuery', () => {
expect(result).toEqual({
locales: ['', 'en', 'cy', 'fr', 'de'],
taxons: ['taxon1', 'taxon2', 'taxon3'],
people: ['person1', 'person2', 'person3'],
organisations: ['org1', 'org2', 'org3'],
documentTypes: ['dt1', 'dt2', 'dt3'],
governments: ['gov1', 'gov2', 'gov3'],
Expand All @@ -150,6 +164,7 @@ describe('[Function] sendSearchQuery', () => {
selectedWords: 'keyword1 keyword2',
excludedWords: 'excluded1 excluded2',
taxon: 'taxon',
person: 'person',
publishingOrganisation: 'organisation',
language: 'en',
documentType: '',
Expand Down Expand Up @@ -184,6 +199,7 @@ describe('[Function] sendSearchQuery', () => {
locale: 'en',
organisation: 'organisation',
taxon: 'taxon',
person: 'person',
},
})
})
Expand All @@ -207,6 +223,31 @@ describe('[Function] sendSearchQuery', () => {
locale: 'en',
organisation: 'organisation',
taxon: 'taxon',
person: 'person',
},
})
})
it('Calls the appropriate queries with Person search type', async () => {
jest.spyOn(buildSqlQuery, 'buildSqlQuery').mockReturnValue('query')
;(BigQuery.prototype.query as jest.Mock)
.mockResolvedValueOnce(['Some result'])
.mockResolvedValueOnce(['Some result'])
await sendSearchQuery(makeSearchParams({ searchType: SearchType.Person }))
expect(BigQuery.prototype.query).toHaveBeenCalledTimes(1)
expect(BigQuery.prototype.query).toHaveBeenNthCalledWith(1, {
query: 'query',
location: 'europe-west2',
params: {
excluded_keyword0: 'excluded1',
excluded_keyword1: 'excluded2',
politicalStatus: 'any',
keyword0: 'keyword1',
keyword1: 'keyword2',
link: 'link',
locale: 'en',
organisation: 'organisation',
taxon: 'taxon',
person: 'person',
},
})
})
Expand All @@ -232,6 +273,7 @@ describe('[Function] sendSearchQuery', () => {
locale: 'en',
organisation: 'organisation',
taxon: 'taxon',
person: 'person',
},
})
})
Expand Down
33 changes: 25 additions & 8 deletions src/backend/bigquery/bigquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export const bigQuery = async function (userQuery: string, options?: any) {
if (options.taxon) {
params.taxon = options.taxon
}
if (options.person) {
params.person = options.person
}
if (options.organisation) {
params.organisation = options.organisation
}
Expand Down Expand Up @@ -79,33 +82,44 @@ export const bigQuery = async function (userQuery: string, options?: any) {
const sendInitQuery = async function (): Promise<InitResults> {
let bqLocales: any,
bqTaxons: any,
bqPeople: any,
bqOrganisations: any,
bqDocumentTypes: any,
bqGovernments: any
try {
;[bqLocales, bqTaxons, bqOrganisations, bqDocumentTypes, bqGovernments] =
await Promise.all([
bigQuery(`
;[
bqLocales,
bqTaxons,
bqPeople,
bqOrganisations,
bqDocumentTypes,
bqGovernments,
] = await Promise.all([
bigQuery(`
SELECT DISTINCT locale
FROM \`search.locale\`
`),
bigQuery(`
bigQuery(`
SELECT DISTINCT name
FROM \`search.taxon\`
`),
bigQuery(`
bigQuery(`
SELECT DISTINCT title
FROM \`search.person\`
`),
bigQuery(`
SELECT DISTINCT title
FROM \`search.organisation\`
`),
bigQuery(`
bigQuery(`
SELECT DISTINCT document_type
FROM \`search.document_type\`
`),
bigQuery(`
bigQuery(`
SELECT DISTINCT title
FROM \`search.government\`
`),
])
])
} catch (error) {
log.error(error, 'Error in sendInitQueryError')
}
Expand All @@ -116,6 +130,7 @@ const sendInitQuery = async function (): Promise<InitResults> {
.filter((locale: string) => locale !== 'en' && locale !== 'cy')
),
taxons: bqTaxons.map((taxon: any) => taxon.name),
people: bqPeople.map((person: any) => person.title),
organisations: bqOrganisations.map(
(organisation: any) => organisation.title
),
Expand All @@ -135,6 +150,7 @@ const sendSearchQuery = async function (
const query = buildSqlQuery(searchParams, keywords, excludedKeywords)
const locale = languageCode(searchParams.language)
const taxon = searchParams.taxon
const person = searchParams.person
const organisation = searchParams.publishingOrganisation
const documentType = searchParams.documentType
const link = searchParams.linkSearchUrl
Expand All @@ -147,6 +163,7 @@ const sendSearchQuery = async function (
excludedKeywords,
locale,
taxon,
person,
organisation,
link,
phoneNumber,
Expand Down
28 changes: 28 additions & 0 deletions src/backend/bigquery/buildSqlQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const prefix = (keywords?: string[], link?: string) => `
withdrawn_explanation,
page_views,
taxons,
people,
primary_organisation,
organisations AS all_organisations,
government,
Expand Down Expand Up @@ -76,6 +77,7 @@ const makeParams = (opts = {}) =>
selectedWords: '',
excludedWords: '',
taxon: '',
person: '',
publishingOrganisation: '',
language: '',
documentType: '',
Expand Down Expand Up @@ -230,6 +232,26 @@ describe('buildSqlQuery', () => {
expect(queryFmt(query)).toEqual(queryFmt(expected))
})

it('can filter by any person', () => {
const searchParams: SearchParams = makeParams({
person: 'whoever',
})
const keywords: string[] = []
const excludedKeywords: string[] = []

const query = buildSqlQuery(searchParams, keywords, excludedKeywords)
const expectedClauses = `
AND EXISTS
(
SELECT 1 FROM UNNEST (people) AS person
WHERE person = @person
)
`
const expected = expectedQuery(expectedClauses, keywords)

expect(queryFmt(query)).toEqual(queryFmt(expected))
})

it('can filter by any organisation', () => {
const searchParams: SearchParams = makeParams({
publishingOrganisation: 'whatever',
Expand Down Expand Up @@ -279,6 +301,7 @@ describe('buildSqlQuery', () => {
politicalStatus: 'political',
language: 'whatever',
taxon: 'whatever',
person: 'whoever',
publishingOrganisation: 'whatever',
linkSearchUrl: link,
government: '2015 Conservative government',
Expand All @@ -298,6 +321,11 @@ describe('buildSqlQuery', () => {
SELECT 1 FROM UNNEST (taxons) AS taxon
WHERE taxon = @taxon
)
AND EXISTS
(
SELECT 1 FROM UNNEST (people) AS person
WHERE person = @person
)
AND EXISTS
(
SELECT 1 FROM UNNEST (organisations) AS link
Expand Down
13 changes: 13 additions & 0 deletions src/backend/bigquery/buildSqlQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ export const buildSqlQuery = function (
`
}

let personClause = ''
if (searchParams.person !== '') {
personClause = `
AND EXISTS
(
SELECT 1 FROM UNNEST (people) AS person
WHERE person = @person
)
`
}

let organisationClause = ''
if (searchParams.publishingOrganisation !== '') {
organisationClause = `
Expand Down Expand Up @@ -206,6 +217,7 @@ export const buildSqlQuery = function (
withdrawn_explanation,
page_views,
taxons,
people,
primary_organisation,
organisations AS all_organisations,
government,
Expand All @@ -219,6 +231,7 @@ export const buildSqlQuery = function (
${publishingAppClause}
${localeClause}
${taxonClause}
${personClause}
${organisationClause}
${linkClause}
${phoneNumberClause}
Expand Down
1 change: 1 addition & 0 deletions src/backend/constants/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export enum Route {
getInitData = '/get-init-data',
searchApi = '/search',
searchTaxon = '/taxon',
searchPerson = '/person',
downloadCSV = '/csv',
login = '/login',
loginCallback = '/auth/gds/callback',
Expand Down
4 changes: 4 additions & 0 deletions src/backend/utils/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ const csvFieldFormatters: Record<string, any> = {
name: 'Topic tags',
format: formatNames,
},
people: {
name: 'People',
format: identity,
},
primary_organisation: {
name: 'Primary publishing organisation',
format: identity,
Expand Down
4 changes: 4 additions & 0 deletions src/backend/utils/getParams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('getParams', () => {
'selected-words': 'some selected-words',
'excluded-words': 'some excluded-words',
taxon: 'some taxon',
person: 'some person',
'publishing-organisation': 'some publishing-organisation',
language: 'some language',
'case-sensitive': 'true',
Expand All @@ -35,6 +36,7 @@ describe('getParams', () => {
selectedWords: 'some selected-words',
excludedWords: 'some excluded-words',
taxon: 'some taxon',
person: 'some person',
publishingOrganisation: 'some publishing-organisation',
caseSensitive: true,
documentType: 'some document-type',
Expand All @@ -56,6 +58,7 @@ describe('getParams', () => {
'selected-words': '',
'excluded-words': '',
taxon: '',
person: '',
'publishing-organisation': '',
language: '',
'case-sensitive': 'true',
Expand All @@ -82,6 +85,7 @@ describe('getParams', () => {
publishingOrganisation: '',
publishingStatus: '',
taxon: '',
person: '',
selectedWords: '',
keywordLocation: 'all',
documentType: '',
Expand Down
2 changes: 2 additions & 0 deletions src/backend/utils/getParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const getParams = (req: express.Request): SearchParams => {
const excludedWords =
sanitiseInput(req.query[UrlParams.ExcludedWords] as string) || ''
const taxon = sanitiseInput(req.query[UrlParams.Taxon] as string) || ''
const person = sanitiseInput(req.query[UrlParams.Person] as string) || ''
const publishingOrganisation =
sanitiseInput(req.query[UrlParams.PublishingOrganisation] as string) || ''
const language = sanitiseInput(req.query[UrlParams.Language] as string) || ''
Expand Down Expand Up @@ -60,6 +61,7 @@ export const getParams = (req: express.Request): SearchParams => {
selectedWords,
excludedWords,
taxon,
person,
publishingOrganisation,
language,
documentType,
Expand Down
4 changes: 4 additions & 0 deletions src/common/types/search-api-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export enum UrlParams {
SelectedWords = 'selected-words',
ExcludedWords = 'excluded-words',
Taxon = 'taxon',
Person = 'person',
PublishingOrganisation = 'publishing-organisation',
CaseSensitive = 'case-sensitive',
DocumentType = 'document-type',
Expand All @@ -23,6 +24,7 @@ export enum SearchType {
PhoneNumber = 'phone-number',
Organisation = 'organisation',
Taxon = 'taxon',
Person = 'person',
Language = 'language',
Advanced = 'advanced',
Results = 'results',
Expand Down Expand Up @@ -64,6 +66,7 @@ export type SearchParams = {
selectedWords: string // list of words to search
excludedWords: string // list of words to exclude
taxon: string // taxon to search in
person: string // person whose pages to search
publishingOrganisation: string // organisation to search in
language: string // the language to search for
documentType: string // documentTypeto search in
Expand All @@ -83,6 +86,7 @@ export type SearchResults = unknown[]

export type InitResults = {
taxons: string[]
people: string[]
locales: string[]
organisations: string[]
documentTypes: string[]
Expand Down
Loading