Skip to content
This repository was archived by the owner on May 28, 2023. It is now read-only.

Commit 290f1e6

Browse files
committed
feat(elasticsearch): add request filter configuration
1 parent d959301 commit 290f1e6

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

config/default.json

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
"review"
4141
],
4242
"apiVersion": "5.6",
43-
43+
"useRequestFilter": false,
44+
"overwriteRequestSourceParams": false,
45+
"requestParamsBlacklist": [],
4446
"searchScoring": {
4547
"attributes": {
4648
"attribute_code": {
@@ -81,21 +83,21 @@
8183
"tax_class_id": "tci",
8284
"description": "desc",
8385
"minimal_regular_price": "mrp",
84-
"final_price": "fp",
85-
"price": "p",
86+
"final_price": "fp",
87+
"price": "p",
8688
"special_price": "sp",
8789
"original_final_price": "ofp",
8890
"original_price": "op",
8991
"original_special_price": "osp",
90-
"final_price_incl_tax": "fpit",
92+
"final_price_incl_tax": "fpit",
9193
"original_price_incl_tax": "opit",
9294
"price_incl_tax": "pit",
93-
"special_price_incl_tax": "spit",
95+
"special_price_incl_tax": "spit",
9496
"final_price_tax": "fpt",
9597
"price_tax": "pt",
9698
"special_price_tax": "spt",
9799
"original_price_tax": "opt",
98-
"image": "i",
100+
"image": "i",
99101
"small_image": "si",
100102
"thumbnail": "t"
101103
},
@@ -106,7 +108,7 @@
106108
"default": 10,
107109
"size": 10,
108110
"color": 10
109-
},
111+
},
110112
"priceFilterKey": "final_price",
111113
"priceFilters": {
112114
"ranges": [
@@ -115,7 +117,7 @@
115117
{ "from": 100, "to": 150 },
116118
{ "from": 150 }
117119
]
118-
}
120+
}
119121
},
120122
"varnish": {
121123
"host": "185.246.52.88",
@@ -234,7 +236,7 @@
234236
"useOnlyDefaultUserGroupId": false
235237
},
236238
"review": {
237-
"defaultReviewStatus": 2
239+
"defaultReviewStatus": 2
238240
},
239241
"bodyLimit": "100kb",
240242
"corsHeaders": [
@@ -347,15 +349,6 @@
347349
"includeFields": [ "attribute_code", "id", "entity_type_id", "options", "default_value", "is_user_defined", "frontend_label", "attribute_id", "default_frontend_label", "is_visible_on_front", "is_visible", "is_comparable" ],
348350
"loadByAttributeMetadata": false
349351
},
350-
"productList": {
351-
"sort": "",
352-
"includeFields": [ "type_id", "sku", "product_links", "tax_class_id", "special_price", "special_to_date", "special_from_date", "name", "price", "priceInclTax", "originalPriceInclTax", "originalPrice", "specialPriceInclTax", "id", "image", "sale", "new", "url_key" ],
353-
"excludeFields": [ "configurable_children", "description", "configurable_options", "sgn" ]
354-
},
355-
"productListWithChildren": {
356-
"includeFields": [ "type_id", "sku", "name", "tax_class_id", "special_price", "special_to_date", "special_from_date", "price", "priceInclTax", "originalPriceInclTax", "originalPrice", "specialPriceInclTax", "id", "image", "sale", "new", "configurable_children.image", "configurable_children.sku", "configurable_children.price", "configurable_children.special_price", "configurable_children.priceInclTax", "configurable_children.specialPriceInclTax", "configurable_children.originalPrice", "configurable_children.originalPriceInclTax", "configurable_children.color", "configurable_children.size", "product_links", "url_key"],
357-
"excludeFields": [ "description", "sgn"]
358-
},
359352
"product": {
360353
"excludeFields": [ "updated_at", "created_at", "attribute_set_id", "status", "visibility", "tier_prices", "options_container", "msrp_display_actual_price_type", "has_options", "stock.manage_stock", "stock.use_config_min_qty", "stock.use_config_notify_stock_qty", "stock.stock_id", "stock.use_config_backorders", "stock.use_config_enable_qty_inc", "stock.enable_qty_increments", "stock.use_config_manage_stock", "stock.use_config_min_sale_qty", "stock.notify_stock_qty", "stock.use_config_max_sale_qty", "stock.use_config_max_sale_qty", "stock.qty_increments", "small_image"],
361354
"includeFields": null,

src/api/catalog.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,30 @@ export default ({config, db}) => async function (req, res, body) {
111111
const s = Date.now()
112112
const reqHash = sha3_224(`${JSON.stringify(requestBody)}${req.url}`)
113113
const dynamicRequestHandler = () => {
114-
if (typeof config.entities[entityType] === 'object') {
114+
// filter request parameters
115+
if (config.elasticsearch.useRequestFilter && typeof config.entities[entityType] === 'object') {
115116
const urlParts = elasticBackendUrl.split('?')
116117
const { includeFields, excludeFields } = config.entities[entityType]
118+
119+
const filteredParams = Object.keys(req.query)
120+
.filter(key => !config.elasticsearch.requestParamsBlacklist.includes(key))
121+
.reduce((object, key) => {
122+
object[key] = req.query[key]
123+
return object
124+
}, {})
125+
126+
let _source_include = includeFields
127+
let _source_exclude = excludeFields
128+
129+
if (!config.elasticsearch.overwriteRequestSourceParams) {
130+
_source_include = [...includeFields, ...req.query._source_include]
131+
_source_exclude = [...excludeFields, ...req.query._source_exclude]
132+
}
133+
117134
const urlParams = {
118-
...req.query,
119-
_source_include: includeFields,
120-
_source_exclude: excludeFields
135+
...filteredParams,
136+
_source_include,
137+
_source_exclude
121138
}
122139
elasticBackendUrl = `${urlParts[0]}?${querystring.stringify(urlParams)}`
123140
}

0 commit comments

Comments
 (0)