From 95b27ddb0604fbb4fae130f2d80e5ee2aec6d0fc Mon Sep 17 00:00:00 2001 From: Mqdd Date: Thu, 11 Dec 2025 09:00:03 +0700 Subject: fix --- src/pages/api/shop/search.js | 19 +++++++------------ src/pages/api/shop/searchkey.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 12 deletions(-) create mode 100644 src/pages/api/shop/searchkey.js (limited to 'src/pages/api/shop') diff --git a/src/pages/api/shop/search.js b/src/pages/api/shop/search.js index fec75fd8..d60b9a46 100644 --- a/src/pages/api/shop/search.js +++ b/src/pages/api/shop/search.js @@ -212,27 +212,22 @@ export default async function handler(req, res) { // Searchkey if (req.query.from === 'searchkey') { - const phrase = q.replace(/-/g, ' ').trim(); + const ids = req.query.ids ? req.query.ids.split(',').filter(Boolean) : []; - // encode q - const encodedQuery = encodeURIComponent(`variants_name_t:"${phrase}"`); + const q = ids.map((id) => `product_id_i:${id}`).join(' OR '); const strictQuery = [ - `q=${encodedQuery}`, - `defType=edismax`, - `q.op=AND`, - `mm=${encodeURIComponent('100%')}`, - `qf=${encodeURIComponent('name_s description_clean_t')}`, + `q=${encodeURIComponent(q)}`, + `fq=-publish_b:false AND price_tier1_v2_f:[1 TO *] AND product_rating_f:[8 TO *]`, + // `qf=variants_code_t variants_name_t`, `rows=${limit}`, - `start=${(page - 1) * limit}`, + `start=${offset}`, ]; - if (fq) strictQuery.push(`fq=${encodeURIComponent(fq)}`); - const solrUrl = process.env.SOLR_HOST + '/solr/product/select?' + strictQuery.join('&'); - console.log('[STRICT SEARCHKEY QUERY]', solrUrl); + console.log('[SEARCHKEY FINAL QUERY]', solrUrl); const result = await axios(solrUrl); diff --git a/src/pages/api/shop/searchkey.js b/src/pages/api/shop/searchkey.js new file mode 100644 index 00000000..e8b535e7 --- /dev/null +++ b/src/pages/api/shop/searchkey.js @@ -0,0 +1,31 @@ +import axios from 'axios'; + +export default async function handler(req, res) { + const { url = '', page = 1, limit = 30 } = req.query; + + let offset = (page - 1) * limit; + + const params = [ + `q.op=AND`, + `q=keywords_s:"${url}"`, + `indent=true`, + `rows=${limit}`, + `start=${offset}`, + ]; + + try { + // let result = await axios( + // process.env.SOLR_HOST + `/solr/searchkey/select?` + params.join('&') + // ); + let result = await axios.post( + process.env.SOLR_HOST + `/solr/searchkey/select`, + params.join('&'), + { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } } + ); + + console.log(result.data); + res.status(200).json(result.data); + } catch (error) { + res.status(500).json({ error: 'Internal Server Error' }); + } +} -- cgit v1.2.3