diff --git a/openapi-v2.yaml b/openapi-v2-wip.yaml similarity index 100% rename from openapi-v2.yaml rename to openapi-v2-wip.yaml diff --git a/server/metadata/get_metadata.js b/server/metadata/get_metadata.js index b508997..a4926bc 100644 --- a/server/metadata/get_metadata.js +++ b/server/metadata/get_metadata.js @@ -2,7 +2,6 @@ const client = require('../esclient'); const types = require('../types'); function process_results(results) { - console.log(results) var new_results = results.body["_source"] // Add some metadata to the metadata diff --git a/server/search/search.js b/server/search/search.js index 8e54c6c..4478dcb 100644 --- a/server/search/search.js +++ b/server/search/search.js @@ -51,11 +51,15 @@ function search(q, type, page, pageSize, preference) { body, size: pageSize, from: page * pageSize, - timeout: '300ms', // This is more of a fallback/dirty workaround. - max_concurrent_shard_requests: 40, - pre_filter_shard_size: 2048, + + // Optimizations // https://www.elastic.co/guide/en/elasticsearch/reference/current/search-shard-routing.html#shard-and-node-preference - preference: preference, + max_concurrent_shard_requests: 24, // Defines the number of concurrent shard requests per node this search executes concurrently. This value should be used to limit the impact of the search on the cluster in order to limit the number of concurrent shard requests. Defaults to 5. + pre_filter_shard_size: 2048, + preference: '_local', + // timeout: '800ms', // Specifies the period of time to wait for a response from each shard. If no response is received before the timeout expires, the request fails and returns an error. + // allow_partial_search_results: true, // If true, returns partial results if there are shard request timeouts or shard failures. If false, returns an error with no partial results. + // ignore_unavailable: true, // If false, the request returns an error if it targets a missing or closed index. }); } diff --git a/server/server.js b/server/server.js index a66e2ad..431fd11 100644 --- a/server/server.js +++ b/server/server.js @@ -7,13 +7,19 @@ const cidValidator = require('./metadata/cid_validator'); const app = express(); const port = 9615; -function error(res, code, err) { - console.error(`${code}: ${err}`); - console.trace(err); +function error(req, res, code, err) { + console.error(`${req.url} ${code}: ${err}`); // Unwrap ES errors. if (typeof err === 'object' && 'body' in err && 'error' in err.body) { - console.trace(err.body.error); + err = err.body.error; + } + + if (process.env.NODE_ENV === 'production') { + // Don't leak production errors + err = 'Internal Server Error' + } else { + console.trace(err); } res.status(code).json({ error: `${err}` }).end(); @@ -27,7 +33,7 @@ app.get('/search', (req, res, next) => { const pageSize = 15; if (!('q' in req.query)) { - error(res, 400, 'query argument missing'); + error(req, res, 400, 'query argument missing'); } let page = 0; @@ -37,14 +43,14 @@ app.get('/search', (req, res, next) => { // For performance reasons, don't allow paging too far down if (page > maxPage) { - error(res, 400, 'paging not allowed beyond 100'); + error(req, res, 400, 'paging not allowed beyond 100'); } } search(req.query.q, req.query.type, page, pageSize, req.ip).then((r) => { const { hits } = r.body; - console.debug(`${req.url} 200: Returning ${hits.hits.length} results for ${req.ip}`); + console.debug(`${req.url} 200: Returning ${hits.hits.length} results`); hits.page_size = pageSize; hits.page_count = Math.ceil(hits.total.value / pageSize); @@ -64,7 +70,7 @@ app.get('/metadata/:cid/', function (req, res, next) { let cid = req.params['cid'] if (!cidValidator.Validate(cid)) { - error(res, 400, 'invalid cid'); + error(req, res, 400, 'invalid cid'); } getMetadata(cid).then((r) => { @@ -73,16 +79,11 @@ app.get('/metadata/:cid/', function (req, res, next) { }) app.use(function (err, req, res, next) { - if (process.env.NODE_ENV === 'production') { - // Don't leak details in production - error(res, 500, 'Internal Server Error'); - } - if (res.headersSent) { return next(err); } - error(res, 500, err); + error(req, res, 500, err); }); app.listen(port, '127.0.0.1', () => console.log(`ipfs-search search API listening on port ${port}!`));