Transform lat/lng from collection to meilisearch _geo attribute #7
-
Hi, in order to use meilisearch's proximity search feature i try to add the necessary _geo attribute (https://docs.meilisearch.com/learn/advanced/geosearch.html#preparing-documents-for-location-based-search) to the documents. The statamic collection already has fields My first try was something like this.. but didnt work...
Any idea would be appreciated... Thx, Andreas |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
My first idea would be to use a ViewModel but it seems like it's not called before Search Indexing. We could ask Statamic to enable that but it might take a while. Instead we could add a config field like |
Beta Was this translation helpful? Give feedback.
-
We could do something like this: 'articles' => [
'driver' => 'meilisearch',
'searchables' => ['collection:articles'],
'fields' => ['title', 'content'],
'geosearch' => true,
], Then in the Index class add something like this: public function insert($document)
{
$fields = array_merge(
$this->searchables()->fields($document),
$this->getDefaultFields($document),
$this->getGeoFields($document),
);
$this->getIndex()->updateDocuments([$fields]);
}
private function getGeoFields(Entry|Term|LocalizedTerm|Asset|User $entry)
{
if ($this->config()['geosearch']) {
return [
'lat' => $this->get('latitude'),
'lng' => $entry->get('longitude'),
];
}
} With some kind of options to change the latitude / longitude field names and maybe format them to a specific decimal place?? It's quite a tricky problem... |
Beta Was this translation helpful? Give feedback.
-
Hi @tao thx for the inspirations! I ended up with adding a
|
Beta Was this translation helpful? Give feedback.
Hi @tao
thx for the inspirations! I ended up with adding a
_geo
to my Statamic collection which holds the lat and lng values separated by comma. So the transformer was easy to set up: