diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/pages/api/shop/search.js | 19 | ||||
| -rw-r--r-- | src/pages/api/shop/searchkey.js | 31 | ||||
| -rw-r--r-- | src/pages/searchkey/[slug].jsx | 94 |
3 files changed, 68 insertions, 76 deletions
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' }); + } +} diff --git a/src/pages/searchkey/[slug].jsx b/src/pages/searchkey/[slug].jsx index 9cf1df05..3ebf6469 100644 --- a/src/pages/searchkey/[slug].jsx +++ b/src/pages/searchkey/[slug].jsx @@ -3,6 +3,7 @@ import { useRouter } from 'next/router'; import { useEffect, useState } from 'react'; import Seo from '@/core/components/Seo'; import dynamic from 'next/dynamic'; +import { getNameFromSlug } from '@/core/utils/slug'; import { capitalizeEachWord } from '../../utils/capializeFIrstWord'; const BasicLayout = dynamic(() => @@ -12,98 +13,63 @@ const ProductSearch = dynamic(() => import('@/lib/product/components/ProductSearch') ); -// const BASE_URL = process.env.NEXT_PUBLIC_SELF_HOST; -const BASE_URL = 'https://indoteknik.com'; - -export default function KeywordPage() { - const router = useRouter(); +export default function FindPage() { + const route = useRouter(); const [result, setResult] = useState(null); const [query, setQuery] = useState(null); - // Ambil slug dari URL dinamis - const keywordSlug = router?.query?.slug || ''; - const keyword = keywordSlug.replace(/-/g, ' ').toLowerCase(); - const url = BASE_URL + router.asPath.split('?')[0]; - const slugTitle = capitalizeEachWord(keyword); + const slugRaw = route.query.slug || null; + console.log(slugRaw); + + // const cleanKey = slugRaw ? getNameFromSlug(slugRaw) : ''; + // console.log(cleanKey); + const readableSlug = capitalizeEachWord(slugRaw); - // Fetch info dari Solr index "url_category_brand" - const getUrls = async (url) => { + const getSearchKeyData = async (clean) => { try { - const response = await axios( - `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/url-category_brand?url=${url}` + const res = await axios( + `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/searchkey?url=${clean}&from=searchkey` ); - const data = response?.data?.response?.docs[0] || null; - setResult(data); - console.log('[🔍 result from API]', data); // Tambahin ini - } catch (error) { - console.error('Error fetching data:', error); + + setResult(res?.data?.response?.docs?.[0] || null); + } catch (e) { + console.error('Fetching searchkey failed:', e); } }; - // Panggil fetch setelah router siap useEffect(() => { - if (router.isReady) { - getUrls(url); - } - }, [router.isReady]); + if (!route.isReady) return; + if (!slugRaw) return; + + getSearchKeyData(slugRaw); + }, [route.isReady, slugRaw]); useEffect(() => { if (result) { - 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 }); + const ids = result.product_ids_is || []; setQuery({ - fq, - q, + ids: ids.join(','), from: 'searchkey', }); } - }, [result, keyword]); - - // if (!result) { - // return ( - // <BasicLayout> - // <Seo title='Keyword tidak ditemukan' /> - // <div className='container py-5'> - // <h2>Produk tidak ditemukan berdasarkan keyword</h2> - // </div> - // </BasicLayout> - // ); - // } + }, [result]); return ( <BasicLayout> <Seo - title={`Beli ${slugTitle} Original & Harga Terjangkau - indoteknik.com`} - description={`Beli ${slugTitle} Kirim Jakarta Surabaya Semarang Makassar Manado Denpasar Balikpapan Medan Palembang Lampung Bali Bandung Makassar Manado.`} + title={`Beli ${readableSlug} Original & Harga Terjangkau - indoteknik.com`} + description={`Beli ${readableSlug} Kirim Jakarta Surabaya Semarang Makassar Manado Denpasar.`} additionalMetaTags={[ { property: 'keywords', - content: `Beli ${slugTitle}, harga ${slugTitle}, ${slugTitle} murah, toko ${slugTitle}, ${slugTitle} jakarta, ${slugTitle} surabaya`, + content: `Beli ${readableSlug}, harga ${readableSlug}, ${readableSlug} murah`, }, ]} - canonical={`${process.env.NEXT_PUBLIC_SELF_HOST}${ - router.asPath.split('?')[0] - }`} + canonical={`${process.env.NEXT_PUBLIC_SELF_HOST}${route.asPath}`} /> - {query && ( - <ProductSearch - query={{ ...query, from: 'searchkey' }} - prefixUrl={router.asPath} - /> - )} + + {query && <ProductSearch query={query} prefixUrl={route.asPath} />} </BasicLayout> ); } |
