Skip to content
Draft
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
89 changes: 89 additions & 0 deletions lib/contributors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const { parseParams } = require('../lib/util')

const ApiRequest = require('./api-request')
const ElasticQueryContributorsBuilder = require('./elasticsearch/elastic-query-contributors-builder')

const CONTRIBUTORS_INDEX = process.env.CONTRIBUTORS_INDEX

const SEARCH_SCOPES = [
'has',
'starts_with'
]

const SORT_FIELDS = [
'contributorName',
'count',
'relevance'
]

// Default sort orders for different search scopes
const SEARCH_SCOPE_SORT_ORDER = {
Copy link
Contributor

Choose a reason for hiding this comment

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

It might help to avoid some bugs down the line if the strings here are constants. Right now all of these strings are defined in multiple places.

has: 'count',
starts_with: 'contributorName'
}

const parseBrowseParams = function (params) {
return parseParams(params, {
q: { type: 'string' },
page: { type: 'int', default: 1 },
per_page: { type: 'int', default: 50, range: [0, 100] },
sort: { type: 'string', range: SORT_FIELDS, default: SEARCH_SCOPE_SORT_ORDER[params.search_scope] || 'termLabel' },
sort_direction: { type: 'string', range: ['asc', 'desc'] },
search_scope: { type: 'string', range: SEARCH_SCOPES, default: '' }
})
}

module.exports = function (app, _private = null) {
app.subjects = {}

app.subjects.browse = function (params, opts, request) {
app.logger.debug('Unparsed params: ', params)
params = parseBrowseParams(params)

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

const body = buildElasticContributorsBody(params)

app.logger.debug('Contrbutors#browse', CONTRIBUTORS_INDEX, body)

return app.esClient.search(body, process.env.SUBJECTS_INDEX)
.then((resp) => {
return {
'@type': 'contributorList',
page: params.page,
per_page: params.per_page,
totalResults: resp.hits?.total?.value,
contributors: resp.hits?.hits?.map((hit) => {
// TODO: parse ES response into frontend-friendly format
return {}
})
}
})
}

// For unit testing
if (_private && typeof _private === 'object') {
_private.buildElasticContributorsBody = buildElasticContributorsBody
_private.parseBrowseParams = parseBrowseParams
}
}

/**
* Given GET params, returns a plainobject with `from`, `size`, `query`,
* `sort`, and any other params necessary to perform the ES query based
* on the GET params.
*
* @return {object} An object that can be posted directly to ES
*/
const buildElasticContributorsBody = function (params) {
const body = {
from: (params.per_page * (params.page - 1)),
size: params.per_page
}

const request = ApiRequest.fromParams(params)
const builder = ElasticQueryContributorsBuilder.forApiRequest(request)

body.query = builder.query.toJson()
return body
}
49 changes: 49 additions & 0 deletions lib/elasticsearch/elastic-query-contributors-builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const ElasticQuery = require('./elastic-query')

class ElasticQueryContributorsBuilder {
constructor (apiRequest) {
this.request = apiRequest
this.query = new ElasticQuery()

switch (this.request.params.search_scope) {
case 'has':
this.requireContainingMatch()
break
case 'starts_with':
this.requirePrefixMatch()
break
default:
this.requireTermMatch()
}
}

/**
* Match on the start of a preferred term
*/
requirePrefixMatch () {

}

/**
* Require exact term match on preferred term or variants
*/
requireTermMatch () {

}

/**
* Require general "and" match on provided query
*/
requireContainingMatch () {

}

/**
* Create a ElasticQueryBuilder for given ApiRequest instance
*/
static forApiRequest (request) {
return new ElasticQueryContributorsBuilder(request)
}
}

module.exports = ElasticQueryContributorsBuilder
8 changes: 8 additions & 0 deletions routes/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ module.exports = function (app) {
.catch((error) => next(error))
})

app.get(`/api/v${VER}/discovery/browse/contributors`, function (req, res, next) {
const params = req.query

return app.contributors.browse(params, { baseUrl: app.baseUrl }, req)
.then((resp) => respond(res, resp, params))
.catch((error) => next(error))
})

app.get(`/api/v${VER}/discovery/vocabularies`, function (req, res, next) {
const params = Object.assign({}, req.query, req.params)

Expand Down