Skip to content
2 changes: 1 addition & 1 deletion data/buildingLocations.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{ "value": "ma", "label": "Stephen A. Schwarzman Building (SASB)","nickname": "SASB" }, { "value": "pa", "label": "The New York Public Library for the Performing Arts (LPA)","nickname": "LPA" },{ "value": "sc", "label": "Schomburg Center for Research in Black Culture","nickname": "Schomburg" },{ "value": "rc", "label": "Offsite - request in advance"},{ "value": "bu", "label": "Stavros Niarchos Foundation Library (SNFL)", "nickname": "SNFL" }]
[{ "value": "ma", "label": "Stephen A. Schwarzman Building (SASB)", "shortLabel": "Stephen A. Schwarzman Building", "nickname": "SASB" }, { "value": "pa", "shortLabel": "The New York Public Library for the Performing Arts", "label": "The New York Public Library for the Performing Arts (LPA)","nickname": "LPA" },{ "value": "sc", "label": "Schomburg Center for Research in Black Culture", "shortLabel": "Schomburg Center for Research in Black Culture", "nickname": "Schomburg" },{ "value": "rc", "label": "Offsite - request in advance", "shortLabel": "Offsite"},{ "value": "bu", "label": "Stavros Niarchos Foundation Library (SNFL)", "shortLabel": "Stavros Niarchos Foundation Library", "nickname": "SNFL" }]
26 changes: 26 additions & 0 deletions lib/jsonld_serializers.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,12 @@ ResourceSerializer.formatItemFilterAggregations = function (aggregations) {
// If it's a packed value, parse out the value and label:
if (value.split('||').length === 2) {
;[value, label] = value.split('||')
// For item location, add the buildingLocation label
} else if (field === 'location') {
const loc = buildingLocations.find(l => l.value === value)
if (loc) {
label = loc.shortLabel
}
}
return {
value,
Expand Down Expand Up @@ -380,6 +386,11 @@ class ItemResourceSerializer extends JsonLdItemSerializer {
stmts.holdingLocation = ItemResourceSerializer.getFormattedHoldingLocation(this.body.holdingLocation)
}

const buildingLocation = ItemResourceSerializer.getBuildingLocation(this.body.holdingLocation)
if (buildingLocation) {
stmts.buildingLocation = buildingLocation
}

if (this.body.collectionId) {
stmts.collection = ResourceSerializer.getFormattedCollections(this.body.collectionId)
delete stmts.collectionId
Expand Down Expand Up @@ -411,6 +422,21 @@ class ItemResourceSerializer extends JsonLdItemSerializer {
}]
}

static getBuildingLocation (location) {
const loc = Array.isArray(location) ? location[0] : location
const parentLoc = loc?.['@id']?.split(':')[1]?.substring(0, 2)
if (parentLoc) {
const building = buildingLocations.find(l => l.value === parentLoc)
if (building) {
return [{
'@id': building.value,
prefLabel: building.shortLabel
}]
}
}
return null

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since buildingLocation is calculated from holdingLocation, partner items will continue to return nothing for both. This case is already accounted for on the frontend

}

// Given an item, returns item with an added `identifier`
// of form 'urn:[sourceIdentifierPrefix]:[sourceIdentifier]'
// e.g.
Expand Down
24 changes: 20 additions & 4 deletions lib/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { IndexSearchError, IndexConnectionError, InvalidQuerySyntaxError } = requ
const ResponseMassager = require('./response_massager.js')
const AvailableDeliveryLocationTypes = require('./available_delivery_location_types')

const { parseParams, deepValue } = require('../lib/util')
const { parseParams, deepValue, ITEM_LOCATION_AGG_SCRIPT, ITEM_LOCATION_FILTER_SCRIPT } = require('./util')

const ApiRequest = require('./api-request')
const ElasticQueryBuilder = require('./elasticsearch/elastic-query-builder')
Expand All @@ -28,7 +28,17 @@ const Item = require('./models/Item.js')
const RESOURCES_INDEX = process.env.RESOURCES_INDEX

const ITEM_FILTER_AGGREGATIONS = {
item_location: { nested: { path: 'items' }, aggs: { _nested: { terms: { size: 100, field: 'items.holdingLocation_packed' } } } },
item_location: {
nested: { path: 'items' },
aggs: {
_nested: {
terms: {
size: 100,
script: { source: ITEM_LOCATION_AGG_SCRIPT }
}
}
}
},
item_status: { nested: { path: 'items' }, aggs: { _nested: { terms: { size: 100, field: 'items.status_packed' } } } },
item_format: { nested: { path: 'items' }, aggs: { _nested: { terms: { size: 100, field: 'items.formatLiteral' } } } }
}
Expand Down Expand Up @@ -212,6 +222,7 @@ module.exports = function (app, _private = null) {
}).then((resp) => {
const hitsAndItemAggregations = resp.hits.hits[0]._source
hitsAndItemAggregations.itemAggregations = resp.aggregations

return ResourceSerializer.serialize(hitsAndItemAggregations, Object.assign(opts, { root: true }))
})
})
Expand Down Expand Up @@ -488,8 +499,13 @@ module.exports = function (app, _private = null) {
},
location: (locations) => {
return {
terms: {
'items.holdingLocation.id': locations
script: {
script: {
source: ITEM_LOCATION_FILTER_SCRIPT,
params: {
locations
}
}
}
}
},
Expand Down
7 changes: 7 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,10 @@ exports.isInRecap = (item) => {
exports.regexEscape = function (str) {
return str.replace(/[-/\\^$*+?.()|[\]{}]/g, (match) => { return '\\' + match })
}

/**
* Parent location values from holidng location, for item location filter/aggs
*/
const buildLocationScript = (returnOnFailure, returnOnSuccess) => `def locs = doc['items.holdingLocation.id']; if (locs.size() == 0) return ${returnOnFailure}; def loc = locs.value; int colonIdx = loc.indexOf(':'); if (colonIdx == -1) return ${returnOnFailure}; def locId = loc.substring(colonIdx + 1); if (locId.length() < 2) return ${returnOnFailure}; def parentLoc = locId.substring(0, 2); ${returnOnSuccess}`
exports.ITEM_LOCATION_AGG_SCRIPT = buildLocationScript('null', "if (['ma', 'pa', 'sc', 'rc', 'bu'].contains(parentLoc)) { return parentLoc; } return null;")
exports.ITEM_LOCATION_FILTER_SCRIPT = buildLocationScript('false', "return ['ma', 'pa', 'sc', 'rc', 'bu'].contains(parentLoc) && params.locations.contains(parentLoc);")
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading