Skip to content

Commit

Permalink
Add fuzzy search
Browse files Browse the repository at this point in the history
  • Loading branch information
amits97 committed Nov 20, 2024
1 parent e1c947f commit 81b065a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 12 deletions.
62 changes: 62 additions & 0 deletions api/common/post-fuzzy-search.js
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;
}
30 changes: 18 additions & 12 deletions api/posts-service/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as userNameLib from "../libs/username-lib";
import * as searchFilterLib from "../libs/searchfilter-lib";
import { appendRatings } from "../common/post-ratings";
import { appendCommentsCount } from "../common/post-comments";
import { fuzzySearch } from "../common/post-fuzzy-search";

export async function main(event, context, callback) {
var lastEvaluatedKey;
Expand Down Expand Up @@ -120,18 +121,23 @@ export async function main(event, context, callback) {
result = await dynamoDbLib.call("scan", params);

if (result?.Items?.length === 0) {
// No search results
const emptySearchWriteParams = {
TableName: "NaadanChordsEmptySearch",
Item: {
timestamp: Date.now(),
searchQuery: event.search,
ipAddress: event.sourceIP,
type: "SEARCH",
},
};

await dynamoDbLib.call("put", emptySearchWriteParams);
// Try fuzzy search over post ids
result = await fuzzySearch(event.search);

if (result?.Items?.length === 0) {
// No search results
const emptySearchWriteParams = {
TableName: "NaadanChordsEmptySearch",
Item: {
timestamp: Date.now(),
searchQuery: event.search,
ipAddress: event.sourceIP,
type: "SEARCH",
},
};

await dynamoDbLib.call("put", emptySearchWriteParams);
}
}
} else {
result = await dynamoDbLib.call("query", params);
Expand Down
14 changes: 14 additions & 0 deletions api/posts-service/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/posts-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"dependencies": {
"aws-sdk": "^2.1359.0",
"fuse.js": "^7.0.0",
"serverless-domain-manager": "^5.1.0",
"serverless-webpack": "^5.5.4",
"webpack-node-externals": "^2.5.2"
Expand Down

0 comments on commit 81b065a

Please sign in to comment.