Create a simple search engine
By using the search
method, you can implement a simple but fast search engine. This example uses Workers Binding, but can be easily adapted to use the REST API instead.
To replicate this example remember to:
- Disable
rewrite_query
, as you want to match the original user query - Configure your AutoRAG to have small chunk sizes, usually 256 tokens is enough
export interface Env { AI: Ai;}
export default { async fetch(request, env): Promise<Response> { const url = new URL(request.url); const userQuery = url.searchParams.get("query") ?? "How do I train a llama to deliver coffee?"; const searchResult = await env.AI.autorag("my-rag").search({ query: userQuery, rewrite_query: false, });
return Response.json({ files: searchResult.data.map((obj) => obj.filename), }); },} satisfies ExportedHandler<Env>;
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Products
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark