React Components
Search documents
The SearchBox component provides a ready-to-use semantic search interface for your documents. Users can search their indexed content and view results with relevance scores.
bashnpm install @easyrag/react-components
tsximport { SearchBox } from '@easyrag/react-components'; function App() { return ( <SearchBox token={yourToken} datasetId="my-dataset" /> ); }
| Prop | Type | Description |
|---|---|---|
token | string | Authentication token (frontend token recommended) |
datasetId | string | Dataset ID to search within |
| Prop | Type | Default | Description |
|---|---|---|---|
apiUrl | string | https://api.easyrag.com/v1 | API base URL |
filters | array | [] | Metadata filters to apply |
limit | number | 5 | Number of results to fetch |
maxResults | number | 10 | Maximum results to display |
showScore | boolean | true | Display relevance scores |
showMetadata | boolean | false | Display document metadata |
theme | 'default' | 'minimal' | 'custom' | 'default' | Visual theme |
className | string | '' | Additional CSS classes |
customStyles | object | {} | Custom CSS class overrides |
labels | object | {} | Custom text labels |
showPoweredBy | boolean | true | Show "Powered by EasyRAG" |
poweredByUrl | string | https://easyrag.com | Powered by link URL |
| Prop | Type | Description |
|---|---|---|
onSearch | (query: string) => void | Called when search is submitted |
onResults | (results: SearchResult[]) => void | Called when results are received |
onError | (error: Error) => void | Called when an error occurs |
renderResult | (result: SearchResult, index: number) => ReactNode | Custom result renderer |
tsx<SearchBox token={token} datasetId="my-dataset" />
tsx<SearchBox token={token} datasetId="my-dataset" showScore={true} showMetadata={false} />
tsx<SearchBox token={token} datasetId="company-docs" filters={[ { key: 'department', match: { value: 'engineering' } }, { key: 'confidential', match: { value: false } } ]} />
tsx<SearchBox token={token} datasetId="my-dataset" onSearch={(query) => { console.log('Searching for:', query); }} onResults={(results) => { console.log(`Found ${results.length} results`); // Track in analytics analytics.track('search', { query, resultsCount: results.length }); }} onError={(error) => { console.error('Search failed:', error); }} />
tsx<SearchBox token={token} datasetId="my-dataset" renderResult={(result, index) => ( <div className="custom-result"> <h3>{result.metadata.title || 'Untitled'}</h3> <p>{result.pageContent}</p> <div className="metadata"> <span>Source: {result.metadata.source}</span> <span>Score: {(result.score * 100).toFixed(1)}%</span> </div> </div> )} />
tsx<SearchBox token={token} datasetId="my-dataset" theme="custom" customStyles={{ container: 'my-search-wrapper', input: 'my-search-input', button: 'my-search-button', resultCard: 'my-result-card', }} />
tsx<SearchBox token={token} datasetId="my-dataset" labels={{ placeholder: 'Search documentation...', buttonSearch: 'Find', buttonSearching: 'Searching...', emptyState: 'No results found. Try different keywords.', }} />
Modern, polished design with EasyRAG branding:
tsx<SearchBox token={token} datasetId="dataset" theme="default" />
Clean, simple styling:
tsx<SearchBox token={token} datasetId="dataset" theme="minimal" />
Full control over styling:
tsx<SearchBox token={token} datasetId="dataset" theme="custom" customStyles={{ container: 'bg-white rounded-lg shadow', input: 'border-2 border-gray-300 p-3', button: 'bg-blue-600 text-white px-6 py-3', resultCard: 'p-4 border-b hover:bg-gray-50', }} />
Full TypeScript support included:
typescriptimport type { SearchBoxProps, SearchResult } from '@easyrag/react-components'; interface SearchResult { score: number; pageContent: string; metadata: Record<string, any>; } const props: SearchBoxProps = { token: 'your-token', datasetId: 'my-dataset', showScore: true, onResults: (results: SearchResult[]) => { console.log(results); } };
Target specific elements with CSS:
css/* Container */ .easyrag-search-box { } /* Form */ .easyrag-search-box form { } /* Input */ .easyrag-search-box input { } /* Button */ .easyrag-search-box button { } /* Results container */ .easyrag-search-box .results { } /* Individual result */ .easyrag-search-box .result { }
Search within user-specific data:
tsx<SearchBox token={token} datasetId="shared-dataset" filters={[ { key: 'userId', match: { value: currentUser.id } }, { key: 'teamId', match: { value: currentUser.teamId } } ]} />
tsxfunction DocumentSearch() { const [hasFiles, setHasFiles] = useState(false); return ( <> <FileUpload token={token} datasetId="dataset" onUploadComplete={() => setHasFiles(true)} /> {hasFiles && ( <SearchBox token={token} datasetId="dataset" /> )} </> ); }
tsxfunction SmartSearch() { const [searchQuery, setSearchQuery] = useState(''); return ( <> <SearchBox token={token} datasetId="dataset" onSearch={(query) => setSearchQuery(query)} /> {searchQuery && ( <ChatBox token={token} datasetId="dataset" initialMessages={[ { id: 1, role: 'user', content: `Tell me more about: ${searchQuery}` } ]} /> )} </> ); }
Generate short-lived tokens from your backend:
typescript// Backend app.post('/api/tokens/create', authenticate, async (req, res) => { const token = signFrontendToken({ customerId: req.user.id, datasetId: req.body.datasetId, ttlSeconds: 3600 }); res.json({ token }); }); // Frontend const token = await fetch('/api/tokens/create') .then(r => r.json()) .then(d => d.token);
Apply filters for security and relevance:
tsx<SearchBox token={token} datasetId="dataset" filters={[ // Security: only user's data { key: 'userId', match: { value: currentUser.id } }, // Relevance: only published docs { key: 'status', match: { value: 'published' } } ]} />
Monitor search behavior:
tsx<SearchBox token={token} datasetId="dataset" onSearch={(query) => { analytics.track('search_started', { query }); }} onResults={(results) => { analytics.track('search_completed', { query, resultsCount: results.length, hasResults: results.length > 0 }); }} />
tsx<SearchBox token={token} datasetId="dataset" onError={(error) => { console.error('Search error:', error); toast.error('Search failed. Please try again.'); }} />
The SearchBox component includes:
Need help? Contact us at: