Okay, let's break down this challenge and propose a detailed, efficient solution for managing your facet mappings.
The core problem is managing the lifecycle of facet mappings independently of the index type lifecycle, because indices can outlive their index types but still require those mappings. You need a robust way to store, access, and eventually clean up unused mappings.
Here's a proposed solution focusing on decoupling, clarity, and efficient cleanup:
1. Storage Strategy: Dedicated Mapping Store
Choose one central place to store all mappings. Both a relational database table or a dedicated search index (like Elasticsearch/OpenSearch) can work.
-
Option A: Relational Database (Recommended for Structure & Relationships)
-
Create a dedicated table (e.g., facet_mappings).
-
Schema:
mapping_id: Primary Key (e.g., UUID or auto-incrementing INT).
facet_field_name: VARCHAR (e.g., 'color_code', 'brand_id'). The field in your search index documents this mapping applies to. (Index this column).
original_value: VARCHAR or TEXT. The value as it exists in the search index facet. (Index this column).
mapped_value: VARCHAR or TEXT. The user-friendly display value.
created_at: TIMESTAMP.
updated_at: TIMESTAMP.
last_seen_active_at: TIMESTAMP (Nullable). This field is crucial for the cleanup process. (Index this column).
- Optional:
created_by, notes, applies_to_indices (TEXT/JSON - could list patterns or specific indices, but adds complexity, maybe avoid initially).
-
Primary Key Consideration: A composite key on (facet_field_name, original_value) might seem natural, but having a separate mapping_id offers more flexibility, especially if you later want to track history or other metadata. Ensure you have a unique constraint on (facet_field_name, original_value).
-
Option B: Dedicated Search Index (e.g., Elasticsearch/OpenSearch)
- Create a new index (e.g.,
facet_mappings_store).
- Document Structure:
{
"mapping_id": "unique_id_here", // Use Elasticsearch's _id or generate your own
"facet_field_name": "color_code", // Keyword type
"original_value": "C01", // Keyword type
"mapped_value": "Crimson Red", // Text or Keyword type
"created_at": "timestamp",
"updated_at": "timestamp",
"last_seen_active_at": "timestamp" // Date type
}
- Benefit: Potentially faster lookups, especially if you already use this technology heavily. You can leverage search capabilities if needed (e.g., searching mappings themselves).
- Drawback: Might require careful handling of consistency if updates are frequent.
Recommendation: Start with the Relational Database approach if you have one available. It often provides clearer structure and relationship integrity for this kind of configuration data.
2. Mapping Definition and Usage (Frontend/Backend)
- UI: Your frontend allows users to define mappings for specific facet fields. When a user saves a mapping (e.g., for field
color_code, map C01 to Red), the backend saves this to the central facet_mappings store (DB table or search index).
- Search Service:
- When performing a search that includes faceting, the search service receives the results from the underlying search engine (e.g., Elasticsearch).
- It gets the facet counts (e.g.,
color_code: C01 (count: 5), color_code: C02 (count: 10)).
- For each facet field configured to use mappings, the search service queries the
facet_mappings store.
- It looks up mappings based on
facet_field_name and the original_value returned by the search engine.
- Example DB Query:
SELECT mapped_value FROM facet_mappings WHERE facet_field_name = 'color_code' AND original_value IN ('C01', 'C02');
- Example ES Query: Use a
terms query on facet_field_name and original_value.
- It replaces the
original_value with the mapped_value in the results sent back to the frontend. If a mapping doesn't exist for a specific value, it typically displays the original value.
- Crucially: As part of this lookup or shortly after, update the
last_seen_active_at timestamp for the mappings that were successfully found and used in the current search results. This can be done asynchronously or batched to minimize performance impact on the search request itself.
3. The Deletion Strategy: Periodic Cleanup Job
This is the core solution to avoid premature deletion and handle redundancy.
4. Implementation Details & Considerations
- Configuration Source: You need a reliable way to determine, for any given active index, which of its facet fields are configured to use mappings. This configuration might be stored alongside the index definition in your database.
last_seen_active_at Update Strategy:
- During Search: More accurate, reflects actual usage. Can add slight overhead. Use batching/async updates.
- During Cleanup Job (Step 3): Simpler, based purely on configuration. Less overhead on search path. Might keep mappings around slightly longer if a field is configured but rarely yields results for mapped values.
- Hybrid: Update during search for mappings