Skip to content

Latest commit

 

History

History
48 lines (38 loc) · 1018 Bytes

File metadata and controls

48 lines (38 loc) · 1018 Bytes
endpoint get_source
lang php
es_version 9.3
client elasticsearch/elasticsearch==9.3.0

Elasticsearch 9.3 get_source endpoint (PHP example)

Use $client->getSource() to retrieve only the document body, without metadata.

$doc = $client->getSource(['index' => 'products', 'id' => 'prod-1']);
echo "{$doc['name']}\${$doc['price']}\n";

Selecting fields

Use _source_includes or _source_excludes to return a subset of fields:

$doc = $client->getSource([
    'index' => 'products',
    'id' => 'prod-1',
    '_source_includes' => ['name', 'price'],
]);

Handling missing documents

A ClientResponseException with status 404 is thrown when the document does not exist:

use Elastic\Elasticsearch\Exception\ClientResponseException;

try {
    $client->getSource(['index' => 'products', 'id' => 'prod-999']);
} catch (ClientResponseException $e) {
    if ($e->getCode() === 404) {
        echo "Document not found\n";
    } else {
        throw $e;
    }
}