Laravel/Scout/Algolia search by id/index/objectID

857 views Asked by At

I need the ability to search by id/index/objectID with algolia. So subscriber id 1829 I need the ability for someone to enter that into the search bar and 1829 comes up.

Using Algolia directly doesn't work either so first is this possible and second if so how do you set that up.

If this isn't possible I will have to create a separate search which isn't a problem just annoying.

2

There are 2 answers

0
Julien Bourdeau On

If you want to search only the objectID, you can also have something like this:

if (is_int($query)) {
    Model::search($query, function ($algolia, $query, $options) {

        $record = $algolia->getObject($query);

        return [
            'nbHits' => 1,
            'hits' => [$record],
        ];
    })->get();
} else {
    Model::serach($query);
}
0
Bara' ayyash On

You can make ObjectId searchable with highest priority, so when you search for the id, the first element should be the one with that id.