-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from sultur/fuse-search-score
fuse search score with new getTargetObject function
- Loading branch information
Showing
2 changed files
with
111 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,32 @@ | ||
/* | ||
Fuzzy search function that returns an object in the form of {result: (Object), score: (Number)} | ||
* @param {String} query - The search term | ||
* @param {Object} data - The data to search | ||
*/ | ||
function philipsFuzzySearch(query, data) { | ||
// Restructure data to be searchable by name | ||
var newData = Object.keys(data).map(function (key) { | ||
return { ID: key, info: data[key] }; | ||
}); | ||
console.log("new data: ", newData); | ||
// Fuzzy search for the query term (returns an array of objects) | ||
var fuse = new Fuse(newData, { | ||
keys: ["info", "info.name"], | ||
includeScore: true, | ||
shouldSort: true, | ||
threshold: 0.5, | ||
}); | ||
let searchResult = fuse.search(query); | ||
|
||
let searchTerm = query; | ||
let result = fuse.search(searchTerm); | ||
|
||
console.log("result: ", result); | ||
if (result[0] === undefined) { | ||
let resultObject = new Object(); | ||
console.log("result: ", searchResult); | ||
if (searchResult[0] === undefined) { | ||
console.log("no match found"); | ||
return null; | ||
} else { | ||
return result[0].item; | ||
// Structure the return object to be in the form of {result: (Object), score: (Number)} | ||
resultObject.result = searchResult[0].item; | ||
resultObject.score = searchResult[0].score; | ||
console.log("resultObject :", resultObject); | ||
return resultObject; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters