Skip to content

Commit

Permalink
Merge pull request #461 from NYPL/qa
Browse files Browse the repository at this point in the history
deploy search scope updates to production
  • Loading branch information
charmingduchess authored Feb 10, 2025
2 parents 5c10bd1 + 8ad1c42 commit 8790427
Show file tree
Hide file tree
Showing 13 changed files with 44,190 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ build
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml


config/local*
4 changes: 4 additions & 0 deletions lib/api-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ class ApiRequest {
.some((userParam) => ApiRequest.IDENTIFIER_NUMBER_PARAMS.includes(userParam))
}

hasSubjectPrefix () {
return this.params.subject_prefix
}

static fromParams (params) {
return new ApiRequest(params)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/elasticsearch/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const SEARCH_SCOPES = {
'titleDisplay.folded',
'contentsTitle.folded',
'tableOfContents.folded',
'genreForm',
'donor.folded',
'parallelTitle.folded^5',
'parallelTitleDisplay.folded',
Expand All @@ -45,7 +46,6 @@ const SEARCH_SCOPES = {
'titleDisplay.folded',
'seriesStatement.folded',
'contentsTitle.folded',
'tableOfContents.folded',
'donor.folded',
'parallelTitle.folded^5',
'parallelTitleDisplay.folded',
Expand Down
28 changes: 28 additions & 0 deletions lib/elasticsearch/elastic-query-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ class ElasticQueryBuilder {
if (this.request.hasIdentifierNumberParam()) {
this.requireSpecificIdentifierMatch()
}

if (this.request.hasSubjectPrefix()) {
this.applySubjectPrefix()
}
}

/**
Expand Down Expand Up @@ -156,6 +160,19 @@ class ElasticQueryBuilder {
// TODO: Boost on subjectLiteral prefix and term matching
}

applySubjectPrefix () {
const q = this.request.params.subject_prefix
this.query.addMust({
bool: {
should: [
prefixMatch('subjectLiteral.raw', q),
prefixMatch('parallelSubjectLiteral.raw', q)
]
}
})
this.boostSubjectMatches()
}

/**
* Build ES query for standard_number searches
*/
Expand Down Expand Up @@ -328,6 +345,17 @@ class ElasticQueryBuilder {
])
}

/**
* Boost bibs with strong subjectLiteral matches
**/
boostSubjectMatches () {
const q = this.request.params.subject_prefix
this.query.addShoulds([
termMatch('subjectLiteral.raw', q, 50),
termMatch('parallelSubjectLiteral.raw', q, 50)
])
}

/**
* Boost bibs with strong creator/contributor matches
*/
Expand Down
4 changes: 4 additions & 0 deletions lib/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ const parseSearchParams = function (params) {
contributor: { type: 'string' },
title: { type: 'string' },
subject: { type: 'string' },
subject_prefix: { type: 'string' },
isbn: { type: 'string' },
issn: { type: 'string' },
lccn: { type: 'string' },
Expand Down Expand Up @@ -620,8 +621,11 @@ module.exports = function (app, _private = null) {

// Conduct a search across resources:
app.resources.search = function (params, opts, request) {
app.logger.debug('Unparsed params: ', params)
params = parseSearchParams(params)

app.logger.debug('Parsed params: ', params)

let body = buildElasticBody(params)

// Strip unnecessary _source fields
Expand Down
44 changes: 44 additions & 0 deletions test/elastic-query-builder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,50 @@ describe('ElasticQueryBuilder', () => {
})
})

describe('subject_prefix=', () => {
it('applies subject_prefix clauses to query', () => {
const request = new ApiRequest({ subject_prefix: 'toast' })
const inst = ElasticQueryBuilder.forApiRequest(request)

const query = inst.query.toJson()

expect(query.bool.must[0].bool.should.length).to.equal(2)
expect(query.bool.must[0].bool.should[0])
expect(query.bool.must[0].bool.should[0]).to.deep.equal({
prefix: {
'subjectLiteral.raw': {
value: 'toast',
boost: 1
}
}
})
expect(query.bool.must[0].bool.should[1]).to.deep.equal({
prefix: {
'parallelSubjectLiteral.raw': {
value: 'toast',
boost: 1
}
}
})
expect(query.bool.should[0]).to.deep.equal({
term: {
'subjectLiteral.raw': {
value: 'toast',
boost: 50
}
}
})
expect(query.bool.should[1]).to.deep.equal({
term: {
'parallelSubjectLiteral.raw': {
value: 'toast',
boost: 50
}
}
})
})
})

describe('multiple adv search params', () => {
it('applies multiple param clauses to query', () => {
const request = new ApiRequest({
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/query-30fa9978a3de9d1d665b329a9ad2bef6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"took": 58,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 0,
"relation": "eq"
},
"max_score": null,
"hits": []
}
}
Loading

0 comments on commit 8790427

Please sign in to comment.