|
| 1 | +const { parseBrowseParams } = require('./elasticsearch/browse-utils') |
| 2 | + |
| 3 | +const ApiRequest = require('./api-request') |
| 4 | +const ElasticQueryBrowseBuilder = require('./elasticsearch/elastic-query-browse-builder') |
| 5 | + |
| 6 | +const BROWSE_INDEX = process.env.BROWSE_INDEX |
| 7 | + |
| 8 | +const parseNameAndRole = (delimitedString) => { |
| 9 | + if (!delimitedString.includes('||')) { |
| 10 | + return { name: delimitedString, role: null } |
| 11 | + } |
| 12 | + const [name, role] = delimitedString.split('||') |
| 13 | + return { name, role } |
| 14 | +} |
| 15 | + |
| 16 | +module.exports = function (app, _private = null) { |
| 17 | + app.contributors = {} |
| 18 | + |
| 19 | + app.contributors.browse = function (params, opts, request) { |
| 20 | + app.logger.debug('Unparsed params: ', params) |
| 21 | + params = parseBrowseParams(params) |
| 22 | + |
| 23 | + app.logger.debug('Parsed params: ', params) |
| 24 | + |
| 25 | + const body = buildElasticContributorsBody(params) |
| 26 | + |
| 27 | + app.logger.debug('Contrbutors#browse', BROWSE_INDEX, body) |
| 28 | + |
| 29 | + return app.esClient.search(body, process.env.BROWSE_INDEX) |
| 30 | + .then((resp) => { |
| 31 | + return { |
| 32 | + '@type': 'contributorList', |
| 33 | + page: params.page, |
| 34 | + per_page: params.per_page, |
| 35 | + totalResults: resp.hits?.total?.value, |
| 36 | + contributors: resp.hits?.hits?.reduce((workingResponse, hit) => { |
| 37 | + if (hit.matched_queries?.[0] === 'preferredTerm' || hit.matched_queries?.[0] === 'preferredTermPrefix') { // if match is on preferredTerm, use that regardless of variant matches |
| 38 | + const { name, role } = parseNameAndRole(hit._source.preferredTerm) |
| 39 | + |
| 40 | + let contributorData = workingResponse.find(item => item.termLabel === name) |
| 41 | + |
| 42 | + if (!contributorData) { |
| 43 | + contributorData = { |
| 44 | + '@type': 'preferredTerm', |
| 45 | + termLabel: name |
| 46 | + } |
| 47 | + workingResponse.push(contributorData) |
| 48 | + } |
| 49 | + |
| 50 | + if (role) { |
| 51 | + // just add the role count to the top level response |
| 52 | + const roleCount = { role, count: hit._source.count } |
| 53 | + if (!contributorData.roleCounts) { |
| 54 | + contributorData.roleCounts = [] |
| 55 | + } |
| 56 | + contributorData.roleCounts.push(roleCount) |
| 57 | + } else { |
| 58 | + // top-level contributor object |
| 59 | + contributorData.count = hit._source.count |
| 60 | + contributorData.broaderTerms = hit._source.broaderTerms?.map((term) => ({ termLabel: term })) |
| 61 | + contributorData.narrowerTerms = hit._source.narrowerTerms?.map((term) => ({ termLabel: term })) |
| 62 | + contributorData.seeAlso = hit._source.seeAlso?.map((term) => ({ termLabel: term })) |
| 63 | + contributorData.uri = hit._source.uri |
| 64 | + } |
| 65 | + } else { |
| 66 | + // Match was on a variant- use that in the response |
| 67 | + const matchedVariantTerm = hit.inner_hits.variants.hits.hits[0]._source.variant |
| 68 | + |
| 69 | + const variantData = { |
| 70 | + '@type': 'variant', |
| 71 | + termLabel: matchedVariantTerm, |
| 72 | + preferredTerms: [ |
| 73 | + { |
| 74 | + termLabel: hit._source.preferredTerm, |
| 75 | + count: hit._source.count |
| 76 | + } |
| 77 | + ] |
| 78 | + } |
| 79 | + |
| 80 | + workingResponse.push(variantData) |
| 81 | + } |
| 82 | + |
| 83 | + return workingResponse |
| 84 | + }, []) |
| 85 | + } |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 89 | + // For unit testing |
| 90 | + if (_private && typeof _private === 'object') { |
| 91 | + _private.buildElasticContributorsBody = buildElasticContributorsBody |
| 92 | + _private.parseBrowseParams = parseBrowseParams |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Given GET params, returns a plainobject with `from`, `size`, `query`, |
| 98 | + * `sort`, and any other params necessary to perform the ES query based |
| 99 | + * on the GET params. |
| 100 | + * |
| 101 | + * @return {object} An object that can be posted directly to ES |
| 102 | + */ |
| 103 | +const buildElasticContributorsBody = function (params) { |
| 104 | + const body = { |
| 105 | + from: (params.per_page * (params.page - 1)), |
| 106 | + size: params.per_page |
| 107 | + } |
| 108 | + |
| 109 | + const request = ApiRequest.fromParams(params) |
| 110 | + const builder = ElasticQueryBrowseBuilder.forApiRequest(request) |
| 111 | + |
| 112 | + body.query = builder.query.toJson() |
| 113 | + |
| 114 | + // match only termType 'contributor' |
| 115 | + body.query.bool.must.push({ term: { termType: { value: 'contributor' } } }) |
| 116 | + |
| 117 | + // Exclude items that have count == 0 |
| 118 | + body.query.bool.must.push({ range: { count: { gt: 0 } } }) |
| 119 | + |
| 120 | + return body |
| 121 | +} |
0 commit comments