|
| 1 | +import { Router } from 'express'; |
| 2 | +import { apiStatus } from '../../lib/util'; |
| 3 | +import { buildMultiEntityUrl } from '../../lib/elastic'; |
| 4 | +import request from 'request'; |
| 5 | +import get from 'lodash/get'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Builds ES query based on config |
| 9 | + */ |
| 10 | +const buildQuery = ({ value, config }) => { |
| 11 | + const searchedFields = get(config, 'urlModule.map.searchedFields', []) |
| 12 | + .map((field) => ({ match_phrase: { [field]: { query: value } } })) |
| 13 | + const searchedEntities = get(config, 'urlModule.map.searchedEntities', []) |
| 14 | + .map((entity) => ({ type: { value: entity } })) |
| 15 | + |
| 16 | + return { |
| 17 | + query: { |
| 18 | + bool: { |
| 19 | + filter: { |
| 20 | + bool: { |
| 21 | + should: searchedFields, |
| 22 | + filter: { |
| 23 | + bool: { |
| 24 | + should: searchedEntities |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + }, |
| 31 | + size: 1 // we need only one record |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * checks result equality because ES can return record even if searched value is not EXACLY what we want (check `match_phrase` in ES docs) |
| 37 | + */ |
| 38 | +const checkFieldValueEquality = ({ config, response, value }) => { |
| 39 | + const isEqualValue = get(config, 'urlModule.map.searchedFields', []) |
| 40 | + .find((field) => response._source[field] === value) |
| 41 | + |
| 42 | + return Boolean(isEqualValue) |
| 43 | +} |
| 44 | + |
| 45 | +module.exports = ({ config }) => { |
| 46 | + const router = Router() |
| 47 | + router.post('/:index', (req, res) => { |
| 48 | + const { url, excludeFields, includeFields } = req.body |
| 49 | + if (!url) { |
| 50 | + return apiStatus(res, 'Missing url', 500); |
| 51 | + } |
| 52 | + |
| 53 | + const esUrl = buildMultiEntityUrl({ |
| 54 | + config, |
| 55 | + includeFields: includeFields ? includeFields.concat(get(config, 'urlModule.map.includeFields', [])) : [], |
| 56 | + excludeFields |
| 57 | + }) |
| 58 | + const query = buildQuery({ value: url, config }) |
| 59 | + |
| 60 | + // Only pass auth if configured |
| 61 | + let auth = null; |
| 62 | + if (config.elasticsearch.user || config.elasticsearch.password) { |
| 63 | + auth = { |
| 64 | + user: config.elasticsearch.user, |
| 65 | + pass: config.elasticsearch.password |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // make simple ES search |
| 70 | + request({ |
| 71 | + uri: esUrl, |
| 72 | + method: 'POST', |
| 73 | + body: query, |
| 74 | + json: true, |
| 75 | + auth: auth |
| 76 | + }, (_err, _res, _resBody) => { |
| 77 | + if (_err) { |
| 78 | + console.log(_err) |
| 79 | + return apiStatus(res, new Error('ES search error'), 500); |
| 80 | + } |
| 81 | + const responseRecord = _resBody.hits.hits[0] |
| 82 | + if (responseRecord && checkFieldValueEquality({ config, response: responseRecord, value: req.body.url })) { |
| 83 | + return res.json(responseRecord) |
| 84 | + } |
| 85 | + res.json() |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
| 89 | + return router |
| 90 | +} |
0 commit comments