From 9ca4e764383ffc3800fbe899dd7e07c297c51e75 Mon Sep 17 00:00:00 2001 From: Mqdd Date: Fri, 5 Dec 2025 10:50:31 +0700 Subject: fix query --- src/pages/api/shop/search.js | 41 ++++++++++++++++++++++++++++++++++ src/pages/searchkey/[slug].jsx | 50 +++++++++++++++++++++++++++++------------- 2 files changed, 76 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/pages/api/shop/search.js b/src/pages/api/shop/search.js index 42d16100..fec75fd8 100644 --- a/src/pages/api/shop/search.js +++ b/src/pages/api/shop/search.js @@ -210,6 +210,47 @@ export default async function handler(req, res) { fq.map((val) => `fq=${encodeURIComponent(val)}`) ); + // Searchkey + if (req.query.from === 'searchkey') { + const phrase = q.replace(/-/g, ' ').trim(); + + // encode q + const encodedQuery = encodeURIComponent(`variants_name_t:"${phrase}"`); + + const strictQuery = [ + `q=${encodedQuery}`, + `defType=edismax`, + `q.op=AND`, + `mm=${encodeURIComponent('100%')}`, + `qf=${encodeURIComponent('name_s description_clean_t')}`, + `rows=${limit}`, + `start=${(page - 1) * limit}`, + ]; + + if (fq) strictQuery.push(`fq=${encodeURIComponent(fq)}`); + + const solrUrl = + process.env.SOLR_HOST + '/solr/product/select?' + strictQuery.join('&'); + + console.log('[STRICT SEARCHKEY QUERY]', solrUrl); + + const result = await axios(solrUrl); + + try { + result.data.response.products = productMappingSolr( + result.data.response.docs, + auth?.pricelist || false + ); + + delete result.data.response.docs; + result.data = camelcaseObjectDeep(result.data); + + return res.status(200).json(result.data); + } catch (e) { + return res.status(400).json({ error: e.message }); + } + } + const solrUrl = process.env.SOLR_HOST + '/solr/product/select?' + parameter.join('&'); diff --git a/src/pages/searchkey/[slug].jsx b/src/pages/searchkey/[slug].jsx index f09520f4..9cf1df05 100644 --- a/src/pages/searchkey/[slug].jsx +++ b/src/pages/searchkey/[slug].jsx @@ -47,26 +47,41 @@ export default function KeywordPage() { } }, [router.isReady]); - // Jika Solr index ditemukan, siapkan parameter pencarian useEffect(() => { if (result) { - const fq = `category_parent_ids:${result.category_id_i} AND manufacture_id_i:${result.brand_id_i}`; - const q = keyword || '*:*'; // keyword dari URL + let fqParts = []; + + if (result.category_id_i) { + fqParts.push(`category_parent_ids:${result.category_id_i}`); + } + + if (result.brand_id_i) { + fqParts.push(`manufacture_id_i:${result.brand_id_i}`); + } + + const fq = fqParts.join(' AND '); + const q = keyword || '*:*'; + console.log('SOLR QUERY:', { q, fq }); - setQuery({ fq, q }); + + setQuery({ + fq, + q, + from: 'searchkey', + }); } }, [result, keyword]); - if (!result) { - return ( - - -
-

Produk tidak ditemukan berdasarkan keyword

-
-
- ); - } + // if (!result) { + // return ( + // + // + //
+ //

Produk tidak ditemukan berdasarkan keyword

+ //
+ //
+ // ); + // } return ( @@ -83,7 +98,12 @@ export default function KeywordPage() { router.asPath.split('?')[0] }`} /> - {query && } + {query && ( + + )} ); } -- cgit v1.2.3