Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow transforming of search index values #2462

Merged
merged 3 commits into from
Oct 6, 2020
Merged

Allow transforming of search index values #2462

merged 3 commits into from
Oct 6, 2020

Conversation

riasvdv
Copy link
Contributor

@riasvdv riasvdv commented Sep 18, 2020

This PR allows you to add a transformers key to your search index configuration:

For example, when you have a location field that saves all raw data in the entry:

'indexes' => [
        'locations => [
            'driver' => 'algolia',
            'searchables' => 'collection:locations',
            'fields' => [
                'title',
                'address',
            ],
            'transformers' => [
                'address' => function ($address) {
                    if (is_null($address)) {
                        return null;
                    }

                    return [
                        '_geoloc' => $address['rawAnswer']['hits'][0]['_geoloc'],
                        'country' => $address['rawAnswer']['hits'][0]['country'],
                        'city' => $address['rawAnswer']['hits'][0]['city'],
                        'postcode' => $address['rawAnswer']['hits'][0]['postcode'],
                    ];
                }
            ]
        ],

    ],

This would result in a document like this:

[
    'title' => 'A new location',
    '_geoloc' => [
        'lat' => 51.3345,
        'lng' => 4.3712,
    ],
    'country' => 'Belgium',
    'city' => 'Antwerp',
    'postcode' => 2940,
]

Which is important for Algolia, because the _geoloc data has to be top-level.

@jasonvarga
Copy link
Member

Thanks for the PR.

In this example, is your address field a custom fieldtype?

If so, I'd prefer to have the transformer on the fieldtype level, so we could implement this: statamic/ideas#321

@riasvdv
Copy link
Contributor Author

riasvdv commented Sep 18, 2020

Ah, yeah it is. But I'd still prefer to have some control. For example in a Bard field, I'd want to strip everything not text related to the search index. That would be hard to do in "userland" since you can't just edit the Bard fieldtype class

@riasvdv
Copy link
Contributor Author

riasvdv commented Sep 30, 2020

@jasonvarga Is this implementation something you want to continue with? Then I'm willing to add tests as well, otherwise we can close this

@jasonvarga
Copy link
Member

I think I initially misunderstood this PR. This does look like a good solution!

The only thing a little unclear is that you need to return an array of key/value pairs. If you wanted to perform just a simple transform, I would have initially just thought you could return the value.

e.g. This:

'transformers' => [
    'title' => function ($title) {
        return strtoupper($title);
    }
]

instead of this:

'transformers' => [
    'title' => function ($title) {
        return ['title' => strtoupper($title)];
    }
]

I wonder if there's a solution that's the best of both worlds?

@jasonvarga
Copy link
Member

I suppose we could check if the return value is an array, treat it how you are doing it right now, otherwise assume it's a single value.

If you want the returned value to be an array, you'd need to wrap it.

'transformers' => [
    'array_field' => function ($arr) {
        return ['array_field' => $arr];
    }
]

@riasvdv
Copy link
Contributor Author

riasvdv commented Oct 6, 2020

@jasonvarga Added support for both cases + tests!

Copy link
Member

@jasonvarga jasonvarga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants