algolia / scout-extended

Scout Extended: The Full Power of Algolia in Laravel
https://www.algolia.com/doc/framework-integration/laravel/getting-started/introduction-to-scout-extended/
MIT License
393 stars 85 forks source link

Getting queryID from search resutlts #306

Closed amman20 closed 2 years ago

amman20 commented 2 years ago

Description

queryID is not reachable because it's neglected when creating ScoutExtendedBuilder.

Steps To Reproduce

When I send a callback to search Method:

$query = Product::search(
                $q,
                fn (SearchIndex $algolia, string $query, array $options) => setUpAlgoliaAnalytics($algolia, $query, $options)
            );
function setUpAlgoliaAnalytics(SearchIndex $algolia, string $query, array $options)
    {

        //For enabling insights
        $options['clickAnalytics'] = true;

        return $algolia->search($query, $options);
    }

$algolia->search($query, $options); returns an array with queryID added to it . However, when query builder is instantiated queryID value is neglected.

I need queryID to be able to implement insights

DevinCodes commented 2 years ago

Hi @amman20 , thank you for reaching out.

You can get the queryID by doing something like this:

$query = Product::search($q)->with(['clickAnalytics' => true]);
$queryID = $query->raw()['queryID'];

The raw method gives you access to the raw response as an array, and you can get the queryID from there. I hope this helps 🙂

If you feel like the code you described should work as well, feel free to open a PR and we'll happily review it 😄

Cheers!

amman20 commented 2 years ago

thank you @DevinCodes for your reply I will try this.