SearchSense: A Complete Library For TypeSense with Laravel

Davey Velthove | 2024-12-08

After three intensive months of research and development, I am thrilled to announce that a complete TypeSense library for Laravel called SearchSense is released as beta software. This library will manage the entire flow; from client requests to returning JSON responses ‐ making it a breeze for you to implement.

Download and Documentation

→ Click here for the download and documentation (README.md included)

Why TypeSense?

TypeSense stands out as a more user-friendly alternative to Elasticsearch. Before this project, I invested a significant amount of time following comprehensive courses on Elasticsearch. While powerful, it often felt unnecessarily complex to construct intricate queries.

I’d known about TypeSense for a while, but I initially dismissed it due to common prejudices: “Elasticsearch is the market leader” or “everyone uses Elasticsearch.” Embracing TypeSense proved to be a valuable lesson in challenging these assumptions.

When integrating Laravel Scout with TypeSense, you’ll notice the absence of an “Eloquent-style” wrapper for crafting queries. Instead, you must provide raw queries. This raises questions like, “How do I handle client API requests efficiently?” and “How should I structure my responses?”

The solution I’ve developed addresses all these challenges. It’s quite advanced, and I’m excited to share its key features.

The Blueprint

  • Centralize Entity Information: Define your entity’s metadata once and reuse it across the entire library.
  • OOP-Based Collection Schema: Predefine your collection schema through object-oriented programming for clarity and maintainability.
  • Static Filters: Easily set filters based on criteria such as user roles. For instance, regular users may see limited documents, while admins can view them all.
  • Custom Client Selection: Choose a custom client class for additional, entity-specific functionality.

Middleware

  • API Endpoint Control: Securely specify which API endpoints clients can access.
  • Field Whitelisting: Control which fields are exposed and queryable, enhancing both performance and security.

Client

  • OOP Query Building: Construct queries programmatically in an intuitive, object-oriented manner.
  • Entity-Specific Clients: Create dedicated client classes (e.g., <Model>TypeSenseClient) to encapsulate entity-specific logic and keep your code organized.

Response

  • Consistent JSON Structure: Define a standardized, well-organized JSON response format.
  • Sortable Fields: Specify which fields users can sort by in their responses.
  • Checkbox and Select Filters: Easily implement these filter types in your application.
  • autoSetOptions(): Automatically generate filter options based on your data.
  • Faceting Data: Effortlessly include facet information for filters and their respective options.
  • Whitelist Fields in Responses: Control which document fields appear in the final output.
  • CamelCase Transformations: Automatically convert field names to camelCase for frontend convenience, saving you manual effort.

Currently, the software is in its beta stage. While it already provides robust functionality, certain advanced features might contain bugs even though I tested the software thoroughly.

Stay tuned for more updates and get ready to experience the seamless synergy of SearchSense and Laravel Scout!