Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scc 3874 #455

Merged
merged 8 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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)
Copy link
Member

Choose a reason for hiding this comment

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

Still need these debugs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd leave them in myself, but can remove them if that seems better


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