How to set elastic search custom field type?

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
kdim95
Advanced
Posts: 213
Joined: 26 Aug 2022, 12:17

How to set elastic search custom field type?

Post by kdim95 » 05 Apr 2024, 12:45

Laravel framework version: 10.16.1
Aimeos Laravel version: 2023.04.*
PHP Version: 10.46.0
Environment: Linux

Hello,

I want to set a custom field to be of type "date" in the elastic search mappings.
The reason behind this is because filtering results based on date breaks with custom attributes added to the decorator.

I found the configuration file here:
/vendor/aimeoscom/ai-elastic/setup/default/elastic

I added my own mapping and it works:
'myfield' => [ 'type' => 'date', 'format' => 'yyyy-MM-dd HH:mm:ss' ]

I want to know how to add this mapping the correct way, so it won't disappear when updating the package.

Best regards

nos3
Posts: 90
Joined: 01 Sep 2015, 13:26

Re: How to set elastic search custom field type?

Post by nos3 » 07 Apr 2024, 11:49

Create e.g. a ./setup/default/elastic/product.php in your own extension and add your new field there:

Code: Select all

return array(
	'base' => function ( array $mappings ) {

		$map = [
			'base' => [
				'properties' => [
					'myfield' => [ 'type' => 'date', 'format' => 'yyyy-MM-dd HH:mm:ss' ]
				],
			],
		];

		return array_replace_recursive( $mappings, $map );
	},

kdim95
Advanced
Posts: 213
Joined: 26 Aug 2022, 12:17

Re: How to set elastic search custom field type?

Post by kdim95 » 08 Apr 2024, 08:41

Thank you @nos3, works great

Code: Select all

<?php

return [
    'base' => function( array $mappings ) {

        $map = [
            'base' => [
                'properties' => [
                    'myfield' => [ 'type' => 'date', 'format' => 'yyyy-MM-dd HH:mm:ss' ]
                ]
            ]
        ];

        return array_replace_recursive( $mappings, $map );
    }
];

Post Reply