Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(typesense): skip collection check for search operations #898

Merged

Conversation

tharropoulos
Copy link
Contributor

What is this?

This PR improves search performance in the Typesense driver by removing an unnecessary collection existence check during search operations. Currently, before executing any search, there's an API call out to the collection's endpoint that verifies if a collection exists, which adds latency that increases with collection size and complexity. By skipping this verification specifically for search operations while maintaining it for indexing, we can significantly improve search response times.

Changes

Code Changes:

  1. In src/Engines/TypesenseEngine.php:
    • Modified getOrCreateCollectionFromModel() to add early return when indexOperation is false
    • Collection existence verification now only runs during index operations
    • Search operations bypass collection check and directly execute search queries

Performance Improvements:

  • Eliminates extra API call to verify collection existence before each search
  • Reduces search latency, especially beneficial for large collections with many fields
  • Maintains safety checks during index operations where verification is crucial

Demo

Before this change, each search operation required two API calls:

  1. GET /collections/{name} - Collection verification
  2. GET /collections/{name}/documents/search - Actual search

After this change, search operations only make the necessary search API call, while index operations maintain all safety checks.

Context

This optimization was prompted by a user's report of latency issues during search operations, particularly noticeable in larger collections. The change aligns with performance best practices by eliminating redundant API calls while maintaining data integrity through appropriate verification during index operations. I opened up an issue on #897 to keep track of the issue and the changes. This PR closes #897.

- Add early return for search operations in `getOrCreateCollectionFromModel`
- Maintain collection existence check only when `indexOperation` is true
- Optimize search performance by reducing unnecessary API calls
@taylorotwell taylorotwell merged commit 2fe33e6 into laravel:10.x Dec 18, 2024
17 checks passed
@tharropoulos tharropoulos deleted the typesense-skip-server-check-for-search branch December 18, 2024 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Typesense: Added latency due to unnecessary collection check before search
2 participants