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

feat(typesense): add 'infix' option to search parameters #863

Closed
wants to merge 2 commits into from

Conversation

TitusKirch
Copy link

This PR adds the infix option to the Typesense search parameters in Laravel Scout.

The Infix Search option allows users to perform more flexible searches by matching substrings in the middle of words. This provides greater accuracy and control over search results, especially when dealing with partial matches. It improves the search experience in applications where infix matching is critical, such as autocomplete features or when searching for terms that may not be at the beginning or end of a string.

@jasonbosco
Copy link
Contributor

jasonbosco commented Sep 9, 2024

@TitusKirch You can already set any search parameters (including infix) dynamically like this: https://laravel.com/docs/11.x/scout#typesense-dynamic-search-parameters

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

@TitusKirch
Copy link
Author

@TitusKirch You can already set any search parameters (including infix) dynamically like this: https://laravel.com/docs/11.x/scout#typesense-dynamic-search-parameters

@jasonbosco thanks for your quick reply. I know that you can set it via dynamic search parameters using the options method. However, it is not possible to set them globally depending on the query_by parameter. If you set the following globally:

<?php
$options = [
    'search-parameters' => [
        'query_by' => 'title, description',
    ],
];

Now I would have to append the options method in every search operation to achieve this, as the default is off.

Or am I missing something?

@TitusKirch
Copy link
Author

I have found a way to avoid having to redefine this every time. If someone has the same problem, you can do the following:

In the respective model you can define a method typesenseSearchParameters. E.g. as follows:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class User extends Model
{
    use Searchable;

    //...
    
    /**
     * Return the Typesense search parameters for the model.
     */
    public function typesenseSearchParameters(): array
    {
        return [
            'query_by' => 'name',
            'infix' => 'always',
        ];
    }
    
    //...
}

This then also works. See also in the TypesenseEngine: https://github.com/laravel/scout/blob/10.x/src/Engines/TypesenseEngine.php#L262-L264

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.

3 participants