diff options
| author | Mqdd <ahmadmiqdad27@gmail.com> | 2025-12-05 10:50:31 +0700 |
|---|---|---|
| committer | Mqdd <ahmadmiqdad27@gmail.com> | 2025-12-05 10:50:31 +0700 |
| commit | 9ca4e764383ffc3800fbe899dd7e07c297c51e75 (patch) | |
| tree | dcdbae47ccc58d145802a5fb127fcbfc45176a12 /src/pages/api | |
| parent | c10b1479d9a134700f10c3f8c734daf5042626c5 (diff) | |
<Miqdad> fix query
Diffstat (limited to 'src/pages/api')
| -rw-r--r-- | src/pages/api/shop/search.js | 41 |
1 files changed, 41 insertions, 0 deletions
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('&'); |
