Skip to content

Commit

Permalink
Merge pull request #464 from NYPL/SCC-4563/add-format-filter
Browse files Browse the repository at this point in the history
Scc 4563/add format filter
  • Loading branch information
charmingduchess authored Mar 3, 2025
2 parents 79d435b + 18280b4 commit dab3106
Show file tree
Hide file tree
Showing 27 changed files with 15,949 additions and 651 deletions.
2 changes: 1 addition & 1 deletion config/production.env
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ NYPL_OAUTH_URL=https://isso.nypl.org/
ENCRYPTED_NYPL_OAUTH_ID=AQECAHh7ea2tyZ6phZgT4B9BDKwguhlFtRC6hgt+7HbmeFsrsgAAAGswaQYJKoZIhvcNAQcGoFwwWgIBADBVBgkqhkiG9w0BBwEwHgYJYIZIAWUDBAEuMBEEDMLKVUQA58B6vprNcAIBEIAoaz0lI9EL2M9NyTuEwT8JDmPBt6aXfMiFs027DEuwsCN0wS0qWeFL1g==
ENCRYPTED_NYPL_OAUTH_SECRET=AQECAHh7ea2tyZ6phZgT4B9BDKwguhlFtRC6hgt+7HbmeFsrsgAAAIcwgYQGCSqGSIb3DQEHBqB3MHUCAQAwcAYJKoZIhvcNAQcBMB4GCWCGSAFlAwQBLjARBAyWz91LOP2YP5fg0q0CARCAQ9inO9SV1M8R0Pkkx84r7UdwlU1FxfXvIjk/z6Qs81KBAVELhby2iD5LawQyDrR9tjhuMbotS6QnydwwMR/p8+qJXHI=

NYPL_CORE_VERSION=v2.27
NYPL_CORE_VERSION=v2.28

LOG_LEVEL=info
FEATURES=on-site-edd
Expand Down
2 changes: 1 addition & 1 deletion config/qa.env
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ NYPL_OAUTH_URL=https://isso.nypl.org/
ENCRYPTED_NYPL_OAUTH_ID=AQECAHh7ea2tyZ6phZgT4B9BDKwguhlFtRC6hgt+7HbmeFsrsgAAAGswaQYJKoZIhvcNAQcGoFwwWgIBADBVBgkqhkiG9w0BBwEwHgYJYIZIAWUDBAEuMBEEDMLKVUQA58B6vprNcAIBEIAoaz0lI9EL2M9NyTuEwT8JDmPBt6aXfMiFs027DEuwsCN0wS0qWeFL1g==
ENCRYPTED_NYPL_OAUTH_SECRET=AQECAHh7ea2tyZ6phZgT4B9BDKwguhlFtRC6hgt+7HbmeFsrsgAAAIcwgYQGCSqGSIb3DQEHBqB3MHUCAQAwcAYJKoZIhvcNAQcBMB4GCWCGSAFlAwQBLjARBAyWz91LOP2YP5fg0q0CARCAQ9inO9SV1M8R0Pkkx84r7UdwlU1FxfXvIjk/z6Qs81KBAVELhby2iD5LawQyDrR9tjhuMbotS6QnydwwMR/p8+qJXHI=

NYPL_CORE_VERSION=v2.27
NYPL_CORE_VERSION=v2.28

LOG_LEVEL=debug
FEATURES=on-site-edd
Expand Down
1 change: 1 addition & 0 deletions lib/elasticsearch/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const SEARCH_SCOPES = {
}

