Implementing Search Functionality
1. Implementing Basic Search Logic
Example: SQL LIKE search
SELECT id, title FROM articles
WHERE title ILIKE '%' || :q || '%'
OR body ILIKE '%' || :q || '%'
LIMIT 50;
Warning: Leading-wildcard LIKE prevents B-tree index use; switch to FTS or trigram index for scale.
2. Implementing Full-Text Search
Example: PostgreSQL tsvector
ALTER TABLE articles ADD COLUMN tsv tsvector
GENERATED ALWAYS AS (
setweight(to_tsvector('english', title), 'A') ||
setweight(to_tsvector('english', body), 'B')
) STORED;
CREATE INDEX articles_tsv_idx ON articles USING GIN(tsv);
SELECT id, title, ts_rank(tsv, q) AS rank
FROM articles, plainto_tsquery('english', :q) q
WHERE tsv @@ q
ORDER BY rank DESC LIMIT 20;
3. Implementing Search Indexing
| Mode | Detail |
| Synchronous | Index inside write transaction; simple but slows writes |
| Async (outbox) | Write event, process to index |
| CDC | Debezium → Elasticsearch sink |
| Reindex job | Periodic full rebuild |
4. Implementing Search Filters
| Type | Detail |
| Term | Exact match (status, category) |
| Range | Date / numeric ranges |
| Geo | Within radius / bounding box |
| Boolean combos | AND/OR/NOT |
| Multi-select | Facets |
| Method | Detail |
| from + size | OK to ~10k results |
| search_after | Deep pagination, cursor-based |
| scroll | Snapshot iteration (export) |
| PIT (point in time) | Modern stable cursor |
6. Implementing Search Ranking
| Factor | Detail |
| TF-IDF / BM25 | Default relevance scoring |
| Field boosts | Title weighted > body |
| Recency | Decay function on date |
| Popularity | Click/view signals |
| Personalization | User preferences boost |
| Learning to Rank | ML on engagement signals |
7. Implementing Faceted Search
Example: Elasticsearch aggregations
{
"query": { "match": { "title": "laptop" } },
"aggs": {
"brand": { "terms": { "field": "brand.keyword", "size": 20 } },
"price": { "histogram": { "field": "price", "interval": 100 } },
"in_stock": { "filter": { "term": { "in_stock": true } } }
}
}
8. Implementing Auto-Complete
| Approach | Detail |
| Edge n-grams | Index prefixes |
| Completion suggester | Elasticsearch FST-based, very fast |
| Trie in Redis | Lightweight self-managed |
| Top-N popular | Boost by query frequency |
9. Implementing Search Suggestions
| Type | Detail |
| Did-you-mean | Levenshtein distance |
| Phonetic | Soundex, Metaphone |
| Synonyms | Synonym filter at index time |
| Related queries | Co-occurrence in session logs |
10. Implementing Search Result Highlighting
Example: Elasticsearch highlight
{
"query": { "match": { "body": "performance" } },
"highlight": {
"fields": { "body": { "fragment_size": 120, "number_of_fragments": 2 } },
"pre_tags": ["<mark>"],
"post_tags": ["</mark>"]
}
}
11. Implementing Search Analytics
| Track | Use |
| Top queries | Trends |
| Zero-result rate | Identify gaps |
| CTR per position | Rank quality |
| Refinement rate | Initial relevance |
| Latency p99 | Performance |
12. Integrating Search Engines (Elasticsearch)
| Component | Role |
| Index template | Mappings + settings per index pattern |
| ILM | Lifecycle: hot → warm → cold → delete |
| Aliases | Zero-downtime reindex |
| Pipelines | Ingest-time enrichment |
| Java client | elasticsearch-java typed API |