diff options
Diffstat (limited to 'src/pages/api')
| -rw-r--r-- | src/pages/api/shop/search.js | 36 | ||||
| -rw-r--r-- | src/pages/api/shop/searchkey.js | 38 |
2 files changed, 74 insertions, 0 deletions
diff --git a/src/pages/api/shop/search.js b/src/pages/api/shop/search.js index 42d16100..d60b9a46 100644 --- a/src/pages/api/shop/search.js +++ b/src/pages/api/shop/search.js @@ -210,6 +210,42 @@ export default async function handler(req, res) { fq.map((val) => `fq=${encodeURIComponent(val)}`) ); + // Searchkey + if (req.query.from === 'searchkey') { + const ids = req.query.ids ? req.query.ids.split(',').filter(Boolean) : []; + + const q = ids.map((id) => `product_id_i:${id}`).join(' OR '); + + const strictQuery = [ + `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=${offset}`, + ]; + + const solrUrl = + process.env.SOLR_HOST + '/solr/product/select?' + strictQuery.join('&'); + + console.log('[SEARCHKEY FINAL 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/api/shop/searchkey.js b/src/pages/api/shop/searchkey.js new file mode 100644 index 00000000..2735e72c --- /dev/null +++ b/src/pages/api/shop/searchkey.js @@ -0,0 +1,38 @@ +import axios from 'axios'; + +export default async function handler(req, res) { + const { url = '', page = 1, limit = 30 } = req.query; + + let q = '*:*'; + + if (!req.query.all) { + const url = (req.query.q || '').trim(); + q = `keywords_s:"${url}"`; + } + let offset = (page - 1) * limit; + + const params = [ + `q.op=AND`, + // `q=keywords_s:"${url}"`, + `q=${q}`, + `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' }); + } +} |
