-
Notifications
You must be signed in to change notification settings - Fork 340
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
[10.x] Add Typesense engine #773
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @jasonbosco, thanks for your PR. There's still quite a bit of styling improvements needed here. Can you make sure the code style of this PR adheres to the rest of the library? When done we can mark it as ready for Taylor to review. Thanks
@driesvints Thank you for the quick review! @karakhanyans has fixed the linting issues and the |
@jasonbosco a lot of the DocBlocks are still missing their first line comment. Could you add those? |
Thanks all 👍 |
@jasonbosco You're calling a few model methods I've never heard of:
Can you explain both of these methods? What are they supposed to do? Examples? More detail in general please. I generally need full documentation for how to use this from start to finish. Please mark as ready for review when posted. |
@taylorotwell In the current engine we have, these are methods that needed to be defined in the model. But @karakhanyans and I just discussed this, and we're going to take this opportunity to simplify this interface, and move it into the engine configuration, so these methods won't be required any more. Will share detailed docs once we make these changes. |
Thanks |
@karakhanyans has fixed the root cause of the issue in typesense-php. The latest code should now pull the correct version of typesense-php and fix the issue you saw. |
@jasonbosco how do I handle soft deleted records? What do I add to my schema? Furthermore, if I'm using Typesense how can I delete all my collections for a fresh start? Please mark as ready for review when questions are answered. |
Soft deletes are handled here. Essentially if So you'd want to add a field called ...
'typesense' => [
...
'model-settings' => [
User::class => [
'collection-schema' => [
'fields' => [
[
'name' => 'name',
'type' => 'string',
],
[
'name' => 'created_at',
'type' => 'int64',
],
[ /****** Add this field for soft deletes ********************************/
'name' => '__soft_deleted',
'type' => 'int32',
'optional' => true
],
],
'default_sorting_field' => 'created_at',
],
'search-parameters' => [
'query_by' => 'name, title'
],
],
],
], I'll update the docs in the description with this information shortly.
If you're running on dev, to get a clean slate, you'd want to stop the Typesense server, delete the contents of the In a production environment, you'd want to call |
@jasonbosco another bug related to soft delete. If I do this: return User::search('Delpha')->withTrashed()->get(); I get a result (as expected because Delpha is trashed). But if I do this: return User::search('Delpha')->onlyTrashed()->get(); I don't get any results. I would expect to still get my result. |
@jasonbosco drafting this again pending response on above. |
@taylorotwell - could you give it a shot now? @karakhanyans has fixed the soft delete issue and also written a test for it. |
OK - thanks. Another question. Let's imagine I have a schema for my user model. I later add another column to the database and want to add it to my schema. I update my Scout configuration file's Typesense schema configuration... then what? I assume the schema isn't automatically updated in Typesense? What is the user story here? |
That's correct. Schema changes need to be done in Typesense directly using the alter schema endpoints. Alternatively, there's also an auto-schema detection mode in Typesense, where users can define field names using regex in the collection schema, so at least new field additions matching a pattern do not need schema alterations, but will be auto-added by Typesense when a document is indexed with that field name pattern. We can probably add a note to the configuration section in Scout saying that the schema defined here is only used when the collection is created the first time to make this clear? |
@jasonbosco would it be reasonable to make the default config in Scout reflect auto-schema detection? |
@taylorotwell Setting that to the default could potentially lead to unnecessary fields being indexed and taking up RAM, without the user realizing. For eg, I've commonly seen URL fields getting indexed accidentally, when typically they are only used for display purposes and not for full-text search purposes. May be we can call out auto-schema detection as a commented-out field in the example schema, with a link to the docs, so users are aware of its existence? |
@jasonbosco is there any kind of admin dashboard or something where users can make schema changes? It just feels cumbersome to have to manually craft HTTP calls to update the schema. |
The dashboard that the core Typesense team maintains is available as part of Typesense Cloud. Happy to set you up with a free account. If you want to sign up and let me know, I can provision a free cluster for you to use. There's a Postman collection available here. There's a community-maintained dashboard here, but it looks like it doesn't support the alter schema endpoint yet. |
Thanks 👍 |
@taylorotwell @driesvints Following up from our conversation, this PR adds native support for Typesense (an open source alternative to Algolia) to Scout.
It follows a code structure similar to the Algolia engine to maintain consistency.
CC: @karakhanyans @arayiksmbatyan
P.S. This PR supersedes #772.
Typesense
Typesense is an open source, typo tolerant search engine that is optimized for instant sub-50ms searches,
while providing an intuitive developer experience.
When using the Typesense driver you will need to install the Typesense PHP SDK via the Composer package manager:
Then, set the
SCOUT_DRIVER
environment variable as well as your Typesense host and API key credentials within your application's .env file:You can also modify
port
,path
andprotocol
accordingly:For more information regarding Typesense, please consult the Typesense documentation.
toSearchableArray
To make your search compatible with Typsense, cast your model
id
to string andcreated_at
to int32 timestamp as shown below.Collection Schemas and Search Parameters
To setup Typesense collection schemas, in
config/scout.php
find thetypesense
driver configs and modifymodel-settings
array as shown in example below.To modify the model
query_by
attributes you have to setupseach-parameters
inmodel-settings
Note:
query_by
is a required parameter.Search Parameters On The Fly
Typesense supports the ability to set almost all search parameters on the fly, if needed.
To modify search parameters on the fly you can use
setSearchParameters
method of the Typesense Engine.Here are some example search parameters:
Full list of search parameters and descriptions can be found here.