Great question! Here’s a breakdown of your situation and some recommendations:
1. Scout + Typesense for Search
Laravel Scout with Typesense is an excellent choice for full-text and fuzzy searching, especially when you have large datasets and want fast, typo-tolerant search. It will outperform SQL LIKE queries in both speed and relevance.
2. Sorting in Typesense
Typesense supports sorting, but as you noted, sorting on string fields (especially long ones) can be memory-intensive. For fields like name or short titles, enabling sorting is generally fine. However, for long text fields (like descriptions), it’s best to avoid enabling sorting.
Typesense best practices:
- Enable sorting only on fields that users actually need to sort by.
- Avoid enabling sorting on large text fields.
- For each sortable field, set
"sort": truein your Typesense schema.
Example schema for a model:
{
"name": "products",
"fields": [
{"name": "id", "type": "int32"},
{"name": "name", "type": "string", "sort": true},
{"name": "category", "type": "string", "sort": true},
{"name": "description", "type": "string"} // not sortable
]
}
3. Hybrid Approach: Search with Typesense, Sort with SQL
If you need to sort by any column (including those not indexed in Typesense), consider a hybrid approach:
- Search with Typesense to get relevant IDs.
- Fetch and sort results with Eloquent using those IDs, applying SQL sorting.
Example:
// 1. Search with Typesense (via Scout)
$ids = Model::search($query)->keys(); // returns array of IDs
// 2. Fetch and sort in SQL
$results = Model::whereIn('id', $ids)
->orderBy($sortField, $sortDirection)
->paginate(20);
This way, you get fuzzy search and can sort by any column, but you only sort a small subset of IDs, not the entire table.
4. Alternatives
- Meilisearch: Similar to Typesense, with good support for sorting and fuzzy search.
- Elasticsearch: More complex, but very powerful for advanced search and sorting needs.
5. Summary
- Scout + Typesense is appropriate for your use case, as long as you’re careful about which fields are sortable.
- For sorting on all fields, use a hybrid approach: search with Typesense, then sort with SQL.
- Avoid enabling sorting on large text fields in Typesense.
References:
Let me know if you’d like code examples for a specific model or more details on the hybrid approach!