/*!
- * @ecomplus/search-engine
- * (c) E-Com Club <ti@e-com.club>
- * Released under the MIT License.
- */
-
-/**
+ /**
* JS lib to handle products search with E-Com Plus stores.
* {@link https://github.com/ecomclub/search-engine GitHub}
*
@@ -225,7 +219,7 @@ index.js
diff --git a/docs/global.html b/docs/global.html
index bb4c367..2eac217 100644
--- a/docs/global.html
+++ b/docs/global.html
@@ -27,7 +27,7 @@
@@ -116,6 +116,465 @@
+
+ Methods
+
+
+
+
+
+
+ addGlobalFilter(field, value)
+
+
+
+
+
+
+
+
+
+ - Source:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Add custom fetch DSL middleware with simple term filter for all instances.
+
+
+
+
+
+
+
+
+
+
+ Example
+
+ import { addGlobalFilter } from '@ecomplus/search-engine'
+addGlobalFilter('channel_id', 123)
+
+
+
+
+ Parameters:
+
+
+
+
+
+
+ Name
+
+
+ Type
+
+
+
+
+
+ Description
+
+
+
+
+
+
+
+
+ field
+
+
+
+
+
+string
+
+
+
+
+
+
+
+
+
+ Item field to filter with dot notation
+
+
+
+
+
+
+ value
+
+
+
+
+
+string
+|
+
+number
+|
+
+boolean
+
+
+
+
+
+
+
+
+
+ Searched field value
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ setSalesChannel(channelId)
+
+
+
+
+
+
+
+
+
+ - Source:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Set channel ID for searches in all instances.
+
+
+
+
+
+
+
+
+
+
+ Example
+
+ import { setSalesChannel } from '@ecomplus/search-engine'
+setSalesChannel(123)
+
+
+
+
+ Parameters:
+
+
+
+
+
+
+ Name
+
+
+ Type
+
+
+
+
+
+ Description
+
+
+
+
+
+
+
+
+ channelId
+
+
+
+
+
+number
+
+
+
+
+
+
+
+
+
+ Sales channel ID
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ setWarehouse(warehouseCode)
+
+
+
+
+
+
+
+
+
+ - Source:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Set warehouse (multi CD) code for searches in all instances.
+
+
+
+
+
+
+
+
+
+
+ Example
+
+ import { setWarehouse } from '@ecomplus/search-engine'
+setWarehouse('MG')
+
+
+
+
+ Parameters:
+
+
+
+
+
+
+ Name
+
+
+ Type
+
+
+
+
+
+ Description
+
+
+
+
+
+
+
+
+ warehouseCode
+
+
+
+
+
+string
+
+
+
+
+
+
+
+
+
+ Warehouse code
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -278,7 +737,7 @@ result
Source:
@@ -1068,7 +1527,7 @@ Type:
diff --git a/docs/index.html b/docs/index.html
index e20d102..cfcdff8 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -27,7 +27,7 @@
@@ -112,7 +112,7 @@ CDN
diff --git a/docs/methods_fetch.js.html b/docs/methods_fetch.js.html
index a052e27..abbf37d 100644
--- a/docs/methods_fetch.js.html
+++ b/docs/methods_fetch.js.html
@@ -27,7 +27,7 @@
@@ -43,6 +43,8 @@ methods/fetch.js
import { search } from '@ecomplus/client'
+import * as cloneDeep from 'lodash.clonedeep'
+import dslMiddlewares from './../lib/dsl-middlewares'
export default (self, isSimpleSearch, axiosConfig) => {
// mount axios req options for complex or simpĺe search
@@ -51,9 +53,20 @@ methods/fetch.js
axiosConfig
}
+ // prepare search DSL
+ let dsl = cloneDeep(self.dsl)
+ dslMiddlewares.forEach(fn => {
+ if (typeof fn === 'function') {
+ const _dsl = fn(dsl)
+ if (_dsl) {
+ dsl = _dsl
+ }
+ }
+ })
+
if (isSimpleSearch === true) {
// https://www.elastic.co/guide/en/elasticsearch/reference/6.3/search-uri-request.html
- const { query } = self.dsl
+ const { query } = dsl
reqOptions.url += '?q='
if (query && query.bool && Array.isArray(query.bool.filter)) {
// parse query filters to string
@@ -73,13 +86,13 @@ methods/fetch.js
}
// handle pagination
;['from', 'size'].forEach(field => {
- if (self.dsl[field]) {
- reqOptions.url += `&${field}=${self.dsl[field]}`
+ if (dsl[field]) {
+ reqOptions.url += `&${field}=${dsl[field]}`
}
})
} else {
reqOptions.method = 'post'
- reqOptions.data = self.dsl
+ reqOptions.data = dsl
if (isSimpleSearch && !axiosConfig) {
// fallback for old reference with `fetch(axiosConfig)`
reqOptions.axiosConfig = isSimpleSearch
@@ -165,7 +178,7 @@ methods/fetch.js
diff --git a/docs/methods_get-brands.js.html b/docs/methods_get-brands.js.html
index fd8c2dc..8a1c176 100644
--- a/docs/methods_get-brands.js.html
+++ b/docs/methods_get-brands.js.html
@@ -27,7 +27,7 @@
@@ -84,7 +84,7 @@ methods/get-brands.js
diff --git a/docs/methods_get-categories.js.html b/docs/methods_get-categories.js.html
index 67e1710..e9f7a7d 100644
--- a/docs/methods_get-categories.js.html
+++ b/docs/methods_get-categories.js.html
@@ -27,7 +27,7 @@
@@ -78,7 +78,7 @@ methods/get-categories.js
diff --git a/docs/methods_get-items.js.html b/docs/methods_get-items.js.html
index 9cd623c..da38854 100644
--- a/docs/methods_get-items.js.html
+++ b/docs/methods_get-items.js.html
@@ -27,7 +27,7 @@
@@ -89,7 +89,7 @@ methods/get-items.js
diff --git a/docs/methods_get-price-range.js.html b/docs/methods_get-price-range.js.html
index 5143354..7884989 100644
--- a/docs/methods_get-price-range.js.html
+++ b/docs/methods_get-price-range.js.html
@@ -27,7 +27,7 @@
@@ -93,7 +93,7 @@ methods/get-price-range.js
diff --git a/docs/methods_get-specs.js.html b/docs/methods_get-specs.js.html
index 9f63d63..8565eee 100644
--- a/docs/methods_get-specs.js.html
+++ b/docs/methods_get-specs.js.html
@@ -27,7 +27,7 @@
@@ -103,7 +103,7 @@ methods/get-specs.js
diff --git a/docs/methods_get-term-suggestions.js.html b/docs/methods_get-term-suggestions.js.html
index 32cb35c..edf21c8 100644
--- a/docs/methods_get-term-suggestions.js.html
+++ b/docs/methods_get-term-suggestions.js.html
@@ -27,7 +27,7 @@
@@ -109,7 +109,7 @@ methods/get-term-suggestions.js
diff --git a/docs/methods_get-total-count.js.html b/docs/methods_get-total-count.js.html
index 44e858f..77d3277 100644
--- a/docs/methods_get-total-count.js.html
+++ b/docs/methods_get-total-count.js.html
@@ -27,7 +27,7 @@
@@ -91,7 +91,7 @@ methods/get-total-count.js
diff --git a/docs/methods_merge-filter.js.html b/docs/methods_merge-filter.js.html
index 6cbd635..aba5267 100644
--- a/docs/methods_merge-filter.js.html
+++ b/docs/methods_merge-filter.js.html
@@ -27,7 +27,7 @@
@@ -154,7 +154,7 @@ methods/merge-filter.js
diff --git a/docs/methods_remove-filter.js.html b/docs/methods_remove-filter.js.html
index 6fc273a..759a736 100644
--- a/docs/methods_remove-filter.js.html
+++ b/docs/methods_remove-filter.js.html
@@ -27,7 +27,7 @@
@@ -96,7 +96,7 @@ methods/remove-filter.js
diff --git a/docs/methods_reset.js.html b/docs/methods_reset.js.html
index 9837a1e..bdb6915 100644
--- a/docs/methods_reset.js.html
+++ b/docs/methods_reset.js.html
@@ -27,7 +27,7 @@
@@ -84,7 +84,7 @@ methods/reset.js
diff --git a/docs/methods_set-brand-ids.js.html b/docs/methods_set-brand-ids.js.html
index bd9bac3..96f80f5 100644
--- a/docs/methods_set-brand-ids.js.html
+++ b/docs/methods_set-brand-ids.js.html
@@ -27,7 +27,7 @@
@@ -81,7 +81,7 @@ methods/set-brand-ids.js
diff --git a/docs/methods_set-brand-names.js.html b/docs/methods_set-brand-names.js.html
index f29a5ed..a787a63 100644
--- a/docs/methods_set-brand-names.js.html
+++ b/docs/methods_set-brand-names.js.html
@@ -27,7 +27,7 @@
@@ -81,7 +81,7 @@ methods/set-brand-names.js
diff --git a/docs/methods_set-category-ids.js.html b/docs/methods_set-category-ids.js.html
index 8327683..51088ef 100644
--- a/docs/methods_set-category-ids.js.html
+++ b/docs/methods_set-category-ids.js.html
@@ -27,7 +27,7 @@
@@ -81,7 +81,7 @@ methods/set-category-ids.js
diff --git a/docs/methods_set-category-names.js.html b/docs/methods_set-category-names.js.html
index 685f918..6e2b4f8 100644
--- a/docs/methods_set-category-names.js.html
+++ b/docs/methods_set-category-names.js.html
@@ -27,7 +27,7 @@
@@ -81,7 +81,7 @@ methods/set-category-names.js
diff --git a/docs/methods_set-page-number.js.html b/docs/methods_set-page-number.js.html
index 9947b1a..b948363 100644
--- a/docs/methods_set-page-number.js.html
+++ b/docs/methods_set-page-number.js.html
@@ -27,7 +27,7 @@
@@ -82,7 +82,7 @@ methods/set-page-number.js
diff --git a/docs/methods_set-page-size.js.html b/docs/methods_set-page-size.js.html
index 63e4f60..92fde21 100644
--- a/docs/methods_set-page-size.js.html
+++ b/docs/methods_set-page-size.js.html
@@ -27,7 +27,7 @@
@@ -81,7 +81,7 @@ methods/set-page-size.js
diff --git a/docs/methods_set-price-range.js.html b/docs/methods_set-price-range.js.html
index beb0b87..963ac84 100644
--- a/docs/methods_set-price-range.js.html
+++ b/docs/methods_set-price-range.js.html
@@ -27,7 +27,7 @@
@@ -95,7 +95,7 @@ methods/set-price-range.js
diff --git a/docs/methods_set-product-ids.js.html b/docs/methods_set-product-ids.js.html
index 3befaa2..d6e54d8 100644
--- a/docs/methods_set-product-ids.js.html
+++ b/docs/methods_set-product-ids.js.html
@@ -27,7 +27,7 @@
@@ -80,7 +80,7 @@ methods/set-product-ids.js
diff --git a/docs/methods_set-search-term.js.html b/docs/methods_set-search-term.js.html
index a006bf2..fbf3a3b 100644
--- a/docs/methods_set-search-term.js.html
+++ b/docs/methods_set-search-term.js.html
@@ -27,7 +27,7 @@
@@ -108,7 +108,7 @@ methods/set-search-term.js
diff --git a/docs/methods_set-skus.js.html b/docs/methods_set-skus.js.html
index fa4311f..a572f1f 100644
--- a/docs/methods_set-skus.js.html
+++ b/docs/methods_set-skus.js.html
@@ -27,7 +27,7 @@
@@ -80,7 +80,7 @@ methods/set-skus.js
diff --git a/docs/methods_set-sort-order.js.html b/docs/methods_set-sort-order.js.html
index f19898d..9f378d0 100644
--- a/docs/methods_set-sort-order.js.html
+++ b/docs/methods_set-sort-order.js.html
@@ -27,7 +27,7 @@
@@ -44,26 +44,16 @@ methods/set-sort-order.js
import query from './../lib/dsl'
-const inStockSort = {
- _script: {
- type: 'number',
- script: {
- lang: 'painless',
- source: "doc['quantity'].value > 0 ? 1 : 0"
- },
- order: 'desc'
- }
-}
-
export default (self, enumOrder) => {
// get default sort array
const sort = query.sort.slice()
+ let timestamp
// defines most common sorting options
switch (enumOrder) {
case 'sales':
// sort by sales after relevance
- sort.splice(2, 0, inStockSort, {
+ sort.splice(2, 0, {
sales: {
order: 'desc'
}
@@ -72,18 +62,17 @@ methods/set-sort-order.js
case 'news':
// sort by hex ID after relevance replacing score sort
- sort[sort.length - 1] = inStockSort
- sort.push({
+ sort[sort.length - 1] = {
_id: {
order: 'desc'
}
- })
+ }
break
case 'lowest_price':
case 'highest_price':
// sort by price after stock and before relevance
- sort.splice(1, 0, inStockSort, {
+ sort.splice(1, 0, {
price: {
order: enumOrder === 'lowest_price' ? 'asc' : 'desc'
}
@@ -92,12 +81,17 @@ methods/set-sort-order.js
case 'offers':
// sort by percentage offer price
+ timestamp = Date.now()
sort.splice(1, 0, {
_script: {
type: 'number',
script: {
lang: 'painless',
- source: "doc['quantity'].value > 0 && doc['price'].value > 0 && doc['base_price'].value > 0" +
+ source: "doc['price'].value > 0 && doc['base_price'].value > 0" +
+ " && (doc['price_effective_date.start'].empty || " +
+ `doc['price_effective_date.start'].date.millis <= ${timestamp}L)` +
+ " && (doc['price_effective_date.end'].empty || " +
+ `doc['price_effective_date.end'].date.millis >= ${timestamp}L)` +
" ? doc['base_price'].value / doc['price'].value : 0"
},
order: 'desc'
@@ -115,13 +109,11 @@ methods/set-sort-order.js
order: 'desc'
}
})
- // sort by stock right after relevance
- sort.splice(2, 0, inStockSort)
break
default:
// alphabetical order by 'slug', 'sku' or any
- sort.splice(1, 0, inStockSort, {
+ sort.splice(1, 0, {
[enumOrder]: {
order: 'asc'
}
@@ -169,7 +161,7 @@ methods/set-sort-order.js
diff --git a/docs/methods_set-spec.js.html b/docs/methods_set-spec.js.html
index 7a3ae46..4a34336 100644
--- a/docs/methods_set-spec.js.html
+++ b/docs/methods_set-spec.js.html
@@ -27,7 +27,7 @@
@@ -130,7 +130,7 @@ methods/set-spec.js
diff --git a/docs/modifiers_add-global-filter.js.html b/docs/modifiers_add-global-filter.js.html
new file mode 100644
index 0000000..9220ef2
--- /dev/null
+++ b/docs/modifiers_add-global-filter.js.html
@@ -0,0 +1,103 @@
+
+
+
+
+
+ modifiers/add-global-filter.js - Documentation
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ modifiers/add-global-filter.js
+
+
+
+
+
+
+
+
+
+ import * as merge from 'lodash.merge'
+import dslMiddlewares from './../lib/dsl-middlewares'
+
+const addGlobalFilter = (field, value) => {
+ dslMiddlewares.push(dsl => {
+ merge(dsl, {
+ query: {
+ bool: {
+ filter: [{
+ term: {
+ [field]: value
+ }
+ }]
+ }
+ }
+ })
+ })
+}
+
+/**
+ * @method
+ * @name addGlobalFilter
+ * @description Add custom fetch DSL middleware with simple term filter for all instances.
+ * @param {string} field - Item field to filter with dot notation
+ * @param {string|number|boolean} value - Searched field value
+ *
+ * @example
+ * import { addGlobalFilter } from '@ecomplus/search-engine'
+ * addGlobalFilter('channel_id', 123)
+ */
+
+export default addGlobalFilter
+
+export { addGlobalFilter, dslMiddlewares }
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/docs/modifiers_set-sales-channel.js.html b/docs/modifiers_set-sales-channel.js.html
new file mode 100644
index 0000000..0b2cfa7
--- /dev/null
+++ b/docs/modifiers_set-sales-channel.js.html
@@ -0,0 +1,83 @@
+
+
+
+
+
+ modifiers/set-sales-channel.js - Documentation
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ modifiers/set-sales-channel.js
+
+
+
+
+
+
+
+
+
+ import addGlobalFilter from './add-global-filter'
+
+export default channelId => addGlobalFilter('channel_id', channelId)
+
+/**
+ * @method
+ * @name setSalesChannel
+ * @description Set channel ID for searches in all instances.
+ * @param {number} channelId - Sales channel ID
+ *
+ * @example
+ * import { setSalesChannel } from '@ecomplus/search-engine'
+ * setSalesChannel(123)
+ */
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/docs/modifiers_set-warehouse.js.html b/docs/modifiers_set-warehouse.js.html
new file mode 100644
index 0000000..653a8b1
--- /dev/null
+++ b/docs/modifiers_set-warehouse.js.html
@@ -0,0 +1,102 @@
+
+
+
+
+
+ modifiers/set-warehouse.js - Documentation
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ modifiers/set-warehouse.js
+
+
+
+
+
+
+
+
+
+ import dslMiddlewares from './../lib/dsl-middlewares'
+
+export default warehouseCode => {
+ dslMiddlewares.push(dsl => {
+ if (!dsl.sort) {
+ dsl.sort = []
+ }
+ dsl.sort.splice(1, 0, {
+ 'warehouses.in_stock': {
+ order: 'desc',
+ nested: {
+ path: 'warehouses',
+ filter: {
+ term: {
+ 'warehouses.code': warehouseCode
+ }
+ }
+ }
+ }
+ })
+ })
+}
+
+/**
+ * @method
+ * @name setWarehouse
+ * @description Set warehouse (multi CD) code for searches in all instances.
+ * @param {string} warehouseCode - Warehouse code
+ *
+ * @example
+ * import { setWarehouse } from '@ecomplus/search-engine'
+ * setWarehouse('MG')
+ */
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/docs/module-@ecomplus_search-engine.html b/docs/module-@ecomplus_search-engine.html
index 6a9acb4..114d2f8 100644
--- a/docs/module-@ecomplus_search-engine.html
+++ b/docs/module-@ecomplus_search-engine.html
@@ -27,7 +27,7 @@
@@ -70,7 +70,7 @@ @ecomplus/search-engine
Source:
-
- index.js, line 7
+ constructor.js, line 1
@@ -213,7 +213,7 @@ Returns:
new EcomSea
Source:
@@ -332,7 +332,7 @@ dslSource:
-
- index.js, line 93
+ constructor.js, line 87
@@ -411,7 +411,7 @@ historySource:
-
- index.js, line 86
+ constructor.js, line 80
@@ -484,7 +484,7 @@ localStor
Source:
-
- index.js, line 79
+ constructor.js, line 73
@@ -557,7 +557,7 @@ resultSource:
@@ -634,7 +634,7 @@ storageKey<
Source:
-
- index.js, line 71
+ constructor.js, line 65
@@ -711,7 +711,7 @@ storeIdSource:
-
- index.js, line 64
+ constructor.js, line 58
@@ -794,7 +794,7 @@ fetchSource:
@@ -4624,7 +4624,7 @@ setSortOr
Source:
@@ -5026,7 +5026,7 @@ Returns:
diff --git a/docs/index.js.html b/docs/constructor.js.html
similarity index 95%
rename from docs/index.js.html
rename to docs/constructor.js.html
index 81f8452..0e8db27 100644
--- a/docs/index.js.html
+++ b/docs/constructor.js.html
@@ -3,7 +3,7 @@
- index.js - Documentation
+ constructor.js - Documentation
@@ -27,12 +27,12 @@
- index.js
+ constructor.js
@@ -42,13 +42,7 @@ index.js
-
- - index.js, line 93 + constructor.js, line 87
historySource:
-
- index.js, line 86
+ constructor.js, line 80
@@ -484,7 +484,7 @@ localStor
Source:
-
- index.js, line 79
+ constructor.js, line 73
@@ -557,7 +557,7 @@ resultSource:
@@ -634,7 +634,7 @@ storageKey<
Source:
-
- index.js, line 71
+ constructor.js, line 65
@@ -711,7 +711,7 @@ storeIdSource:
-
- index.js, line 64
+ constructor.js, line 58
@@ -794,7 +794,7 @@ fetchSource:
@@ -4624,7 +4624,7 @@ setSortOr
Source:
@@ -5026,7 +5026,7 @@ Returns:
diff --git a/docs/index.js.html b/docs/constructor.js.html
similarity index 95%
rename from docs/index.js.html
rename to docs/constructor.js.html
index 81f8452..0e8db27 100644
--- a/docs/index.js.html
+++ b/docs/constructor.js.html
@@ -3,7 +3,7 @@
- index.js - Documentation
+ constructor.js - Documentation
@@ -27,12 +27,12 @@
- index.js
+ constructor.js
@@ -42,13 +42,7 @@ index.js
-
- - index.js, line 86 + constructor.js, line 80
- - index.js, line 79 + constructor.js, line 73
resultSource:
@@ -634,7 +634,7 @@ storageKey<
Source:
-
- index.js, line 71
+ constructor.js, line 65
@@ -711,7 +711,7 @@ storeIdSource:
-
- index.js, line 64
+ constructor.js, line 58
@@ -794,7 +794,7 @@ fetchSource:
@@ -4624,7 +4624,7 @@ setSortOr
Source:
@@ -5026,7 +5026,7 @@ Returns:
diff --git a/docs/index.js.html b/docs/constructor.js.html
similarity index 95%
rename from docs/index.js.html
rename to docs/constructor.js.html
index 81f8452..0e8db27 100644
--- a/docs/index.js.html
+++ b/docs/constructor.js.html
@@ -3,7 +3,7 @@
- index.js - Documentation
+ constructor.js - Documentation
@@ -27,12 +27,12 @@
- index.js
+ constructor.js
@@ -42,13 +42,7 @@ index.js
-
- - index.js, line 71 + constructor.js, line 65
storeIdSource:
-
- index.js, line 64
+ constructor.js, line 58
@@ -794,7 +794,7 @@ fetchSource:
@@ -4624,7 +4624,7 @@ setSortOr
Source:
@@ -5026,7 +5026,7 @@ Returns:
diff --git a/docs/index.js.html b/docs/constructor.js.html
similarity index 95%
rename from docs/index.js.html
rename to docs/constructor.js.html
index 81f8452..0e8db27 100644
--- a/docs/index.js.html
+++ b/docs/constructor.js.html
@@ -3,7 +3,7 @@
- index.js - Documentation
+ constructor.js - Documentation
@@ -27,12 +27,12 @@
- index.js
+ constructor.js
@@ -42,13 +42,7 @@ index.js
-
- - index.js, line 64 + constructor.js, line 58