-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
95 additions
and
12 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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import Fuse from "fuse.js"; | ||
import * as dynamoDbLib from "../libs/dynamodb-lib"; | ||
|
||
async function getPostDetails(postId) { | ||
const params = { | ||
TableName: "NaadanChords", | ||
ProjectionExpression: | ||
"postId, category, createdAt, updatedAt, postType, title, userId", | ||
Key: { | ||
postId, | ||
}, | ||
}; | ||
let result = await dynamoDbLib.call("get", params); | ||
return result.Item; | ||
} | ||
|
||
export async function fuzzySearch(query) { | ||
let allPostIds = []; | ||
let params = { | ||
TableName: "NaadanChords", | ||
IndexName: "postType-updatedAt-index", | ||
KeyConditionExpression: "postType = :postType", | ||
ExpressionAttributeValues: { | ||
":postType": "POST", | ||
}, | ||
ScanIndexForward: false, | ||
ProjectionExpression: "postId", | ||
}; | ||
|
||
let lek = "init"; | ||
while (lek) { | ||
const data = await dynamoDbLib.call("query", params); | ||
allPostIds.push(...data.Items); | ||
lek = data.LastEvaluatedKey; | ||
if (lek) params.ExclusiveStartKey = lek; | ||
} | ||
|
||
const options = { | ||
includeScore: true, | ||
threshold: 0.5, | ||
isCaseSensitive: false, | ||
keys: ["postId"], | ||
}; | ||
|
||
const scanResult = new Fuse(allPostIds, options); | ||
let result = scanResult.search(query, { limit: 15 }); | ||
|
||
if (result.length > 0) { | ||
allPostIds = result.map((item) => item.item.postId); | ||
} else { | ||
return { Items: [] }; | ||
} | ||
|
||
// Get post details | ||
result = { Items: [] }; | ||
for (let i = 0; i < allPostIds.length; i++) { | ||
const res = await getPostDetails(allPostIds[i]); | ||
result.Items.push(res); | ||
} | ||
|
||
return result; | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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