Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

onurzdgn's avatar

Elasticsearch and DataTables

Hi everyone, I am new on elasticsearch. I want write data to Datatables* from Elasticsearch. How can I do?

*DataTables = https://datatables.net/

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

You can use the official Elasticsearch PHP client to connect to your Elasticsearch instance and retrieve the data you need. You can then use the DataTables API to format the data and display it in a table.

Here is an example of how you can do this:

// Connect to Elasticsearch
$client = Elasticsearch\ClientBuilder::create()->build();

// Retrieve data from Elasticsearch
$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'body' => [
        'query' => [
            'match_all' => []
        ]
    ]
];
$results = $client->search($params);

// Format data for DataTables
$data = [];
foreach ($results['hits']['hits'] as $result) {
    $data[] = [
        'field1' => $result['_source']['field1'],
        'field2' => $result['_source']['field2'],
        'field3' => $result['_source']['field3']
    ];
}

// Initialize DataTables
$('#myTable').DataTable({
    data: data
});
1 like

Please or to participate in this conversation.