Build Elasticsearch Full-Text Search
backend
TypeScript
architecture
strict_senior
Create advanced search system with fuzzy matching, filters, aggregations, and autocomplete.
By lucas_a
12/8/2025
Prompt
Build a complete Elasticsearch full-text search system for [APPLICATION_NAME] with the following specifications:
Requirements
- Application name: [APPLICATION_NAME]
- Index name: [INDEX_NAME]
- Document type: [PRODUCT/ARTICLE/USER/etc]
- Searchable fields: [FIELD_1], [FIELD_2], [FIELD_3]
- Field boosting: [FIELD_1: WEIGHT], [FIELD_2: WEIGHT]
- Filter fields: [CATEGORY/PRICE/DATE/etc]
- Price range filters: [YES/NO]
- Category facets: [YES/NO]
- Autocomplete needed: [YES/NO]
- Fuzzy matching: [YES/NO]
- Highlighting: [YES/NO]
- Pagination size: [RESULTS_PER_PAGE]
- Elasticsearch version: [7.x/8.x]
Deliverables
Generate complete Elasticsearch integration code with:
Client initialization:
- Import @elastic/elasticsearch Client
- Configure connection to [ELASTICSEARCH_HOST]
- Authentication setup if needed
- Connection pooling and retry logic
Index creation for [INDEX_NAME]:
- Index settings: shards, replicas
- Custom analyzers for autocomplete with edge_ngram
- Tokenizers and filters configuration
- Mappings for all [SEARCHABLE_FIELDS]
- Field types (text, keyword, float, date, etc.)
- Analyzer assignment to text fields
Document indexing functions:
- Single document indexing with client.index()
- Bulk indexing function for multiple documents
- Document ID generation
- Error handling and retry logic
- Index refresh strategy
Search query builders:
- Basic match query for single field
- Multi-match query across [SEARCHABLE_FIELDS] with boosting
- Bool query with must/should/filter clauses
- Fuzzy query with AUTO fuzziness if [FUZZY_MATCHING]
- Wildcard and prefix queries
Filtering implementation:
- Range filters for [PRICE/DATE] fields
- Term filters for [CATEGORY] fields
- Multiple filter combination with bool query
- Filter context vs query context
Aggregations if needed:
- Terms aggregation for category facets
- Range aggregation for price buckets
- Date histogram for time-based data
- Nested aggregations for complex analytics
Autocomplete search if [AUTOCOMPLETE]:
- Edge n-gram analyzer configuration
- Autocomplete query implementation
- Limit results to top [N] suggestions
- Debouncing strategy
Result highlighting if [HIGHLIGHTING]:
- Highlight configuration for matched fields
- Fragment size and number configuration
- Custom highlight tags
- Parse highlighted results
Pagination:
- from/size pagination for [RESULTS_PER_PAGE]
- Search after for deep pagination
- Total count handling
Response parsing:
- Extract hits from response
- Parse _source documents
- Handle aggregation results
- Format highlighted snippets
API endpoint integration:
- Express/Fastify route handlers
- Request validation
- Query parameter parsing
- Response formatting
- Error handling
TypeScript types:
- Search request types
- Search response types
- Document schema types
- Aggregation result types
Output production-ready Elasticsearch search implementation.
Tags
elasticsearch
search
full-text
indexing
Tested Models
gpt-4-turbo
claude-3-opus