transformItems prop allows you to post-process search results before they’re displayed. This is useful for normalizing data, adding computed properties, filtering results, or adapting non-standard record structures.
Basic Usage
ThetransformItems function receives an array of hits and must return an array of hits:
Function Signature
Common Use Cases
1. Adding Computed Properties
Add custom properties to hits for use in custom components:2. Filtering Results
Remove certain results based on custom logic:3. Modifying URLs
Adjust result URLs to match your routing:4. Adding Index Badges
Identify which index each result came from:5. Sorting Results
Re-sort results based on custom criteria:Adapting Non-Standard Record Structures
If your Algolia index doesn’t follow the DocSearch schema, usetransformItems to map your data structure:
When transforming records to match the DocSearch structure, ensure you include all required fields:
objectID, content, url, url_without_anchor, type, anchor, hierarchy, _highlightResult, and _snippetResult.Required DocSearchHit Structure
Working with Highlight and Snippet Results
_highlightResult and _snippetResult contain HTML-formatted text with search term matches wrapped in highlight tags:
Combining with Other Features
With Multiple Indices
transformItems processes hits from all indices together:
With Custom Hit Component
Pass transformed data to your custom component:Performance Considerations
Good Practices
Avoid
Debugging Tips
Console Logging
Add
console.log(items) in your transform function to inspect the raw hits and understand the data structure.Type Checking
Use TypeScript to ensure your transformed items match the expected structure.
