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

Support for scripted fields #203

Open
prugala opened this issue Dec 12, 2024 · 2 comments
Open

Support for scripted fields #203

prugala opened this issue Dec 12, 2024 · 2 comments

Comments

@prugala
Copy link

prugala commented Dec 12, 2024

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.

@damienalexandre
Copy link
Member

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.

@prugala
Copy link
Author

prugala commented Dec 12, 2024

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

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

No branches or pull requests

2 participants