-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable switching between keyword and vector search, UI changes
- Loading branch information
Showing
9 changed files
with
127 additions
and
49 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,44 @@ | ||
import { search, vectorSearch } from "@/lib/search"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
export const runtime = "edge"; | ||
|
||
function customError(msg: string, code: number) { | ||
return NextResponse.json({ message: msg }, { status: code }); | ||
} | ||
|
||
export async function GET(request: NextRequest) { | ||
const searchParams = request.nextUrl.searchParams; | ||
if (!searchParams.has("query") || !searchParams.has("mode")) { | ||
return customError("Missing parameters", 400); | ||
} | ||
|
||
const query = searchParams.get("query"); | ||
const searchMode = searchParams.get("mode"); | ||
|
||
if (query === null || searchMode === null) { | ||
return customError("Invalid parameters", 400); | ||
} else if (query.length > 128) { | ||
return customError("Search entry is too long", 400); | ||
} else if (query.length === 0) { | ||
return NextResponse.json({}); | ||
} else if (!["normal", "vector"].includes(searchMode)) { | ||
return customError("Invalid mode", 400); | ||
} | ||
|
||
try { | ||
const results = | ||
searchMode === "normal" ? await search(query) : await vectorSearch(query); | ||
|
||
// Reduce network payload size for users | ||
const records = JSON.parse(JSON.stringify(results))["records"]; | ||
records.forEach((book: Record<string, any>) => { | ||
delete book.embeddings; | ||
delete book.xata; | ||
}); | ||
|
||
return NextResponse.json({ records }); | ||
} catch (error) { | ||
return customError("A strange error has ocurred", 500); | ||
} | ||
} |
This file was deleted.
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
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
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
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
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,21 @@ | ||
.searchSwitch { | ||
font: inherit; | ||
font-family: inherit; | ||
color: white; | ||
background-color: transparent; | ||
border-width: 2px; | ||
border-style: solid; | ||
border-color: rgba(195, 195, 195, 0.5); | ||
padding: 0.3rem; | ||
border-radius: 0; | ||
text-shadow: inherit; | ||
transition: border-color 0.15s; | ||
} | ||
|
||
.searchSwitch:hover { | ||
border-color: rgb(255, 255, 255); | ||
} | ||
|
||
.searchSwitch:active { | ||
background-color: rgba(255, 255, 255, 0.5); | ||
} |
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,31 @@ | ||
"use client"; | ||
import { useState } from "react"; | ||
import styles from "./SearchSwitch.module.css"; | ||
|
||
export let useVectorSearch = false; | ||
|
||
export function SearchSwitch() { | ||
const [searchMode, setSearchMode] = useState("Full-Text Search"); | ||
|
||
function handleClick() { | ||
if (searchMode === "Full-Text Search") { | ||
useVectorSearch = true; | ||
setSearchMode("Vector Search"); | ||
} else { | ||
useVectorSearch = false; | ||
setSearchMode("Full-Text Search"); | ||
} | ||
} | ||
|
||
return ( | ||
<button | ||
name="searchSwitch" | ||
id="searchSwitch" | ||
type="button" | ||
onClick={handleClick} | ||
className={styles.searchSwitch} | ||
> | ||
{searchMode} | ||
</button> | ||
); | ||
} |
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,2 @@ | ||
export * from "./SearchSwitch"; | ||
export { default } from "./SearchSwitch"; |