- Elastic Cloud Serverless
- Elasticsearch
- Elastic Observability
- Get started
- Observability overview
- Elastic Observability Serverless billing dimensions
- Create an Observability project
- Quickstart: Monitor hosts with Elastic Agent
- Quickstart: Monitor your Kubernetes cluster with Elastic Agent
- Quickstart: Monitor hosts with OpenTelemetry
- Quickstart: Unified Kubernetes Observability with Elastic Distributions of OpenTelemetry (EDOT)
- Quickstart: Collect data with AWS Firehose
- Get started with dashboards
- Applications and services
- Application performance monitoring (APM)
- Get started with traces and APM
- Learn about data types
- Collect application data
- View and analyze data
- Act on data
- Use APM securely
- Reduce storage
- Managed intake service event API
- Troubleshooting
- Synthetic monitoring
- Get started
- Scripting browser monitors
- Configure lightweight monitors
- Manage monitors
- Work with params and secrets
- Analyze monitor data
- Monitor resources on private networks
- Use the CLI
- Configure a Synthetics project
- Multifactor Authentication for browser monitors
- Configure Synthetics settings
- Grant users access to secured resources
- Manage data retention
- Scale and architect a deployment
- Synthetics Encryption and Security
- Troubleshooting
- Application performance monitoring (APM)
- Infrastructure and hosts
- Logs
- Inventory
- Incident management
- Data set quality
- Observability AI Assistant
- Machine learning
- Reference
- Get started
- Elastic Security
- Elastic Security overview
- Security billing dimensions
- Create a Security project
- Elastic Security requirements
- Elastic Security UI
- AI for Security
- Ingest data
- Configure endpoint protection with Elastic Defend
- Manage Elastic Defend
- Endpoints
- Policies
- Trusted applications
- Event filters
- Host isolation exceptions
- Blocklist
- Optimize Elastic Defend
- Event capture and Elastic Defend
- Endpoint protection rules
- Identify antivirus software on your hosts
- Allowlist Elastic Endpoint in third-party antivirus apps
- Elastic Endpoint self-protection features
- Elastic Endpoint command reference
- Endpoint response actions
- Cloud Security
- Explore your data
- Dashboards
- Detection engine overview
- Rules
- Alerts
- Advanced Entity Analytics
- Investigation tools
- Asset management
- Manage settings
- Troubleshooting
- Manage your project
- Changelog
Get started with the serverless PHP client
editGet started with the serverless PHP client
editThis client is for use with Elasticsearch Serverless only. See also the Elasticsearch clients.
This page guides you through the installation process of the PHP client for Elasticsearch Serverless, shows you how to initialize the client, and how to perform basic Elasticsearch operations with it.
Requirements
edit- PHP 8.0 or higher installed on your system.
Installation
editUsing the command line
editYou can install the PHP client using composer with the following commands:
composer require elastic/elasticsearch-serverless
Initialize the client
editInitialize the client using your API key and Elasticsearch endpoint:
require 'vendor/autoload.php'; use Elastic\Elasticsearch\Serverless\ClientBuilder; $client = ClientBuilder::create() ->setEndpoint('<elasticsearch-endpoint>') ->setApiKey('<api-key>') ->build();
To get API keys for the Elasticsearch endpoint for a project, see Get started.
Using the API
editAfter you’ve initialized the client, you can start ingesting documents. You can
use the bulk
API for this. This API enables you to index, update, and delete
several documents in one request.
Creating an index and ingesting documents
editYou can call the bulk
API with a body parameter, an array of actions (index)
and documents.
The following is an example of indexing some classic books into the books
index:
$body = [ [ "index" => [ "_index" => "books" ]], [ "name" => "Snow Crash", "author" => "Neal Stephenson", "release_date" => "1992-06-01", "page_count" => 470], [ "index" => [ "_index" => "books" ]], [ "name" => "Revelation Space", "author" => "Alastair Reynolds", "release_date" => "2000-03-15", "page_count" => 585], [ "index" => [ "_index" => "books" ]], [ "name" => "1984", "author" => "George Orwell", "release_date" => "1949-06-08", "page_count" => 328], [ "index" => [ "_index" => "books" ]], [ "name" => "Fahrenheit 451", "author" => "Ray Bradbury", "release_date" => "1953-10-15", "page_count" => 227], [ "index" => [ "_index" => "books" ]], [ "name" => "Brave New World", "author" => "Aldous Huxley", "release_date" => "1932-06-01", "page_count" => 268], [ "index" => [ "_index" => "books" ]], [ "name" => "The Handmaid's Tale", "author" => "Margaret Atwood", "release_date" => "1985-06-01", "page_count" => 311] ]; $response = $client->bulk(body: $body); # You can check the response if the items are indexed and have an ID print_r($response['items']);
When you use the client to make a request to Elasticsearch, it returns an API response object. This object implements the PSR-7 interface, that means you can check the for the HTTP status using the following method:
print($response->getStatusCode());
or get the HTTP response headers using the following:
print_r($response->getHeaders());
or reading the HTTP response body as follows:
print($response->getBody()->getContents()); # or using the asString() dedicated method print($response->asString());
The response body can be accessed as associative array or as object.
var_dump($response['items']); # associative array var_dump($response->items); # object
There are also methods to render the response as array, object, string and boolean values.
var_dump($response->asArray()); // response body content as array var_dump($response->asObject()); // response body content as object var_dump($response->asString()); // response body as string (JSON) var_dump($response->asBool()); // true if HTTP response code between 200 and 300
Getting documents
editYou can get documents by using the following code:
$response = $client->get(index: "books", id: $id);
Searching
editYou can search your documents using the search
API:
# Search for all the books written by Ray Bradbury $query = [ 'query' => [ 'match' => [ 'author' => 'Ray Bradbury' ]]]; $response = $client->search(index: "books", body: $query); printf("Documents found: %d\n", $response['hits']['total']['value']); # total documents found print_r($response['hits']['hits']); # list of books
For more information about the search
API’s query parameters and the response type,
refer to the
Search API
docs.
Updating documents
editYou can call the update
API to update a document:
$id = '<insert the document ID>'; # update the "page_count" value to 300 $body = [ "doc" => [ "page_count" => 300 ]]; $response = $client->update(index: "books", id: $id, body: $body); printf("Operation result: %s\n", $response['result']); # You get 'updated' as a result.
Deleting documents
editYou can call the delete
API to delete a document:
$id = '<insert the document ID>'; $response = $client->delete(index: "books", id: $id); printf("Operation result: %s\n", $response['result']); # You get "deleted" a as result.
Deleting an index
editYou can delete an entire index as follows:
$response = $client->indices()->delete(index: "books"); if ($response['acknowledged']) { print("Index successfully removed!"); }
On this page