From 66c7df7e04bd87085b5fcf2126c577fee3df604f Mon Sep 17 00:00:00 2001 From: Paul Beaudoin Date: Wed, 5 Jun 2024 17:08:03 -0400 Subject: [PATCH] Incorporate latest all, title, and contrib queries Incorporate latest queries from Mike. --- config/qa.env | 4 +- lib/resources.js | 118 +++++++++++++++++++++++++++++++---------------- 2 files changed, 80 insertions(+), 42 deletions(-) diff --git a/config/qa.env b/config/qa.env index f367bf12..22be0b76 100644 --- a/config/qa.env +++ b/config/qa.env @@ -1,7 +1,7 @@ # Greg built self-hosted production domain: ENCRYPTED_ELASTICSEARCH_URI=AQECAHh7ea2tyZ6phZgT4B9BDKwguhlFtRC6hgt+7HbmeFsrsgAAAHgwdgYJKoZIhvcNAQcGoGkwZwIBADBiBgkqhkiG9w0BBwEwHgYJYIZIAWUDBAEuMBEEDIYpOz/BbRlJZUul7gIBEIA1idumQ6fdf/j5/pzF4t96MGGH/eV1gD4WCyLUnScgNYqtRNK0ajRO6XVroswsrJtgCwUerDM= -# 2024-05-29: -ENCRYPTED_RESOURCES_INDEX=AQECAHh7ea2tyZ6phZgT4B9BDKwguhlFtRC6hgt+7HbmeFsrsgAAAHIwcAYJKoZIhvcNAQcGoGMwYQIBADBcBgkqhkiG9w0BBwEwHgYJYIZIAWUDBAEuMBEEDFVXv05VtUwVtXit3wIBEIAv7GbABuJ+cOEA7ZqVzPEdL6zGytekVKeQnh4z50B2RXXsUKlMUz6osvh6S4koOYY= +# 2024-05-31: +ENCRYPTED_RESOURCES_INDEX=AQECAHh7ea2tyZ6phZgT4B9BDKwguhlFtRC6hgt+7HbmeFsrsgAAAHIwcAYJKoZIhvcNAQcGoGMwYQIBADBcBgkqhkiG9w0BBwEwHgYJYIZIAWUDBAEuMBEEDCMPc+iiGA0Q3PdUJQIBEIAvKyPl8IkGYjTCX1hmBAI3yGYUvyc7kpD7EHTMxx6WH3Esu0Wge8+DGDPqDpuJX9Y= ENCRYPTED_SCSB_URL=AQECAHh7ea2tyZ6phZgT4B9BDKwguhlFtRC6hgt+7HbmeFsrsgAAAH8wfQYJKoZIhvcNAQcGoHAwbgIBADBpBgkqhkiG9w0BBwEwHgYJYIZIAWUDBAEuMBEEDBKllElmWYLxGOGopQIBEIA8JJyKde/8m8iCJGKR5D8HoTJhXHeyvw9eIDeuUNKiXLfJwoVz+PDAZSxkCQtM9O91zGhXbe3l6Bk1RlYJ ENCRYPTED_SCSB_API_KEY=AQECAHh7ea2tyZ6phZgT4B9BDKwguhlFtRC6hgt+7HbmeFsrsgAAAGMwYQYJKoZIhvcNAQcGoFQwUgIBADBNBgkqhkiG9w0BBwEwHgYJYIZIAWUDBAEuMBEEDNw8KXkyN8HvtjAX0gIBEIAgX+XG2fxTj6kSchrd/dfHB05KU5pkT0LtPxUTuNCXoLc= diff --git a/lib/resources.js b/lib/resources.js index 5cc1a0d5..c4a4411f 100644 --- a/lib/resources.js +++ b/lib/resources.js @@ -729,6 +729,7 @@ module.exports = function (app, _private = null) { return resp }) .then((updatedResponse) => ResourceResultsSerializer.serialize(updatedResponse, opts)) + .then((resp) => Object.assign(resp, { debug: { query: body } })) }) } @@ -1228,6 +1229,8 @@ const buildElasticQuery = function (params) { // Mike, Elastic consultant, additions: query.bool.should = [] + + // Boost NYPL hits: query.bool.should.push({ term: { nyplSource: { @@ -1236,57 +1239,92 @@ const buildElasticQuery = function (params) { } } }) - if (['all', 'title'].includes(params.search_scope)) { - query.bool.should.push({ - match_phrase: { - 'title.folded': { - query: params.q, - boost: 50 + + // Boost on-site bibs: + query.bool.should.push({ + nested: { + path: 'items', + query: { + regexp: { + 'items.holdingLocation.id': { + value: 'loc:(ma|pa|sc).*', + flags: 'ALL', + boost: 2 + } } } - }) - query.bool.should.push({ - prefix: { - 'title.keywordNormalized': { - value: params.q, - boost: 100 + } + }) + + // Build a version of the query without quotes, if used, since the quote + // character will not be in the literal value: + const querySansQuotes = /^"[^"]+"$/.test(params.q) + ? params.q.substring(1, params.q.length - 1) + : params.q + + if (['all', 'title'].includes(params.search_scope)) { + query.bool.should = query.bool.should.concat([ + { + match_phrase: { + 'title.folded': { + query: querySansQuotes, + boost: 50 + } } - } - }) - query.bool.should.push({ - term: { - 'title.keywordNormalized': { - value: params.q, - boost: 105 + }, + { + prefix: { + 'title.keywordLowercasedStripped': { + value: querySansQuotes, + boost: 25 + } } - } - }) - query.bool.should.push({ - term: { - 'title.keyword': { - value: params.q, - boost: 150 + }, + { + term: { + 'title.keywordLowercasedStripped': { + value: querySansQuotes, + boost: 105 + } + } + }, + { + term: { + 'title.keywordLowercased': { + value: querySansQuotes, + boost: 110 + } + } + }, + { + term: { + 'title.keyword': { + value: querySansQuotes, + boost: 150 + } } } - }) + ]) } if (['all', 'contributor'].includes(params.search_scope)) { - query.bool.should.push({ - prefix: { - 'contributorLiteral.keyword': { - value: params.q, - boost: 100 + query.bool.should = query.bool.should.concat([ + { + match_phrase: { + 'contributorLiteral.folded': { + query: querySansQuotes, + boost: 100 + } } - } - }) - query.bool.should.push({ - term: { - 'contributorLiteral.keyword': { - value: params.q, - boost: 105 + }, + { + match_phrase: { + 'creatorLiteral.keyword': { + query: querySansQuotes, + boost: 100 + } } } - }) + ]) } }