Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
2,419 additions
and
447 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const acl = require('./../model/acl') | ||
const config = require('./../../../config') | ||
const help = require('./../help') | ||
|
||
const SearchIndex = function (server) { | ||
this.server = server | ||
|
||
server.app.routeMethods('/api/index', { | ||
post: this.post.bind(this) | ||
}) | ||
} | ||
|
||
SearchIndex.prototype.post = function (req, res, next) { | ||
if (!req.dadiApiClient.clientId) { | ||
return help.sendBackJSON(null, res, next)( | ||
acl.createError(req.dadiApiClient) | ||
) | ||
} | ||
|
||
// 404 if Search is not enabled | ||
if (config.get('search.enabled') !== true) { | ||
return next() | ||
} | ||
|
||
res.statusCode = 204 | ||
res.end(JSON.stringify({'message': 'Indexing started'})) | ||
|
||
try { | ||
Object.keys(this.server.components).forEach(key => { | ||
let value = this.server.components[key] | ||
|
||
let hasModel = Object.keys(value).includes('model') && | ||
value.model.constructor.name === 'Model' | ||
|
||
if (hasModel) { | ||
value.model.searchHandler.batchIndex() | ||
} | ||
}) | ||
} catch (err) { | ||
console.log(err) | ||
} | ||
} | ||
|
||
module.exports = server => new SearchIndex(server) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const config = require('./../../../config') | ||
const debug = require('debug')('api:model:search') | ||
|
||
/** | ||
* Searches for documents in the database and returns a | ||
* metadata object. | ||
* | ||
* @param {Object} query - the search query | ||
* @param {Object} options - an options object | ||
* @returns {Promise<Metadata>} | ||
*/ | ||
module.exports = function ({ | ||
client, | ||
options = {} | ||
} = {}) { | ||
let err | ||
|
||
if (!this.searchHandler.canUse()) { | ||
err = new Error('Not Implemented') | ||
err.statusCode = 501 | ||
err.json = { | ||
errors: [{ | ||
message: `Search is disabled or an invalid data connector has been specified.` | ||
}] | ||
} | ||
} else if (!options.search || options.search.length < config.get('search.minQueryLength')) { | ||
err = new Error('Bad Request') | ||
err.statusCode = 400 | ||
err.json = { | ||
errors: [{ | ||
message: `Search query must be at least ${config.get('search.minQueryLength')} characters.` | ||
}] | ||
} | ||
} | ||
|
||
if (err) { | ||
return Promise.reject(err) | ||
} | ||
|
||
return this.validateAccess({ | ||
client, | ||
type: 'read' | ||
}).then(() => { | ||
debug(options.search) | ||
return this.searchHandler.find(options.search) | ||
}) | ||
} |
Oops, something went wrong.