distantnative / search-for-kirby

Kirby 3 plugin for adding a search index (sqlite or Algolia).
43 stars 3 forks source link

Algolia integration: different data types #24

Closed hannesherold closed 3 years ago

hannesherold commented 3 years ago

When sendig data to Algolia, the plugin sends all data as type string. To make use of some frontend widgets like rangeInput or rangeSlider, numeric data is mandatory, though.

It would be nice if the plugin would match the data type for Kirby num fields.

distantnative commented 3 years ago

I was contemplating implementing it. For number it would be pretty straightforward, however there are other fields that probably could get similar treatment (e.g. date) to be better usable for Algolia - but these are more ambitious in the way they should be formatted.

That's why I think I'll leave it better to the developer to make an intentional choice if a field needs to be stored in any other format than string. When you define which fields to store e.g. like

'search' => [
    'fields' => [
        'pages' => [
            'title',
            'yourNumberFiled'
        ]
    ]
]

what you can do is - instead of just specifying the field name - passing a callback function in which you transform to your desired data and format, e.g.

'myNumberField' => function ($model) { 
    return $model->myNumberField()->toFloat();
}

'myVirtualNumberField' => function ($model) { 
    return $model-> myNumberField()->toInt() + 5;
}

'myDate' => function ($model) { 
    return $model-> anyDateField()->toTimestamp();
}

I hope this helps your case! Let me know if there's any issue with this approach.

hannesherold commented 3 years ago

Yes, it’s a good solution, simple but also flexible. Nice!

Just one remark. I don't know if this is intended, but when a new page is created, the Algoila index gets updated immediately, even before the status of the new draft is changed. Changes to drafts also trigger an API call. This is not really necessary because drafts shouldn’t get searched anyway, right? Also, Algolia counts each index update as a unit - and units cost.

When the plugin initializes the index (e.g. via command line), drafts seem to be ignored. That’s good. So when creating new pages and working with drafts, wouldn't it be also better to wait until the status is changed to "unlisted" or "listed" before triggering the API?

distantnative commented 3 years ago

Good point: https://github.com/distantnative/search-for-kirby/issues/37