1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
import { productMappingSolr } from '@/utils/solrMapping'
import axios from 'axios';
import camelcaseObjectDeep from 'camelcase-object-deep';
export default async function handler(req, res) {
const { q = null, op = 'AND' } = req.query
if (!q) {
return res.status(422).json({ error: 'parameter missing' })
}
let parameter = [
`q=${escapeSolrQuery(q)}`,
`q.op=${op}`,
`indent=true`,
`fq=-publish_b:false`,
`qf=name_s^2 description_s`,
`facetch=true`,
`fq=price_tier1_v2_f:[1 TO *]`,
`rows=10`,
`sort=product_rating_f DESC, price_discount_f DESC`,
];
let result = await axios(
process.env.SOLR_HOST + '/solr/product/select?' + parameter.join('&')
);
try {
let { auth } = req.cookies;
if (auth) auth = JSON.parse(auth);
result.data.response.products = productMappingSolr(
result.data.response.docs,
auth?.pricelist || false
);
result.data.responseHeader.params.start = parseInt(
result.data.responseHeader.params.start
);
result.data.responseHeader.params.rows = parseInt(
result.data.responseHeader.params.rows
);
delete result.data.response.docs;
result.data = camelcaseObjectDeep(result.data);
res.status(200).json(result.data);
} catch (error) {
res.status(400).json({ error: error.message });
}
}
const escapeSolrQuery = (query) => {
if (query == '*') return query;
const specialChars = /([\+\-\!\(\)\{\}\[\]\^"~\*\?:\\\/])/g;
const words = query.split(/\s+/);
const escapedWords = words.map((word) => {
if (specialChars.test(word)) {
return `"${word.replace(specialChars, '\\$1')}"`;
}
return word;
});
return escapedWords.join(' ');
};
|