Using ViewModels to transform data before Indexing #8
tao
started this conversation in
Feature Requests
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In order to use the MeiliSearch Geosearch you need to provide an array
_geo
withlat
andlng
fields. The default Statamic search transformer doesn't provide much flexibility in combining fields into this format.One solution was to add a
_geo
field to Statamic collection which holds thelat
andlng
values separated by comma. So the transformer was easy to set up as follows:While this might work in some cases it could also be complicated for some users, especially if you don't want to store the coordinates as a comma separated field in your collection.
Another solution could be to call the collection ViewModel before inserting or updating data. To do so we may have to modify the following two methods:
update
andinsert
.For example I have the following ViewModel which does a word count, the same example used in the Statamic documentation:
In the driver function you can call this manually as follows:
And in the
insert
function we need to call it a bit differentlyHowever, now my
word_count
is included in the Search:This could provide a lot more options for manipulating the data before inserting them into the Search Engine, however you may want to perform different transformations on the data for the search engine compared to the frontend views. We could potentially overcome this by trying to call a specialised
search
function in the ViewModel:So this same principle could be applied to include
_geo
required for MeiliSearch:And some safety checks might be necessary to only call the ViewModel on Entries and not Taxonomies, etc.
Beta Was this translation helpful? Give feedback.
All reactions