wire-elements / spotlight

Livewire component that brings Spotlight/Alfred-like functionality to your Laravel application.
MIT License
927 stars 72 forks source link

Strange behavior with Laravel Scout and Algolia #91

Closed bee-interactive closed 2 years ago

bee-interactive commented 2 years ago

``Hi!

I have a project that can search through a list of products. For better search result, I have connected my model with Laravel Scout and Algolia.

When I search within my controller with some specific keywords, I get 9 results.

But when I search within the spotlight component, I only get 1 result..

Here's my function in my spotlight component:

public function searchProduct(string $query): Collection
    {
        return Product::search($query)
            ->get()
            ->map(function (Product $product) {
                return new SpotlightSearchResult(
                    $product->id,
                    $product->name,
                    $product->code,
                    []
                );
            });
    }

It doesn't seem wrong to me.. Am I missing something? Does the SpotlightSearchResult group some results?

Thank you in advance for your support

DanDvoracek commented 2 years ago

Same issue here, using a different syntax:

return Product::where('name', 'like', "%" . $query . "%")
            ->get()
            ->map(function (Product $product) {
                return new SpotlightSearchResult(
                    $product->id,
                    $product->name,
                    'description',
                    []
                );
            });

I have multiple products with Zown in the name. Yet only one gets shown in Spotlight results.

Note that products can have dots in the name and infos like 175.3 x 29.2 x h. 46.4cm.

Any idea why I would get less results than available ?

PhiloNL commented 2 years ago

Spotlight also uses Fuse.js to do additional searching. Maybe this is causing the issue. If you pass additional data as synonyms, this should be resolved, I think:

return new SpotlightSearchResult(
                    $product->id,
                    $product->name,
                    $product->code,
                    ['additional', 'values', 'that', 'fuse.js', 'will', 'search']
                );
bee-interactive commented 2 years ago

@PhiloNL Yess the solution worked!