elastically icon indicating copy to clipboard operation
elastically copied to clipboard

Support for scripted fields

Open prugala opened this issue 1 year ago • 2 comments

Hello

There is option to map scripted field to model class?

I have query:

_source' => true,
'script_fields' => [
    'likedMe' => [
        'script' => [
            'source' => "params._source.likedUsers.contains(params.userId)",
            'params' => [
                'userId' => $user->getId()->toString(),
            ]
        ]
    ]
]

Output from ES looks like:

{
    "took": 9,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "users",
                "_id": "0193ba7e-21e4-7d68-b4d2-69052c4b4d38",
                "_score": 1.0,
                "_source": {
                    "id": "0193ba7e-21e4-7d68-b4d2-69052c4b4d38",
                    "identifier": "ac3",
                    "slug": "ac3",
                    "createdAt": "2024-12-12T10:50:48+00:00",
                    "likedUsers": [
                        "019397c5-fff2-7e5d-81e5-6215a2dc2fa6"
                    ],
                    "likedMe": false,
                    "data": {
                        "location": {
                            "name": "New York",
                            "lat": 41.12,
                            "long": -71.34
                        }
                    }
                },
                "fields": {
                    "likedMe": [
                        true
                    ]
                }
            }
        ]
    }
}

There is a option to overwrite _source.likedMe with custom field from fields.likedMe? Fields _source.likedMe its always false because its default PHP model property value.

prugala avatar Dec 12 '24 13:12 prugala

Hello, this does not exists yet in Elastically, but it would be nice.

Would you mind sending a PR?

I guess an array_merge of "fields" in "_source" before deserialization could to the trick.

damienalexandre avatar Dec 12 '24 13:12 damienalexandre

@damienalexandre yes sure. I will have some time tomorrow or on weekend so prepare PR with support.

prugala avatar Dec 12 '24 13:12 prugala