Skip to content

Commit 8ad1c42

Browse files
authored
Merge pull request #460 from NYPL/main
main -> QA subject_prefix
2 parents 2a46ce1 + a1e1b76 commit 8ad1c42

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,6 @@ build
4444
.elasticbeanstalk/*
4545
!.elasticbeanstalk/*.cfg.yml
4646
!.elasticbeanstalk/*.global.yml
47+
48+
49+
config/local*

lib/api-request.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ class ApiRequest {
6969
.some((userParam) => ApiRequest.IDENTIFIER_NUMBER_PARAMS.includes(userParam))
7070
}
7171

72+
hasSubjectPrefix () {
73+
return this.params.subject_prefix
74+
}
75+
7276
static fromParams (params) {
7377
return new ApiRequest(params)
7478
}

lib/elasticsearch/elastic-query-builder.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ class ElasticQueryBuilder {
8888
if (this.request.hasIdentifierNumberParam()) {
8989
this.requireSpecificIdentifierMatch()
9090
}
91+
92+
if (this.request.hasSubjectPrefix()) {
93+
this.applySubjectPrefix()
94+
}
9195
}
9296

9397
/**
@@ -156,6 +160,19 @@ class ElasticQueryBuilder {
156160
// TODO: Boost on subjectLiteral prefix and term matching
157161
}
158162

163+
applySubjectPrefix () {
164+
const q = this.request.params.subject_prefix
165+
this.query.addMust({
166+
bool: {
167+
should: [
168+
prefixMatch('subjectLiteral.raw', q),
169+
prefixMatch('parallelSubjectLiteral.raw', q)
170+
]
171+
}
172+
})
173+
this.boostSubjectMatches()
174+
}
175+
159176
/**
160177
* Build ES query for standard_number searches
161178
*/
@@ -328,6 +345,17 @@ class ElasticQueryBuilder {
328345
])
329346
}
330347

348+
/**
349+
* Boost bibs with strong subjectLiteral matches
350+
**/
351+
boostSubjectMatches () {
352+
const q = this.request.params.subject_prefix
353+
this.query.addShoulds([
354+
termMatch('subjectLiteral.raw', q, 50),
355+
termMatch('parallelSubjectLiteral.raw', q, 50)
356+
])
357+
}
358+
331359
/**
332360
* Boost bibs with strong creator/contributor matches
333361
*/

lib/resources.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ const parseSearchParams = function (params) {
9393
contributor: { type: 'string' },
9494
title: { type: 'string' },
9595
subject: { type: 'string' },
96+
subject_prefix: { type: 'string' },
9697
isbn: { type: 'string' },
9798
issn: { type: 'string' },
9899
lccn: { type: 'string' },
@@ -620,8 +621,11 @@ module.exports = function (app, _private = null) {
620621

621622
// Conduct a search across resources:
622623
app.resources.search = function (params, opts, request) {
624+
app.logger.debug('Unparsed params: ', params)
623625
params = parseSearchParams(params)
624626

627+
app.logger.debug('Parsed params: ', params)
628+
625629
let body = buildElasticBody(params)
626630

627631
// Strip unnecessary _source fields

test/elastic-query-builder.test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,50 @@ describe('ElasticQueryBuilder', () => {
340340
})
341341
})
342342

343+
describe('subject_prefix=', () => {
344+
it('applies subject_prefix clauses to query', () => {
345+
const request = new ApiRequest({ subject_prefix: 'toast' })
346+
const inst = ElasticQueryBuilder.forApiRequest(request)
347+
348+
const query = inst.query.toJson()
349+
350+
expect(query.bool.must[0].bool.should.length).to.equal(2)
351+
expect(query.bool.must[0].bool.should[0])
352+
expect(query.bool.must[0].bool.should[0]).to.deep.equal({
353+
prefix: {
354+
'subjectLiteral.raw': {
355+
value: 'toast',
356+
boost: 1
357+
}
358+
}
359+
})
360+
expect(query.bool.must[0].bool.should[1]).to.deep.equal({
361+
prefix: {
362+
'parallelSubjectLiteral.raw': {
363+
value: 'toast',
364+
boost: 1
365+
}
366+
}
367+
})
368+
expect(query.bool.should[0]).to.deep.equal({
369+
term: {
370+
'subjectLiteral.raw': {
371+
value: 'toast',
372+
boost: 50
373+
}
374+
}
375+
})
376+
expect(query.bool.should[1]).to.deep.equal({
377+
term: {
378+
'parallelSubjectLiteral.raw': {
379+
value: 'toast',
380+
boost: 50
381+
}
382+
}
383+
})
384+
})
385+
})
386+
343387
describe('multiple adv search params', () => {
344388
it('applies multiple param clauses to query', () => {
345389
const request = new ApiRequest({

0 commit comments

Comments
 (0)