Skip to content

Commit

Permalink
Merge pull request #248 from hotwax/241_product_keyword_search
Browse files Browse the repository at this point in the history
Improved: product keyword search to search on sku, upc, productName, internalName, productId, groupId, groupName, and productCategoryNames fields (#241).
  • Loading branch information
ravilodhi authored Dec 28, 2023
2 parents f52241b + fd22cc5 commit 2c2e9f7
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"Cycle Count": "Cycle Count",
"eCom Store": "eCom Store",
"Enter a SKU, or use the barcode scanner to search a product": "Enter a SKU, or use the barcode scanner to search a product",
"Enter product sku to search": "Enter product sku to search",
"Enter the product sku, upc, product name, etc. to search.": "Enter the product sku, upc, product name, etc. to search.",
"Enter the amount of stock that has changed.": "Enter the amount of stock that has changed.",
"Enter the count of stock on the shelf.": "Enter the count of stock on the shelf.",
"Enter the stock count for the product": "Enter the stock count for the product",
Expand Down
6 changes: 3 additions & 3 deletions src/services/ProductService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { hasError } from "@/utils";

const fetchProducts = async (query: any): Promise <any> => {
return api({
url: "searchProducts",
method: "get",
params: query,
url: "solr-query",
method: "post",
data: query,
cache: true
});
}
Expand Down
14 changes: 9 additions & 5 deletions src/store/modules/product/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as types from './mutation-types'
import { hasError, showToast } from '@/utils'
import { translate } from '@/i18n'
import emitter from '@/event-bus'
import { prepareProductQuery } from '@/utils/solrHelper'


const actions: ActionTree<ProductState, RootState> = {
Expand All @@ -19,11 +20,14 @@ const actions: ActionTree<ProductState, RootState> = {
let resp;

try {
resp = await ProductService.fetchProducts({
"filters": [`sku: ${payload.queryString} OR upc: ${payload.queryString}`,'isVirtual: false'],
"viewSize": payload.viewSize,
"viewIndex": payload.viewIndex
})
const params = {
...payload,
filters: {
isVirtual: { value: 'false' },
}
}
const productQueryPayload = prepareProductQuery(params)
resp = await ProductService.fetchProducts(productQueryPayload)

// resp.data.response.numFound tells the number of items in the response
if (resp.status === 200 && resp.data.response?.numFound > 0 && !hasError(resp)) {
Expand Down
45 changes: 45 additions & 0 deletions src/utils/solrHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const prepareProductQuery = (params: any) => {
const viewSize = params.viewSize ? params.viewSize : process.env.VUE_APP_VIEW_SIZE;
const viewIndex = params.viewIndex ? params.viewIndex : 0;

const payload = {
"json": {
"params": {
"rows": viewSize,
"q.op": "AND",
"start": viewIndex * viewSize
},
"query": "(*:*)",
"filter": [`docType: ${params.docType ? params.docType : 'PRODUCT'}`]
}
} as any

if (params.queryString) {
payload.json.query = `*${params.queryString}* OR "${params.queryString}"^100`
payload.json.params['qf'] = params.queryFields ? params.queryFields : "sku^100 upc^100 productName^50 internalName^40 productId groupId groupName productCategoryNames"
payload.json.params['defType'] = "edismax"
}

// checking that if the params has filters, and then adding the filter values in the payload filter
// for each key present in the params filters
if (params.filters) {
Object.keys(params.filters).map((key: any) => {
const filterValue = params.filters[key].value;

if (Array.isArray(filterValue)) {
const filterOperator = params.filters[key].op ? params.filters[key].op : 'OR';
payload.json.filter += ` AND ${key}: (${filterValue.join(' ' + filterOperator + ' ')})`
} else {
payload.json.filter += ` AND ${key}: ${filterValue}`
}
})
}

if (params.facet) {
payload.json['facet'] = params.facet
}

return payload
}

export { prepareProductQuery }
5 changes: 2 additions & 3 deletions src/views/Search.vue
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ export default defineComponent({
this.fetchingProducts = true;
const viewSize = vSize ? vSize : process.env.VUE_APP_VIEW_SIZE;
const viewIndex = vIndex ? vIndex : 0;
const queryString = '*' + this.queryString + '*';
await this.getProducts(viewSize, viewIndex, queryString);
await this.getProducts(viewSize, viewIndex, this.queryString);
this.fetchingProducts = false;
},
async getProducts(vSize?: any, vIndex?: any, queryString?: string) {
Expand All @@ -156,7 +155,7 @@ export default defineComponent({
if (this.queryString) {
await this.store.dispatch("product/findProduct", payload);
} else {
showToast(translate("Enter product sku to search"))
showToast(translate("Enter the product sku, upc, product name, etc. to search."))
}
}
},
Expand Down

0 comments on commit 2c2e9f7

Please sign in to comment.