Skip to content

Commit

Permalink
Change: Add uri to relevance sort
Browse files Browse the repository at this point in the history
Also stubs in some keyword matching for standard_number searches
  • Loading branch information
nonword committed Jun 6, 2024
1 parent cc62fe1 commit 513a47c
Show file tree
Hide file tree
Showing 57 changed files with 52,969 additions and 59,336 deletions.
90 changes: 79 additions & 11 deletions lib/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ const parseSearchParams = function (params) {
page: { type: 'int', default: 1 },
per_page: { type: 'int', default: 50, range: [0, 100] },
field: { type: 'string', range: Object.keys(AGGREGATIONS_SPEC) },
sort: { type: 'string', range: Object.keys(SORT_FIELDS) },
sort: { type: 'string', range: Object.keys(SORT_FIELDS), default: 'relevance' },
sort_direction: { type: 'string', range: ['asc', 'desc'] },
search_scope: { type: 'string', range: Object.keys(SEARCH_SCOPES), default: 'all' },
filters: { type: 'hash', fields: FILTER_CONFIG },
Expand Down Expand Up @@ -858,7 +858,6 @@ const buildElasticBody = function (params) {
}
}
body.min_score = 1.65
body.sort = ['_score']

// just filters: no score
} else {
Expand Down Expand Up @@ -887,15 +886,25 @@ const buildElasticBody = function (params) {
placeToPutQuery.query.bool.filter.push(hideByNyplSourceClause)
}

// Default is relevance. Handle case where it's set to something else:
if (params.sort && params.sort !== 'relevance') {
const direction = params.sort_direction || SORT_FIELDS[params.sort].initialDirection
const field = SORT_FIELDS[params.sort].field || params.sort
body.sort = [{ [field]: direction }]
// Apply sort:
let direction
let field
/*
// This doesn't work well because the intuitive sort for shelfmarks differs
// from the alphanumeric sort; We really need a bib.shelfMark_sort field.
if (params.sort === 'relevance' && params.search_scope === 'standard_number') {
field = 'shelfMark.raw'
} else ...
*/

if (params.sort === 'relevance') {
field = '_score'
direction = 'desc'
} else {
field = SORT_FIELDS[params.sort].field || params.sort
direction = params.sort_direction || SORT_FIELDS[params.sort].initialDirection
}

// If no explicit sort given, set a default so that pagination is (mostly) consistent
if (!body.sort) body.sort = ['uri']
body.sort = [{ [field]: direction }, { uri: 'asc' }]

return body
}
Expand Down Expand Up @@ -1318,14 +1327,73 @@ const buildElasticQuery = function (params) {
},
{
match_phrase: {
'creatorLiteral.keyword': {
'creatorLiteral.folded': {
query: querySansQuotes,
boost: 100
}
}
}
])
}

/*
if (['standard_number'].includes(params.search_scope)) {
query.bool.should = query.bool.should.concat([
{
nested: {
path: 'items',
query: {
match_phrase: {
'items.shelfMark.raw': {
query: querySansQuotes,
boost: 50
}
}
}
}
},
{
nested: {
path: 'items',
query: {
prefix: {
'items.shelfMark.raw': {
value: querySansQuotes,
boost: 25
}
}
}
}
},
{
nested: {
path: 'items',
query: {
term: {
'items.shelfMark.raw': {
value: querySansQuotes,
boost: 105
}
}
}
}
},
{
nested: {
path: 'items',
query: {
term: {
'items.idBarcode': {
value: querySansQuotes,
boost: 105
}
}
}
}
}
])
}
*/
}

const advancedParamQueries = ADVANCED_SEARCH_PARAMS
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"body": {
"took": 27,
"took": 142,
"timed_out": false,
"_shards": {
"total": 1,
Expand All @@ -13,7 +13,7 @@
"value": 1,
"relation": "eq"
},
"max_score": 130.26839,
"max_score": null,
"hits": [
{
"_index": "resources-2024-05-31",
Expand Down Expand Up @@ -162,6 +162,10 @@
"25 cm."
]
},
"sort": [
130.26839,
"b13627363"
],
"inner_hits": {
"electronicResources": {
"hits": {
Expand Down
Loading

0 comments on commit 513a47c

Please sign in to comment.