Skip to content

Commit e4692ca

Browse files
authored
enhance: search case insensitive, and search tool descriptions (#14)
Signed-off-by: Grant Linville <[email protected]>
1 parent 72dff8c commit e4692ca

File tree

5 files changed

+74
-11
lines changed

5 files changed

+74
-11
lines changed

indexer/main.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,14 @@ func insertSystemTools(db *sql.DB) {
5959
}
6060

6161
_, err = db.Exec(`
62-
INSERT INTO public."ToolEntry" (reference, content, "systemTool", "lastIndexedAt")
63-
VALUES ($1, $2, $3, NOW())
62+
INSERT INTO public."ToolEntry" (reference, content, "systemTool", description, "lastIndexedAt")
63+
VALUES ($1, $2, $3, $4, NOW())
6464
ON CONFLICT (reference) DO UPDATE
65-
SET "content" = $2, "systemTool" = $3, "lastIndexedAt" = NOW()`,
65+
SET "content" = $2, "systemTool" = $3, "description" = $4, "lastIndexedAt" = NOW()`,
6666
tool.Name,
6767
string(toolAsJSON),
6868
true,
69+
tool.Description,
6970
)
7071

7172
if err != nil {
@@ -93,12 +94,13 @@ func reindexRemoteTools(db *sql.DB, apiURL *url.URL) {
9394
index string
9495
reference string
9596
content string
97+
description string
9698
lastIndexedAt string
9799
createdAt string
98100
systemTool bool
99101
)
100102

101-
err = rows.Scan(&index, &createdAt, &lastIndexedAt, &content, &reference, &systemTool)
103+
err = rows.Scan(&index, &createdAt, &lastIndexedAt, &content, &reference, &systemTool, &description)
102104
if err != nil {
103105
log.Fatal(err)
104106
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
"down": "docker compose down",
88
"build": "nuxt build",
99
"dev": "nuxt dev --host",
10-
"dev:migrate": "npx prisma migrate deploy && yarn dev --host",
10+
"dev:migrate": "npx prisma generate && npx prisma migrate deploy && yarn dev --host",
1111
"lint": "eslint --max-warnings 0 .",
1212
"generate": "nuxt generate",
1313
"preview": "nuxt preview",
1414
"postinstall": "nuxt prepare",
1515
"start": "nuxt start",
16-
"start:migrate": "npx prisma migrate deploy && yarn start",
16+
"start:migrate": "npx prisma generate && npx prisma migrate deploy && yarn start",
1717
"go:tidy": "(cd indexer && go mod tidy) && (cd parser && go mod tidy)",
1818
"go:build": "(cd indexer && go build) && (cd parser && go build)"
1919
},
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- AlterTable
2+
ALTER TABLE "ToolEntry" ADD COLUMN "description" TEXT;
3+
4+
UPDATE "ToolEntry" SET description = content->0->>'description' WHERE content->0->>'description' IS NOT NULL;

prisma/schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ model ToolEntry {
1818
createdAt DateTime @default(now())
1919
lastIndexedAt DateTime @updatedAt
2020
content Json
21+
description String?
2122
reference String @unique
2223
examples Example[]
2324
systemTool Boolean @default(false)

src/lib/db.ts

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,76 @@ export async function removeToolForUrlIfExists(url: string): Promise<Tool[]> {
9090

9191
export async function getToolsForQuery(query: string, page: number, pageSize: number): Promise<{ tools: Record<string, Tool[]>, totalCount: number }> {
9292
const skip = (page - 1) * pageSize
93-
const toolEntries = await prisma.toolEntry.findMany({
94-
where: { reference: { contains: query } },
95-
take: page != all ? pageSize : undefined,
93+
94+
// First get the tools whose name (GitHub reference) contains the query
95+
const toolEntriesWithReference = await prisma.toolEntry.findMany({
96+
where: {
97+
reference: {
98+
contains: query,
99+
mode: 'insensitive'
100+
}
101+
},
102+
take: page != all ? pageSize : undefined,
103+
skip: skip > 0 && page != all ? skip : undefined,
104+
})
105+
106+
// Next, get the tools whose description contains the query
107+
const toolEntriesWithDescription = await prisma.toolEntry.findMany({
108+
where: {
109+
AND: [
110+
{
111+
description: {
112+
contains: query,
113+
mode: 'insensitive'
114+
}
115+
},
116+
{
117+
NOT: {
118+
reference: {
119+
contains: query,
120+
mode: 'insensitive'
121+
}
122+
}
123+
}
124+
]
125+
},
126+
take: page != all ? pageSize : undefined,
96127
skip: skip > 0 && page != all ? skip : undefined,
97128
})
98129

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

101-
for (const entry of toolEntries) {
132+
// Add them to the results so that the ones with the query in the reference come first
133+
for (const entry of toolEntriesWithReference) {
102134
const parsedTool = entry.content as Tool[]
103135
tools[entry.reference] = tools[entry.reference] || []
104136
tools[entry.reference].push(...parsedTool)
105137
}
106138

107-
const totalCount = await prisma.toolEntry.count({ where: { reference: { contains: query } } })
139+
for (const entry of toolEntriesWithDescription) {
140+
const parsedTool = entry.content as Tool[]
141+
tools[entry.reference] = tools[entry.reference] || []
142+
tools[entry.reference].push(...parsedTool)
143+
}
144+
145+
const totalCount = await prisma.toolEntry.count({
146+
where: {
147+
OR: [
148+
{
149+
reference: {
150+
contains: query,
151+
mode: 'insensitive'
152+
}
153+
},
154+
{
155+
description: {
156+
contains: query,
157+
mode: 'insensitive'
158+
}
159+
}
160+
]
161+
}
162+
})
163+
108164
return { tools, totalCount }
109165
}

0 commit comments

Comments
 (0)