From 2d5dcd6e810825950b4fc564135314fc466250fa Mon Sep 17 00:00:00 2001 From: Miqdad Date: Sat, 30 Aug 2025 09:52:41 +0700 Subject: Done apply filter without button --- src/pages/api/shop/search.js | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) (limited to 'src/pages/api') diff --git a/src/pages/api/shop/search.js b/src/pages/api/shop/search.js index 3d258a97..8412c8f6 100644 --- a/src/pages/api/shop/search.js +++ b/src/pages/api/shop/search.js @@ -19,7 +19,6 @@ export default async function handler(req, res) { source = '', } = req.query; - let { stock = '' } = req.query; let paramOrderBy = ''; switch (orderBy) { @@ -73,7 +72,7 @@ export default async function handler(req, res) { const formattedQuery = `(${newQ .split(' ') - .map((term) => (term.length < 2 ? term : `${term}*`)) // Tambahkan '*' hanya jika panjang kata >= 2 + .map((term) => (term.length < 2 ? term : `${term}*`)) .join(' ')})`; const mm = @@ -90,8 +89,8 @@ export default async function handler(req, res) { ]; if (orderBy === 'stock') { - filterQueries.push('stock_total_f:{1 TO *}&sort=stock_total_f desc'); - // filterQueries.push(`stock_total_f DESC`) + // JANGAN selipkan &sort di sini; sorting sudah di paramOrderBy + filterQueries.push('stock_total_f:{1 TO *}'); } if (fq && source != 'similar' && typeof fq != 'string') { @@ -113,8 +112,9 @@ export default async function handler(req, res) { let offset = (page - 1) * limit; let parameter = [ - 'facet.field=manufacture_name_s', - 'facet.field=category_name', + // === Facet disjunctive: exclude filter brand/cat saat hitung facet === + 'facet.field={!ex=brand}manufacture_name_s', + 'facet.field={!ex=cat}category_name', 'facet=true', 'indent=true', `facet.query=${escapeSolrQuery(q)}`, @@ -145,21 +145,26 @@ export default async function handler(req, res) { if (brand) parameter.push( - `fq=${brand - .split(',') - .map( - (manufacturer) => - `manufacture_name:"${encodeURIComponent(manufacturer)}"` - ) - .join(' OR ')}` + // tag filter brand + `fq=${encodeURIComponent( + `{!tag=brand}manufacture_name_s:(${brand + .split(',') + .map((manufacturer) => `"${manufacturer.replace(/"/g, '\\"')}"`) + .join(' OR ')})` + )}` ); + if (category) parameter.push( - `fq=${category - .split(',') - .map((cat) => `category_name:"${encodeURIComponent(cat)}"`) - .join(' OR ')}` + // tag filter category + `fq=${encodeURIComponent( + `{!tag=cat}category_name:(${category + .split(',') + .map((cat) => `"${cat.replace(/"/g, '\\"')}"`) + .join(' OR ')})` + )}` ); + // if (category) parameter.push(`fq=category_name:${capitalizeFirstLetter(category.replace(/,/g, ' OR '))}`) if (stock) parameter.push(`fq=stock_total_f:{1 TO *}`); @@ -170,6 +175,7 @@ export default async function handler(req, res) { parameter = parameter.concat( fq.map((val) => `fq=${encodeURIComponent(val)}`) ); + let result = await axios( process.env.SOLR_HOST + '/solr/product/select?' + parameter.join('&') ); -- cgit v1.2.3 From 4b749c672a7a9b93ee65198f8bec703909711e84 Mon Sep 17 00:00:00 2001 From: Miqdad Date: Sat, 30 Aug 2025 10:10:16 +0700 Subject: Fix --- src/pages/api/shop/search.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/pages/api') diff --git a/src/pages/api/shop/search.js b/src/pages/api/shop/search.js index 8412c8f6..a8ce4221 100644 --- a/src/pages/api/shop/search.js +++ b/src/pages/api/shop/search.js @@ -166,7 +166,7 @@ export default async function handler(req, res) { ); // if (category) parameter.push(`fq=category_name:${capitalizeFirstLetter(category.replace(/,/g, ' OR '))}`) - if (stock) parameter.push(`fq=stock_total_f:{1 TO *}`); + if (stock) parameter.push(`fq=stock_total_f:{1 TO *]`); // Single fq in url params if (typeof fq === 'string') parameter.push(`fq=${encodeURIComponent(fq)}`); -- cgit v1.2.3 From 7735e705142e9a56f37c90b09ea5e6ba80d2bfa3 Mon Sep 17 00:00:00 2001 From: Miqdad Date: Sat, 30 Aug 2025 13:53:33 +0700 Subject: push --- src/pages/api/shop/search.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/pages/api') diff --git a/src/pages/api/shop/search.js b/src/pages/api/shop/search.js index a8ce4221..81cc22c5 100644 --- a/src/pages/api/shop/search.js +++ b/src/pages/api/shop/search.js @@ -89,8 +89,7 @@ export default async function handler(req, res) { ]; if (orderBy === 'stock') { - // JANGAN selipkan &sort di sini; sorting sudah di paramOrderBy - filterQueries.push('stock_total_f:{1 TO *}'); + filterQueries.push('stock_total_f:[1 TO *]'); } if (fq && source != 'similar' && typeof fq != 'string') { -- cgit v1.2.3 From 932b92af3f3a1a0a62bef60d647c00e1be483265 Mon Sep 17 00:00:00 2001 From: Miqdad Date: Mon, 1 Sep 2025 12:05:04 +0700 Subject: Fix Search --- src/pages/api/shop/search.js | 68 ++++++++++++++++---------------------------- 1 file changed, 24 insertions(+), 44 deletions(-) (limited to 'src/pages/api') diff --git a/src/pages/api/shop/search.js b/src/pages/api/shop/search.js index 81cc22c5..1760be70 100644 --- a/src/pages/api/shop/search.js +++ b/src/pages/api/shop/search.js @@ -48,31 +48,12 @@ export default async function handler(req, res) { break; } - // let suggestWord = null; - // let keywords = q; - // let checkQ = null; - - // if (q != '*') { - // checkQ = q.trim().split(/[\s\+\-\!\(\)\{\}\[\]\^"~\*\?:\\\/]+/); - // if (checkQ.length > 1) { - // const dataSearchSuggest = await axios( - // `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/suggest?q=${checkQ[1]}` - // ); - // suggestWord = dataSearchSuggest.data.suggestions[0]; - // } - // if (suggestWord && suggestWord?.term.split(' ').length <= 1) { - // keywords = `"${escapeSolrQuery(checkQ[0] + ' ' + suggestWord?.term)}"`; - // } - // } - - // let newQ = keywords; - let checkQ = q.trim().split(/[\s\+\-\!\(\)\{\}\[\]\^"~\*\?:\\\/]+/); let newQ = escapeSolrQuery(q); const formattedQuery = `(${newQ .split(' ') - .map((term) => (term.length < 2 ? term : `${term}*`)) + .map((term) => (term.length < 2 ? term : `${term}*`)) // Tambahkan '*' hanya jika panjang kata >= 2 .join(' ')})`; const mm = @@ -89,7 +70,8 @@ export default async function handler(req, res) { ]; if (orderBy === 'stock') { - filterQueries.push('stock_total_f:[1 TO *]'); + filterQueries.push('stock_total_f:{1 TO *}&sort=stock_total_f desc'); + // filterQueries.push(`stock_total_f DESC`) } if (fq && source != 'similar' && typeof fq != 'string') { @@ -111,7 +93,7 @@ export default async function handler(req, res) { let offset = (page - 1) * limit; let parameter = [ - // === Facet disjunctive: exclude filter brand/cat saat hitung facet === + // === Disjunctive facets: exclude brand & category filters saat hitung facet === 'facet.field={!ex=brand}manufacture_name_s', 'facet.field={!ex=cat}category_name', 'facet=true', @@ -142,30 +124,29 @@ export default async function handler(req, res) { if (auth.feature.onlyReadyStock) stock = true; } - if (brand) - parameter.push( - // tag filter brand - `fq=${encodeURIComponent( - `{!tag=brand}manufacture_name_s:(${brand - .split(',') - .map((manufacturer) => `"${manufacturer.replace(/"/g, '\\"')}"`) - .join(' OR ')})` - )}` - ); + if (brand) { + // bentuk ekspresi sama seperti versi kamu, tapi dibungkus tag brand + const brandExpr = brand + .split(',') + .map( + (manufacturer) => + `manufacture_name:"${encodeURIComponent(manufacturer)}"` + ) + .join(' OR '); + parameter.push(`fq={!tag=brand}(${brandExpr})`); + } - if (category) - parameter.push( - // tag filter category - `fq=${encodeURIComponent( - `{!tag=cat}category_name:(${category - .split(',') - .map((cat) => `"${cat.replace(/"/g, '\\"')}"`) - .join(' OR ')})` - )}` - ); + if (category) { + // sama: tag kategori + const catExpr = category + .split(',') + .map((cat) => `category_name:"${encodeURIComponent(cat)}"`) + .join(' OR '); + parameter.push(`fq={!tag=cat}(${catExpr})`); + } // if (category) parameter.push(`fq=category_name:${capitalizeFirstLetter(category.replace(/,/g, ' OR '))}`) - if (stock) parameter.push(`fq=stock_total_f:{1 TO *]`); + if (stock) parameter.push(`fq=stock_total_f:{1 TO *}`); // Single fq in url params if (typeof fq === 'string') parameter.push(`fq=${encodeURIComponent(fq)}`); @@ -174,7 +155,6 @@ export default async function handler(req, res) { parameter = parameter.concat( fq.map((val) => `fq=${encodeURIComponent(val)}`) ); - let result = await axios( process.env.SOLR_HOST + '/solr/product/select?' + parameter.join('&') ); -- cgit v1.2.3