Skip to content

Commit a2588cf

Browse files
committed
search improvement with fuzzy search
1 parent b2d8f8e commit a2588cf

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

services/productSearchService.js

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,56 @@ exports.getRecords = function(params, callback) {
1515
}
1616

1717
if (params.keyword) {
18-
body.query.query_string = {
19-
"fields": ["name.autocomplete^10", "description.autocomplete^2", "categories.name.autocomplete^3"],
20-
"query": params.keyword,
21-
"default_operator": "AND"
18+
body.query.bool = {
19+
"must": []
2220
};
21+
22+
var terms = params.keyword.split(' ');
23+
for (var i = 0; i < terms.length; i++) {
24+
if (!terms[i]) {
25+
// ignore empty term
26+
continue;
27+
}
28+
body.query.bool.must.push({
29+
"bool": {
30+
"should": [
31+
{
32+
"fuzzy" : {
33+
"name.autocomplete" : {
34+
"value": terms[i],
35+
"boost": 10.0,
36+
"fuzziness": 1,
37+
"prefix_length": 0,
38+
"max_expansions": 100
39+
}
40+
}
41+
},
42+
{
43+
"fuzzy" : {
44+
"description.autocomplete" : {
45+
"value": terms[i],
46+
"boost": 2.0,
47+
"fuzziness": 1,
48+
"prefix_length": 0,
49+
"max_expansions": 100
50+
}
51+
}
52+
},
53+
{
54+
"fuzzy" : {
55+
"category.name.autocomplete" : {
56+
"value": terms[i],
57+
"boost": 3.0,
58+
"fuzziness": 1,
59+
"prefix_length": 0,
60+
"max_expansions": 100
61+
}
62+
}
63+
}
64+
]
65+
}
66+
});
67+
} // end of for
2368
}
2469

2570
db.search({

0 commit comments

Comments
 (0)