spotlight icon indicating copy to clipboard operation
spotlight copied to clipboard

Strange behavior with Laravel Scout and Algolia

Open bee-interactive opened this issue 3 years ago • 1 comments

``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

bee-interactive avatar Aug 09 '22 14:08 bee-interactive

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 ?

DanDvoracek avatar Aug 09 '22 15:08 DanDvoracek

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']
                );

PhiloNL avatar Aug 16 '22 18:08 PhiloNL

@PhiloNL Yess the solution worked!

bee-interactive avatar Aug 21 '22 21:08 bee-interactive