Skip to content

Commit

Permalink
enhance: search case insensitive, and search tool descriptions (#14)
Browse files Browse the repository at this point in the history
Signed-off-by: Grant Linville <[email protected]>
  • Loading branch information
g-linville authored Aug 14, 2024
1 parent 72dff8c commit e4692ca
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 11 deletions.
10 changes: 6 additions & 4 deletions indexer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ func insertSystemTools(db *sql.DB) {
}

_, err = db.Exec(`
INSERT INTO public."ToolEntry" (reference, content, "systemTool", "lastIndexedAt")
VALUES ($1, $2, $3, NOW())
INSERT INTO public."ToolEntry" (reference, content, "systemTool", description, "lastIndexedAt")
VALUES ($1, $2, $3, $4, NOW())
ON CONFLICT (reference) DO UPDATE
SET "content" = $2, "systemTool" = $3, "lastIndexedAt" = NOW()`,
SET "content" = $2, "systemTool" = $3, "description" = $4, "lastIndexedAt" = NOW()`,
tool.Name,
string(toolAsJSON),
true,
tool.Description,
)

if err != nil {
Expand Down Expand Up @@ -93,12 +94,13 @@ func reindexRemoteTools(db *sql.DB, apiURL *url.URL) {
index string
reference string
content string
description string
lastIndexedAt string
createdAt string
systemTool bool
)

err = rows.Scan(&index, &createdAt, &lastIndexedAt, &content, &reference, &systemTool)
err = rows.Scan(&index, &createdAt, &lastIndexedAt, &content, &reference, &systemTool, &description)
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
"down": "docker compose down",
"build": "nuxt build",
"dev": "nuxt dev --host",
"dev:migrate": "npx prisma migrate deploy && yarn dev --host",
"dev:migrate": "npx prisma generate && npx prisma migrate deploy && yarn dev --host",
"lint": "eslint --max-warnings 0 .",
"generate": "nuxt generate",
"preview": "nuxt preview",
"postinstall": "nuxt prepare",
"start": "nuxt start",
"start:migrate": "npx prisma migrate deploy && yarn start",
"start:migrate": "npx prisma generate && npx prisma migrate deploy && yarn start",
"go:tidy": "(cd indexer && go mod tidy) && (cd parser && go mod tidy)",
"go:build": "(cd indexer && go build) && (cd parser && go build)"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- AlterTable
ALTER TABLE "ToolEntry" ADD COLUMN "description" TEXT;

UPDATE "ToolEntry" SET description = content->0->>'description' WHERE content->0->>'description' IS NOT NULL;
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ model ToolEntry {
createdAt DateTime @default(now())
lastIndexedAt DateTime @updatedAt
content Json
description String?
reference String @unique
examples Example[]
systemTool Boolean @default(false)
Expand Down
66 changes: 61 additions & 5 deletions src/lib/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,76 @@ export async function removeToolForUrlIfExists(url: string): Promise<Tool[]> {

export async function getToolsForQuery(query: string, page: number, pageSize: number): Promise<{ tools: Record<string, Tool[]>, totalCount: number }> {
const skip = (page - 1) * pageSize
const toolEntries = await prisma.toolEntry.findMany({
where: { reference: { contains: query } },
take: page != all ? pageSize : undefined,

// First get the tools whose name (GitHub reference) contains the query
const toolEntriesWithReference = await prisma.toolEntry.findMany({
where: {
reference: {
contains: query,
mode: 'insensitive'
}
},
take: page != all ? pageSize : undefined,
skip: skip > 0 && page != all ? skip : undefined,
})

// Next, get the tools whose description contains the query
const toolEntriesWithDescription = await prisma.toolEntry.findMany({
where: {
AND: [
{
description: {
contains: query,
mode: 'insensitive'
}
},
{
NOT: {
reference: {
contains: query,
mode: 'insensitive'
}
}
}
]
},
take: page != all ? pageSize : undefined,
skip: skip > 0 && page != all ? skip : undefined,
})

const tools: Record<string, Tool[]> = {}

for (const entry of toolEntries) {
// Add them to the results so that the ones with the query in the reference come first
for (const entry of toolEntriesWithReference) {
const parsedTool = entry.content as Tool[]
tools[entry.reference] = tools[entry.reference] || []
tools[entry.reference].push(...parsedTool)
}

const totalCount = await prisma.toolEntry.count({ where: { reference: { contains: query } } })
for (const entry of toolEntriesWithDescription) {
const parsedTool = entry.content as Tool[]
tools[entry.reference] = tools[entry.reference] || []
tools[entry.reference].push(...parsedTool)
}

const totalCount = await prisma.toolEntry.count({
where: {
OR: [
{
reference: {
contains: query,
mode: 'insensitive'
}
},
{
description: {
contains: query,
mode: 'insensitive'
}
}
]
}
})

return { tools, totalCount }
}

0 comments on commit e4692ca

Please sign in to comment.