Skip to content

Commit f94ce81

Browse files
Revert "Semantic search attempt #1: convert query into zoekt syntax"
This reverts commit 7e818ca.
1 parent bb92366 commit f94ce81

4 files changed

Lines changed: 2 additions & 267 deletions

File tree

src/app/search/page.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export default function SearchPage() {
2828
const router = useRouter();
2929
const searchQuery = useNonEmptyQueryParam("query") ?? "";
3030
const _numResults = parseInt(useNonEmptyQueryParam("numResults") ?? `${DEFAULT_NUM_RESULTS}`);
31-
const isSemanticSearchEnabled = useNonEmptyQueryParam("semantic") === "true";
3231
const numResults = isNaN(_numResults) ? DEFAULT_NUM_RESULTS : _numResults;
3332

3433
const [selectedMatchIndex, setSelectedMatchIndex] = useState(0);
@@ -39,7 +38,6 @@ export default function SearchPage() {
3938
queryFn: () => search({
4039
query: searchQuery,
4140
numResults,
42-
semantic: isSemanticSearchEnabled,
4341
}),
4442
enabled: searchQuery.length > 0,
4543
});

src/lib/schemas.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export const searchRequestSchema = z.object({
55
query: z.string(),
66
numResults: z.number(),
77
whole: z.boolean().optional(),
8-
semantic: z.boolean().optional(),
98
});
109

1110

src/lib/server/searchService.ts

Lines changed: 1 addition & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,8 @@ import { FileSourceRequest, FileSourceResponse, ListRepositoriesResponse, listRe
44
import { fileNotFound, invalidZoektResponse, ServiceError, unexpectedError } from "../serviceError";
55
import { isServiceError } from "../utils";
66
import { zoektFetch } from "./zoektClient";
7-
import { generateText } from 'ai';
8-
import { openai } from '@ai-sdk/openai'; // Ensure OPENAI_API_KEY environment variable is set
9-
10-
export const search = async ({ query: _query, numResults, whole, semantic }: SearchRequest): Promise<SearchResponse | ServiceError> => {
11-
let query = _query;
12-
13-
if (semantic) {
14-
query = await convertSemanticQueryToZoektQuery(query);
15-
console.log(`Generated query: ${query}`);
16-
}
177

8+
export const search = async ({ query, numResults, whole }: SearchRequest): Promise<SearchResponse | ServiceError> => {
189
const body = JSON.stringify({
1910
q: query,
2011
// @see: https://github.com/TaqlaAI/zoekt/blob/main/api.go#L892
@@ -99,82 +90,4 @@ export const listRepositories = async (): Promise<ListRepositoriesResponse | Ser
9990
}
10091

10192
return parsedListResponse.data;
102-
}
103-
104-
const convertSemanticQueryToZoektQuery = async (query: string) => {
105-
const { text } = await generateText({
106-
model: openai('gpt-4'),
107-
system:
108-
`
109-
You are tasked with converting natural language code search queries into Zoekt's query language. Zoekt queries are fundamentally composed of regular expressions (regex), with certain prefixes, infix operators, and boolean logic controlling their behavior. Your job is to accurately interpret semantic queries and apply the correct Zoekt syntax rules to ensure precise search results.
110-
111-
Key Rules of Zoekt Query Syntax:
112-
113-
1. Regex by Default:
114-
115-
All queries are treated as regex by default. Special characters in the query are parsed as regex operators unless escaped.
116-
Example: A query for foo* matches any text starting with "foo" followed by any characters. To search for "foo*" literally, escape it as foo\\*.
117-
118-
2. Multiple Expressions:
119-
120-
Queries with multiple space-separated terms (expressions) are conjunctive. Each file must match all expressions to be included in the results.
121-
Example: foo bar searches for files containing both /foo/ and /bar/. To search for the phrase "foo bar", use quotes: "foo bar".
122-
123-
3. Boolean Logic:
124-
125-
or: Combines expressions to match any file that contains either expression.
126-
Example: foo or bar returns files matching /foo/ or /bar/.
127-
- (Negation): Excludes results that match the negated expression.
128-
Example: foo -bar returns files containing /foo/ but excluding those that also contain /bar/.
129-
Parentheses: Used to group expressions.
130-
Example: foo (bar or baz) returns files with /foo/ and either /bar/ or /baz/.
131-
132-
4. Prefix Expressions:
133-
134-
Specific prefixes restrict searches to particular parts of the codebase, like file names, contents, or repositories. Boolean logic applies to these as well.
135-
136-
Common Prefixes:
137-
138-
file: or f:: Restrict matches to file names.
139-
- Example: f:README.
140-
content: or c:: Restrict matches to file contents.
141-
- Example: c:README.
142-
repo: or r:: Match repository names.
143-
branch: or b:: Match branch names.
144-
lang:: Restrict matches to files in a specific language.
145-
Example: lang:typescript.
146-
sym:: Match symbol definitions using ctags information.
147-
case:: Adjust case sensitivity (case:yes for case-sensitive, case:no for case-insensitive).
148-
149-
5. Special Handling:
150-
151-
Use quotes for multi-word phrases or operators like or when meant literally: "foo or bar".
152-
Escape special characters in regex with backslashes if you want to match them literally.
153-
154-
Instructions for Converting Semantic Queries:
155-
1. Understand the Query's Intent:
156-
157-
Analyze the natural language query to understand its goal. Is the user looking for specific phrases, file names, function definitions, or other specific code elements?
158-
159-
2. Determine the Right Regex:
160-
161-
Convert phrases or keywords into regular expressions. Remember that Zoekt treats everything as a regex by default. Escape special characters as needed.
162-
Example: Convert “search for functions named main” to sym:\\bmain\\b.
163-
164-
3. Use Boolean Logic:
165-
166-
Combine expressions using or, -, and parentheses where necessary. For example, a query like "find either foo or bar, but not baz" translates to foo or bar -baz.
167-
168-
4. Apply Prefixes:
169-
170-
If the query specifies searching within file names, repositories, or other specific parts of the codebase, use appropriate prefixes like file:, repo:, or lang:.
171-
172-
5. Respect Case Sensitivity:
173-
174-
Consider whether case sensitivity matters based on the semantic query. Apply case:yes or case:no as needed. Output the zoekt query and nothing else.
175-
`,
176-
prompt: query
177-
})
178-
179-
return text;
18093
}

0 commit comments

Comments
 (0)