
Security News
MCP Community Begins Work on Official MCP Metaregistry
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
github.com/elastic/elasticsearch-php
This is the official PHP client for Elasticsearch.
You can run Elasticsearch and Kibana on your local machine using this command:
curl -fsSL https://elastic.co/start-local | sh
or sign-up for a free trial of Elastic Cloud.
Refer to the Installation section of the getting started documentation.
Refer to the Connecting section of the getting started documentation.
The elasticsearch-php
client offers 500+ endpoints for interacting with
Elasticsearch. A list of all these endpoints is available in the
official documentation
of Elasticsearch APIs.
Here we reported the basic operation that you can perform with the client: index, search and delete.
This client is versioned and released alongside Elasticsearch server.
To guarantee compatibility, use the most recent version of this library within the major version of the corresponding Enterprise Search implementation.
For example, for Elasticsearch 8.16
, use 8.16
of this library or above, but
not 9.0
.
The Elasticsearch client is compatible with currently maintained PHP versions.
Language clients are forward compatible; meaning that clients support communicating with greater or equal minor versions of Elasticsearch without breaking. It does not mean that the client automatically supports new features of newer Elasticsearch versions; it is only possible after a release of a new client version. For example, a 8.12 client version won't automatically support the new features of the 8.13 version of Elasticsearch, the 8.13 client version is required for that. Elasticsearch language clients are only backwards compatible with default distributions and without guarantees made.
Elasticsearch Version | Elasticsearch-PHP Branch | Supported |
---|---|---|
main | main | |
9.x | 9.x | 9.x |
8.x | 8.x | 8.x |
The 9.0.0 version of elasticsearch-php
contains the same architecture of 8.x.
It supports PSR-7 for HTTP messages and
PSR-18 for HTTP client communications.
We tried to avoid BC breaks for 9.x
, here the main changes:
elastic/elasticsearch-serverless
client is being deprecated, and its functionality has been merged back into this client. This should have zero impact on the way the client works by default. If an endpoint is available in serverless, the PHP function will contains a @group serverless
phpdoc attribute.
If you try to use an endpoint that is not available in serverless you will get a 410
HTTP error with a message as follows:
"this endpoint exists but is not available when running in serverless mode".
The 9.0.0 client can recognize that it is communicating with a serverless instance if you are using a URL managed by Elastic (e.g. *.elastic.cloud
).
If you are using a proxy, the client will be able to recognize that the host is serverless from the first response. Alternatively, you can explicitly indicate that the host is serverless using the Client::setServerless(true)
function (false
by default).You can have a look at the BREAKING_CHANGES file for more information.
If you need to mock the Elasticsearch client you just need to mock a PSR-18 HTTP Client.
For instance, you can use the php-http/mock-client as follows:
use Elastic\Elasticsearch\ClientBuilder;
use Elastic\Elasticsearch\Response\Elasticsearch;
use Http\Mock\Client;
use Nyholm\Psr7\Response;
$mock = new Client(); // This is the mock client
$client = ClientBuilder::create()
->setHttpClient($mock)
->build();
// This is a PSR-7 response
$response = new Response(
200,
[Elasticsearch::HEADER_CHECK => Elasticsearch::PRODUCT_NAME],
'This is the body!'
);
$mock->addResponse($response);
$result = $client->info(); // Just calling an Elasticsearch endpoint
echo $result->asString(); // This is the body!
We are using the ClientBuilder::setHttpClient()
to set the mock client.
You can specify the response that you want to have using the
addResponse($response)
function. As you can see the $response
is a PSR-7
response object. In this example we used the Nyholm\Psr7\Response
object from
the nyholm/psr7 project. If you are using
PHPUnit you can even mock the ResponseInterface
as
follows:
$response = $this->createMock('Psr\Http\Message\ResponseInterface');
Notice: we added a special header in the HTTP response. This is the product
check header, and it is required for guarantee that elasticsearch-php
is
communicating with an Elasticsearch server 8.0+.
For more information you can read the Mock client section of PHP-HTTP documentation.
If something is not working as expected, please open an issue.
You can checkout the Elastic community discuss forums.
We welcome contributors to the project. You can refer to the CONTRIBUTING guide for more information.
Thanks in advance for your contribution! :heart:
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.
Research
Security News
Malicious npm packages posing as developer tools target macOS Cursor IDE users, stealing credentials and modifying files to gain persistent backdoor access.