Elasticsearch And Apache Solr For Application Search
Elasticsearch and Apache Solr are distributed search platforms built on Apache Lucene. Both can index documents, analyze text, rank matches, filter and aggregate results, and scale search beyond ordinary SQL patterns. Choosing one is an operational and product decision, not simply a replacement for a database LIKE query.
A search cluster is a query-optimized projection of application data. It earns its place when relevance, language analysis, facets, highlighting, or distributed query capacity justify another operational system.
Why This Matters
Search results can be fast, ranked, and stale at the same time. The database or upstream content source remains authoritative, while Elasticsearch or Solr serves a denormalized view that must be rebuilt, secured, and monitored.
Start with the primary database record or other explicitly named source of truth. Identify who supplies input, who may change state, and what result the caller is entitled to observe. For Elasticsearch And Apache Solr For Application Search, the most persuasive evidence comes from execution plans, cache behavior, index state, durable records, and restore tests; naming those observations before implementation prevents tool names from standing in for a design.
Working Model
The application database normally remains authoritative. A search index is a denormalized projection designed around queries. Documents pass through analyzers at index and query time, fields have explicit types, and updates become visible according to refresh behavior. Search clusters have their own capacity, security, backup, and upgrade requirements.
The important vocabulary includes:
- Lucene indexes and segments
- documents and field mappings
- analyzers and tokenization
- relevance scoring
- filters and aggregations
- shards replicas and collections
The vocabulary belongs to indexing lifecycle decisions: mappings and schemas define field meaning, analyzers define tokens, shards and replicas define distribution, aliases support cutover, and relevance scoring defines result order. Treat each as an operational contract.
Core Design Decisions
- Start with product search requirements such as typo tolerance, facets, language analysis, highlighting, and ranking control.
- Design a search document from result-display and filter needs rather than mirroring relational tables mechanically.
- Choose analyzers before indexing production data because analyzer and mapping changes often require reindexing.
- Use filters for exact structured constraints and full-text queries for analyzed relevance.
- Treat Elasticsearch index aliases or Solr collection aliases as controlled cutover tools during reindexing.
- Select managed or self-hosted operation based on team capacity for upgrades, security, backups, and incident response.
Add a specialized store only when its query or latency benefits exceed synchronization and operational costs. Record the reason beside the code or configuration when the choice is not obvious, especially when future maintainers might otherwise “simplify” away a required guarantee.
PHP-Facing Example
The example creates an explicit search document instead of dumping a relational row. Production indexing should be just as intentional about field types, public visibility, and rebuildability.
<?php
declare(strict_types=1);
function productSearchDocument(array $product): array
{
return [
'id' => (string) $product['id'],
'name' => (string) $product['name'],
'description' => (string) $product['description'],
'category' => (string) $product['category'],
'price_cents' => (int) $product['price_cents'],
'published' => (bool) $product['published'],
];
}
print_r(productSearchDocument([
'id' => 42,
'name' => 'Desk lamp',
'description' => 'Adjustable office lighting',
'category' => 'lighting',
'price_cents' => 3999,
'published' => true,
]));
// Prints:
// Array
// (
// [id] => 42
// [name] => Desk lamp
// [description] => Adjustable office lighting
// [category] => lighting
// [price_cents] => 3999
// [published] => 1
// )
In Elasticsearch And Apache Solr For Application Search, notice which values the example accepts and what form it returns. Production code should preserve that clarity when it crosses the primary database record or other explicitly named source of truth, translating external or loosely typed data at the edge instead of allowing it to spread through unrelated classes.
Implementation Workflow
Start with a judged query set and a document design. Build a deterministic indexing path from authoritative records, add an alias-based rebuild and cutover process, and measure query latency and indexing lag. Only then tune analyzers, boosts, and shard counts.
Failure Modes
- Using the search engine as the sole source of truth for transactional records.
- Allowing dynamic mappings to guess important production field types.
- Indexing private fields without enforcing tenant and authorization filters.
- Updating documents directly without a repeatable rebuild path from authoritative data.
- Adding shards for performance without measuring data size, query patterns, and cluster overhead.
- Comparing products from one trivial query while ignoring administration, client support, and team experience.
Separate stale index data from authorization defects, mapping conflicts, cluster outages, and relevance regressions. A user seeing an old product description is different from a user seeing another tenant's private document.
Verification Strategy
Use the following checks as a starting point:
- Maintain a deterministic full-reindex command and test alias cutover.
- Use a fixed relevance corpus with expected top results for important queries.
- Measure indexing lag, query latency distributions, rejected work, heap, disk, and shard health.
- Test malformed documents and mapping conflicts.
- Restore snapshots into an isolated cluster and verify searchable documents.
- Run authorization tests from an untrusted client context.
Verification should include fixed relevance queries, malformed documents, full reindex from source data, alias cutover, snapshot restore, authorization checks from an untrusted tenant, and latency under representative filters.
Security And Data Handling
Limit network access, credentials, indexed fields, and query scope; storage performance never substitutes for authorization.
Search indexes, snapshots, query logs, and highlight snippets are copies of application data. Limit indexed fields, redact logs, and ensure deletion or correction workflows reach the index as well as the source database.
Tradeoffs And Evolution
Add a specialized store only when its query or latency benefits exceed synchronization and operational costs.
Analyzer and mapping changes often require reindexing. Use versioned indexes or collections, compare counts and representative queries, switch aliases deliberately, and retain the previous index until rollback is no longer needed.
Review Questions
Before considering the topic implemented, answer these questions:
- Which Elasticsearch And Apache Solr For Application Search guarantee matters to the caller?
- Where does the primary database record or other explicitly named source of truth live?
- Which input or state can be stale, malformed, duplicated, or unauthorized?
- What evidence from execution plans, cache behavior, index state, durable records, and restore tests proves the normal path?
- Which failure is retryable, and how are duplicate effects prevented?
- How will schema evolution, serialized values, index rebuilds, client versions, and replication behavior be handled during change?
- Which operational signal reveals degradation?
- What simpler choice was rejected, and why?
If the team cannot rebuild the index from authoritative data and explain acceptable lag, the design is incomplete. A search service should improve retrieval without becoming an undocumented source of truth.
Production Walkthrough
For product search, begin with concrete queries: exact SKU lookup, typo-tolerant name search, category facets, price filters, and unpublished-product exclusion. Design a document containing only fields needed for those behaviors. Keep private notes and internal cost fields out of the index.
Create a small relevance corpus before tuning. If desk lamp should rank a particular item first, record that expectation and rerun it when analyzers, synonyms, boosts, or engine versions change. A green health check does not prove relevance.
For rebuilds, create a new index, load from the database, compare document counts and sample records, run the query corpus, then move the alias. Test a failed cutover and a rollback while the old index still exists.
Capacity, Cost, And Operations
Search platforms need sizing decisions that ordinary SQL examples hide. Shards, replicas, heap, disk, merge activity, refresh interval, query concurrency, and indexing throughput all affect reliability. More shards are not automatically faster; each shard has overhead, and too many small shards can make the cluster harder to operate. Start with measured document size, query rate, index rate, and retention needs.
Refresh behavior controls when indexed changes become searchable. A shorter refresh interval can make writes visible sooner but increases background work. A longer interval improves indexing throughput but increases staleness. Product requirements should define acceptable lag for each use case. Checkout, compliance, and access-control decisions should not depend solely on a projection that is allowed to lag.
Backups and restores deserve rehearsal. Snapshots must include enough metadata to recover indexes, mappings, analyzers, aliases, and security configuration. Restoring into an isolated cluster and running the query corpus proves more than observing that snapshot creation succeeded.
Security is another operational boundary. Restrict network access, use least-privilege credentials, and avoid giving browser clients direct power to run arbitrary search DSL against private indexes. Server-side query builders should allowlist filters and fields so a search feature does not become an information-disclosure API.
Review Checks
Before release, answer three questions with evidence: can the index be rebuilt from source data, can a private record be excluded even when it matches a query, and can operators tell whether search is stale or unavailable? Keep a saved query set with expected results, an index-version diagnostic, and a documented alias rollback. These checks make search quality and recovery reviewable instead of dependent on one successful manual search.
Official References
What You Should Be Able To Do
After this lesson, you should be able to explain elasticsearch and apache solr for application search in operational terms, choose it only when its guarantees fit the requirement, implement a narrow PHP-facing boundary, recognize the common failure modes, and verify behavior using evidence from the real system rather than assuming the configuration is correct.
Practice
Model The Boundary
For Elasticsearch And Apache Solr For Application Search, write a short design note for a product-catalog application. Identify the caller, authoritative state, inputs, output, trust boundary, and one invariant. Include one success timeline and one partial-failure timeline.
Show solution
A strong answer names concrete ownership rather than only a tool. The authoritative state should be explicit, and derived copies should be labelled as such. The success timeline should identify when the result becomes observable. The failure timeline should stop after an intermediate step and explain whether retry is safe, whether status must be queried, or whether reconciliation is required.
The invariant should be testable. Examples include “one order causes at most one captured payment,” “only authorized tenant documents appear in results,” or “the latest valid configuration is the one served after rollout.”
Review A Design
Review an implementation of Elasticsearch And Apache Solr For Application Search that works in one local demonstration. Use the lesson’s failure modes to identify at least four production risks. For each risk, propose the narrowest change that restores a clear guarantee.
Show solution
The review should connect each risk to a violated guarantee. Good findings cover validation, authorization, retry or duplicate behavior, environment drift, and observability. Avoid broad recommendations such as “use best practices.” Name the responsible boundary and the evidence that would prove the repair.
A narrow repair may be a constraint, explicit comparison policy, interface contract, timeout, idempotency key, index, security rule, probe, IAM policy, or integration test. The selected mechanism must match the actual risk.
Build A Verification Plan
Create a verification plan for Elasticsearch And Apache Solr For Application Search. Include one unit test, one boundary-level integration test, one negative security or validation test, one failure-injection exercise, and two operational signals. State what each check proves and what it does not prove.
Show solution
The unit test should cover a pure decision. The integration test must cross the real boundary rather than only checking a prepared request. The negative test should use an identity or input that must be rejected. The failure exercise should create a timeout, retry, restart, unavailable dependency, or incompatible version deliberately.
Operational signals should reveal both health and correctness. Useful examples include latency percentiles, error classification, queue age, indexing lag, denied requests, restart count, duplicate side effects, resource saturation, and final business-state reconciliation.