Algolia geo search how to structure db

Hi,

I am trying to adapt to algolia geo search, from my testing without a field _geoloc, algolia geo search doesn’t work.

Now my database looke like this:

<img src="//discourse-cloud-file-uploads.s3.dualstack.us-west-2.amazonaws.com/algolia/original/2X/9/9ff0c33cccaa5518dce69c795099030cd3965fd0.jpg"width="690" height=“335”>

However when I try to run laravel scout I get:

[AlgoliaSearch\AlgoliaException]
_geoloc attribute cannot contains a string near line:1 column:165

I assume that is due to it’s format, but how can I forgot the databse so it has the right structure?

Hi there,

The _geoloc attribute needs to contains floating point numbers. I seems you are sending strings.
I’m not a Laravel expert so I don’t know how you can transform the data. Maybe try in the method getAlgoliaRecord: https://github.com/algolia/algoliasearch-laravel#quick-start-1 ?

Hi, I did fix it now. For it to work I had to create a table called _geoloc and inside it have lag and lng float fields with the data. That table was then attached (joined) to a business table which actually works. It is a bit weird when I tried to input _geoloc as a field in a table it wouldn’t does it have to be in that format? Before I had locations table with lat and lng as floats but the geo search always returned null

Hi,

If you want to keep lat and lng as separate columns, you will simply have to prepare your record before it’s sent to Algolia. Laravel Scout provide a method for that: https://laravel.com/docs/5.4/scout#configuring-searchable-data

In this case you will have

    public function toSearchableArray()
    {
        $record = $this->toArray();

        $record['_geoloc'] = [
            'lat' => $record['lat'],
            'lng' => $record['lng'],
        ];

        unset($record['lat'], $record['lng']);

        return $record;
    }

If you prefer having a _geoloc column you can use cast feature of Laravel. I think you got the “_geoloc attribute cannot contains a string” error because the column wasn’t json decoded.
https://laravel.com/docs/5.4/eloquent-mutators#array-and-json-casting

Please let me know if that answers your question.

Just a quick question regarding your answer, so I have a businesses and _geoloc table. _geoloc is joined go businesses and that way the algolia geo search works fine. Does the algolia geo search actually works without having it named _geoloc? If I have lat and lng separate not inside _geoloc it seems I cannot for example search by radius, if it is inside _geoloc it works fine