Skip to content
This repository was archived by the owner on May 28, 2023. It is now read-only.
Merged
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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [1.12.0-rc1] - UNRELEASED

### Added
- Add url module - @gibkigonzo (#3942)

### Fixed

### Changed / Improved


## [1.11.0] - 2019.12.20

### Fixed
Expand Down
7 changes: 7 additions & 0 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -282,5 +282,12 @@
"description": 1,
"sku": 1,
"configurable_children.sku": 1
},
"urlModule": {
"map": {
"includeFields": ["url_path", "identifier", "id", "slug", "sku", "type_id"],
"searchedFields": ["url_path", "identifier"],
"searchedEntities": ["product", "category", "cms_page"]
}
}
}
4 changes: 4 additions & 0 deletions src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import review from './review';
import cart from './cart';
import product from './product';
import sync from './sync';
import url from './url';

export default ({ config, db }) => {
let api = Router();
Expand Down Expand Up @@ -36,6 +37,9 @@ export default ({ config, db }) => {
// mount the sync resource
api.use('/sync', sync({ config, db }))

// mount the url resource
api.use('/url', url({ config, db }))

// perhaps expose some API metadata at the root
api.get('/', (req, res) => {
res.json({ version });
Expand Down
10 changes: 10 additions & 0 deletions src/api/url/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Router } from 'express';
import createMapRoute from './map';

module.exports = ({ config }) => {
const router = Router()

router.use('/map', createMapRoute({ config }))

return router
}
90 changes: 90 additions & 0 deletions src/api/url/map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { Router } from 'express';
import { apiStatus } from '../../lib/util';
import { buildMultiEntityUrl } from '../../lib/elastic';
import request from 'request';
import get from 'lodash/get';

/**
* Builds ES query based on config
*/
const buildQuery = ({ value, config }) => {
const searchedFields = get(config, 'urlModule.map.searchedFields', [])
.map((field) => ({ match_phrase: { [field]: { query: value } } }))
const searchedEntities = get(config, 'urlModule.map.searchedEntities', [])
.map((entity) => ({ type: { value: entity } }))

return {
query: {
bool: {
filter: {
bool: {
should: searchedFields,
filter: {
bool: {
should: searchedEntities
}
}
}
}
}
},
size: 1 // we need only one record
}
}

/**
* checks result equality because ES can return record even if searched value is not EXACLY what we want (check `match_phrase` in ES docs)
*/
const checkFieldValueEquality = ({ config, response, value }) => {
const isEqualValue = get(config, 'urlModule.map.searchedFields', [])
.find((field) => response._source[field] === value)

return Boolean(isEqualValue)
}

module.exports = ({ config }) => {
const router = Router()
router.post('/:index', (req, res) => {
const { url, excludeFields, includeFields } = req.body
if (!url) {
return apiStatus(res, 'Missing url', 500);
}

const esUrl = buildMultiEntityUrl({
config,
includeFields: includeFields ? includeFields.concat(get(config, 'urlModule.map.includeFields', [])) : [],
excludeFields
})
const query = buildQuery({ value: url, config })

// Only pass auth if configured
let auth = null;
if (config.elasticsearch.user || config.elasticsearch.password) {
auth = {
user: config.elasticsearch.user,
pass: config.elasticsearch.password
}
}

// make simple ES search
request({
uri: esUrl,
method: 'POST',
body: query,
json: true,
auth: auth
}, (_err, _res, _resBody) => {
if (_err) {
console.log(_err)
return apiStatus(res, new Error('ES search error'), 500);
}
const responseRecord = _resBody.hits.hits[0]
if (responseRecord && checkFieldValueEquality({ config, response: responseRecord, value: req.body.url })) {
return res.json(responseRecord)
}
res.json()
})
})

return router
}
14 changes: 13 additions & 1 deletion src/lib/elastic.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ function adjustBackendProxyUrl (req, indexName, entityType, config) {
return url
}

/**
* similar to `adjustBackendProxyUrl`, builds multi-entity query url
*/
function buildMultiEntityUrl ({ config, includeFields = [], excludeFields = [] }) {
let url = `${config.elasticsearch.host}:${config.elasticsearch.port}/_search?_source_include=${includeFields.join(',')}&_source_exclude=${excludeFields.join(',')}`
if (!url.startsWith('http')) {
url = config.elasticsearch.protocol + '://' + url
}
return url
}

function adjustQuery (esQuery, entityType, config) {
if (parseInt(config.elasticsearch.apiVersion) < 6) {
esQuery.type = entityType
Expand Down Expand Up @@ -261,5 +272,6 @@ module.exports = {
getClient,
getHits,
adjustIndexName,
putMappings
putMappings,
buildMultiEntityUrl
}