summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiqdad <ahmadmiqdad27@gmail.com>2025-10-14 09:15:06 +0700
committerMiqdad <ahmadmiqdad27@gmail.com>2025-10-14 09:15:06 +0700
commit88d017f7f325bfcaea7d02081e2ded91b374861a (patch)
tree28c22fb57c0c1b79cf53f38a9f67ae4d940cf80b
parentfdf788bd65061e7003cbdef1f934a72f4382f346 (diff)
<MIqdad> fix ready stock
-rw-r--r--src/pages/api/shop/search.js68
1 files changed, 44 insertions, 24 deletions
diff --git a/src/pages/api/shop/search.js b/src/pages/api/shop/search.js
index 1760be70..6bf10672 100644
--- a/src/pages/api/shop/search.js
+++ b/src/pages/api/shop/search.js
@@ -48,12 +48,31 @@ 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}*`)) // Tambahkan '*' hanya jika panjang kata >= 2
+ .map((term) => (term.length < 2 ? term : `${term}*`))
.join(' ')})`;
const mm =
@@ -70,8 +89,7 @@ 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`)
+ filterQueries.push('stock_total_f:[1 TO *]');
}
if (fq && source != 'similar' && typeof fq != 'string') {
@@ -93,7 +111,7 @@ export default async function handler(req, res) {
let offset = (page - 1) * limit;
let parameter = [
- // === Disjunctive facets: exclude brand & category filters saat hitung facet ===
+ // === Facet disjunctive: exclude filter brand/cat saat hitung facet ===
'facet.field={!ex=brand}manufacture_name_s',
'facet.field={!ex=cat}category_name',
'facet=true',
@@ -124,29 +142,30 @@ export default async function handler(req, res) {
if (auth.feature.onlyReadyStock) stock = true;
}
- 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 (brand)
+ parameter.push(
+ // tag filter brand
+ `fq=${encodeURIComponent(
+ `{!tag=brand}manufacture_name_s:(${brand
+ .split(',')
+ .map((manufacturer) => `"${manufacturer.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(
+ // 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 *}`);
+ if (stock) parameter.push(`fq=stock_total_f:[1 TO *]`);
// Single fq in url params
if (typeof fq === 'string') parameter.push(`fq=${encodeURIComponent(fq)}`);
@@ -155,6 +174,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('&')
);