const FILTER_CONFIG = {
format: { operator: 'match', field: ['formatId'], repeatable: true },
recordType: { operator: 'match', field: ['recordTypeId'], repeatable: true },
owner: { operator: 'match', field: ['items.owner.id', 'items.owner.label'], repeatable: true, path: 'items' },
subjectLiteral: {
Expand Down
25 changes: 14 additions & 11 deletions lib/jsonld_serializers.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,9 @@ class ResourceSerializer extends JsonLdItemSerializer {
stmts.hasItemDates = this.body.numItemDatesParsed[0] / numItems >= parseFloat(process.env.BIB_HAS_DATES_THRESHOLD)
}
}
if (this.body.recordTypeId) {
stmts.recordType = ResourceSerializer.getFormattedRecordType(this.body.recordTypeId)
if (this.body.formatId) {
stmts.format = ResourceSerializer.getFormattedFormat(this.body.formatId)
delete stmts.formatId
delete stmts.recordTypeId
}

Expand All @@ -285,11 +286,11 @@ class ResourceSerializer extends JsonLdItemSerializer {
}
}

ResourceSerializer.getFormattedRecordType = function (recordTypeId) {
const prefLabel = nyplCore.recordTypes()[recordTypeId]?.label
ResourceSerializer.getFormattedFormat = function (formatId) {
const prefLabel = nyplCore.formats()[formatId]?.label
if (!prefLabel) return null
return {
'@id': recordTypeId,
'@id': formatId,
prefLabel
}
}
Expand Down Expand Up @@ -483,6 +484,10 @@ class AggregationSerializer extends JsonLdItemSerializer {
return `res:${this.body.id}`
}

buildAggLabelsFromNyplCore (value, field) {
return nyplCore[field]()[value]?.label
}

async statements () {
const stmts = await JsonLdItemSerializer.prototype.statements.call(this)

Expand All @@ -497,12 +502,10 @@ class AggregationSerializer extends JsonLdItemSerializer {
v.value = p[0]
v.label = p[1]
} else if (field === 'buildingLocation') {
// Build buildingLocation agg labels from nypl-core:
v.label = nyplCore.sierraLocations()[v.value]?.label
} else if (field === 'recordType') {
// Build recordType agg labels from nypl-core:
v.label = nyplCore.recordTypes()[v.value]?.label
// Unknown recordType? Remove it:
v.label = this.buildAggLabelsFromNyplCore(v.value, 'sierraLocations')
} else if (field === 'format') {
v.label = this.buildAggLabelsFromNyplCore(v.value, 'formats')
// Unknown format? Remove it:
if (!v.label) return null
} else {
v.label = v.value
Expand Down
7 changes: 3 additions & 4 deletions lib/load_nypl_core.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const _data = {}
const nyplCoreObjects = require('@nypl/nypl-core-objects')

const loadNyplCoreData = () => {
const vocabularies = {
sierraLocations: 'by-sierra-location',
recordTypes: 'by-record-types',
recapCustomerCodes: 'by-recap-customer-code',
m2CustomerCodes: 'by-m2-customer-code',
patronTypes: 'by-patron-type'
patronTypes: 'by-patron-type',
formats: 'by-formats'
}
return Promise.all(Object.keys(vocabularies).map(async (vocab) => {
const nyplCoreValues = await nyplCoreObjects(vocabularies[vocab])
Expand All @@ -17,9 +16,9 @@ const loadNyplCoreData = () => {

module.exports = {
loadNyplCoreData,
formats: () => _data.formats || {},
patronTypes: () => _data.patronTypes || {},
sierraLocations: () => _data.sierraLocations || {},
recapCustomerCodes: () => _data.recapCustomerCodes || {},
recordTypes: () => _data.recordTypes || {},
m2CustomerCodes: () => _data.m2CustomerCodes || {}
}
3 changes: 1 addition & 2 deletions lib/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ const ITEM_FILTER_AGGREGATIONS = {

// Configures aggregations:
const AGGREGATIONS_SPEC = {
recordType: { terms: { field: 'recordTypeId' } },
format: { terms: { field: 'formatId' } },
buildingLocation: { terms: { field: 'buildingLocationIds' } },
owner: { nested: { path: 'items' }, aggs: { _nested: { terms: { field: 'items.owner_packed' } } } },
subjectLiteral: { terms: { field: 'subjectLiteral.raw' } },
language: { terms: { field: 'language_packed' } },
materialType: { terms: { field: 'materialType_packed' } },
mediaType: { terms: { field: 'mediaType_packed' } },
publisher: { terms: { field: 'publisherLiteral.raw' } },
contributorLiteral: { terms: { field: 'contributorLiteral.raw' } },
Expand Down
2 changes: 1 addition & 1 deletion swagger.v1.1.x.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
{
"name": "filters[*]",
"in": "query",
"description": "Specify a hash of filters to apply, where key is: 'recordType', 'buildingLocation', 'owner', 'subjectLiteral', 'holdingLocation', 'deliveryLocation', 'language', 'materialType', 'mediaType', 'carrierType', 'publisher', 'contributor', 'creator', 'issuance', 'createdYear', 'dateAfter', or 'dateBefore'",
"description": "Specify a hash of filters to apply, where key is: 'format', 'buildingLocation', 'owner', 'subjectLiteral', 'holdingLocation', 'deliveryLocation', 'language', 'materialType', 'mediaType', 'carrierType', 'publisher', 'contributor', 'creator', 'issuance', 'createdYear', 'dateAfter', or 'dateBefore'",
"required": false,
"type": "string"
},
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/es-aggregations-response.json
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@
}
]
},
"recordType": {
"format": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
Expand Down
Loading

0 comments on commit dab3106

Please sign in to comment.