Skip to content

Commit

Permalink
search improvement with fuzzy search
Browse files Browse the repository at this point in the history
  • Loading branch information
hkulekci committed Aug 10, 2018
1 parent b2d8f8e commit a2588cf
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions services/productSearchService.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,56 @@ exports.getRecords = function(params, callback) {
}

if (params.keyword) {
body.query.query_string = {
"fields": ["name.autocomplete^10", "description.autocomplete^2", "categories.name.autocomplete^3"],
"query": params.keyword,
"default_operator": "AND"
body.query.bool = {
"must": []
};

var terms = params.keyword.split(' ');
for (var i = 0; i < terms.length; i++) {
if (!terms[i]) {
// ignore empty term
continue;
}
body.query.bool.must.push({
"bool": {
"should": [
{
"fuzzy" : {
"name.autocomplete" : {
"value": terms[i],
"boost": 10.0,
"fuzziness": 1,
"prefix_length": 0,
"max_expansions": 100
}
}
},
{
"fuzzy" : {
"description.autocomplete" : {
"value": terms[i],
"boost": 2.0,
"fuzziness": 1,
"prefix_length": 0,
"max_expansions": 100
}
}
},
{
"fuzzy" : {
"category.name.autocomplete" : {
"value": terms[i],
"boost": 3.0,
"fuzziness": 1,
"prefix_length": 0,
"max_expansions": 100
}
}
}
]
}
});
} // end of for
}

db.search({
Expand Down

0 comments on commit a2588cf

Please sign in to comment.