Building Hybrid keyword and vector search with Algolia an...
This guide provides a structured approach to implementing an AI-powered search system, focusing on hybrid keyword-vector search, reranking, and UI integration. Steps address latency, indexing, and ranking challenges specific to AI search workflows.
Set up search infrastructure
Provision an Elasticsearch cluster with vector support or configure Pinecone with OpenAI embeddings. Install required plugins like elasticsearch-knn for vector search capabilities.
docker run -e "discovery.type=single-node" -p 9200:9200 docker.elastic.co/elasticsearch/elasticsearch:8.7.0⚠ Common Pitfalls
- •Missing vector plugin causes index failures
- •Incompatible Elasticsearch version with vector extensions
Create hybrid search index
Design an index mapping that combines keyword fields (text) with vector fields (embeddings). Use a multi-field approach to enable both term matching and semantic similarity searches.
{
"mappings": {
"properties": {
"content": { "type": "text" },
"embedding": { "type": "dense_vector", "dims": 1536 }
}
}
}⚠ Common Pitfalls
- •Incorrect vector dimensions cause indexing errors
- •Missing keyword fields limit traditional search capabilities
Implement hybrid search query
Construct a search query that combines match_all with a knn_search for vector similarity. Use function_score to weight results by both keyword relevance and vector distance.
{
"query": {
"function_score": {
"query": { "match_all": {} },
"knn": { "field": "embedding", "k": 10 },
"score_mode": "max"
}
}
}⚠ Common Pitfalls
- •Improper score_mode leads to unexpected ranking
- •KNN search without filtering causes excessive computation
Add reranking with business logic
Use a separate API (e.g., Cohere Rerank) to reorder results based on custom criteria after initial vector search. Pass original query and results to the reranker for context-aware sorting.
import cohere
co = cohere.Client('YOUR_API_KEY')
response = co.rerank(
query='search term',
documents=[result['text'] for result in initial_results],
top_n=5
)⚠ Common Pitfalls
- •Reranking API rate limits during high traffic
- •Missing document context causes poor reranking decisions
Build search UI components
Create a search bar with auto-suggestions, result cards showing confidence scores, and filters for different content types. Implement loading states for asynchronous search requests.
function SearchBox({ onSearch }) {
return (
<input
type="text"
placeholder="Search..."
onChange={(e) => onSearch(e.target.value)}
/>
);
}⚠ Common Pitfalls
- •Ignoring loading states causes UI freezes
- •Missing confidence indicators leads to unclear result quality
What you built
This implementation addresses core AI search challenges through hybrid indexing, reranking, and structured UI components. Validate each stage with real user queries and monitor performance metrics for continuous optimization.