elastic / elasticsearch-php

Official PHP client for Elasticsearch.
https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html
MIT License
5.25k stars 964 forks source link

Ability to Execute _enrich Policy and Utilize JSON File Attachments in Bulk Operations with elasticsearch-php SDK #1355

Closed yesee-yongb-xu closed 10 months ago

yesee-yongb-xu commented 10 months ago

Summary of problem or feature request

Hello,

I am reaching out to inquire about some functionalities in the elasticsearch-php SDK:

Is it possible to execute an _enrich policy using a command similar to POST /_enrich/policy/A/_execute within the SDK? Can I perform bulk operations using the POST _bulk endpoint with attached JSON files? I am looking forward to understanding how to utilize these features (if supported) within the elasticsearch-php SDK. Your guidance would be highly appreciated.

Thank you!

System details

ezimuel commented 10 months ago

@yesee-yongb-xu sorry for my late reply. Here an example using execute enrich policy API:

use Elastic\Elasticsearch\ClientBuilder;

$client = ClientBuilder::create()
    ->setHosts(['put here the Elasticsearch URL'])
    ->build();

$params = ['name' => 'A'];

// Execute enrich policy API
$response = $client->enrich()->executePolicy($params);

And here an example of bulk API with 3 JSON files. Please note, in this example I'm using _id equal to 1, 2 and 3.

$files = [ 'file1.json', 'file2.json', 'file3.json'];

$params = [
    'index' => 'put here name of the index',
    'body' => []
];
$i = 1;
foreach ($files as $file) {
    $json = json_decode(file_get_contents($file), true);
    $params['body'][]= [
        'index' => [
            '_id' => $i++
        ]
    ];
    $params['body'][] = $json;
}
$response = $client->bulk($params);

Let me know if you need more help.

yesee-yongb-xu commented 10 months ago

Wow, I am unable to locate this use in the elasticsearch-php SDK file. Thank you,i will try it.