A third-party PHP client to interface with Elastic Enterpise Search including Elastic App Search and Elastic Workplace Search.
Using this client assumes that you have a Enterprise Search server installed and running.
You can install the client in your project by using composer:
composer require elastic/enterprise-search
After the installation you can use the client as follows:
use Elastic\EnterpriseSearch\Client;
use Elastic\EnterpriseSearch\EnterpriseSearch\Request;
$client = new Client([
'host' => 'http://localhost:3002',
'enterprise-search' => [
'username' => '<insert here the username>',
'password' => '<insert here the password>'
]
]);
$es = $client->enterpriseSearch(); // Enterprise Search endpoints
$result = $es->getVersion(new Request\GetVersion); // Call the getVersion endpoint
echo $result->number; // prints the Enterprise Search version (e.g. 7.11.0)
You can access the result of each endpoints using object or array interface, as follows:
$result = $es->getVersion(new Request\GetVersion);
echo $result->number; // prints the Enterprise Search version (e.g. 7.11.0)
echo $result['number']; // same as above
var_dump($result->asArray()); // returns all the body response as array
var_dump($result->asObject()); // returns all the body response as object
var_dump($result->asString()); // returns all the body response as string (i.e. JSON)
Moreover, you can access the full HTTP response of the result using a PSR-7 object.
$result = $es->getVersion(new Request\GetVersion);
$response = $result->getResponse(); // PSR-7 object
echo $response->getStatusCode(); // e.g. 200
var_dump($response->getHeaders()); // returns all the response headers
This client is versioned and released alongside Enterprise Search.
To guarantee compatibility, use the most recent version of this library within the major version of the corresponding Enterprise Search implementation.
For example, for Enterprise Search 7.4
, use 7.4
of this library or above, but not 8.0
.
The Enterprise Search PHP client provides API for the following Elastic products:
You can consume the Enterprise Search APIs using the Client::enterpriseSearch()
function.
For instance, you can enable the Read-Only mode flag endpoint as follows:
use Elastic\EnterpriseSearch\Client;
use Elastic\EnterpriseSearch\EnterpriseSearch\Request;
use Elastic\EnterpriseSearch\EnterpriseSearch\Schema;
$client = new Client([
'host' => 'http://localhost:3002',
'enterprise-search' => [
'username' => '<insert here the username>',
'password' => '<insert here the password>'
]
]);
$es = $client->enterpriseSearch();
$result = $es->putReadOnly(
new Request\PutReadOnly(
new Schema\ReadOnlyState(true)
)
);
var_dump($result['enabled']); // true
For more information about the Enterprise Search API you can read the official documentation here.
You can consume the App Search APIs using the Client::appSearch()
function.
For instance, you can create an Engine using the following endpoint:
use Elastic\EnterpriseSearch\Client;
use Elastic\EnterpriseSearch\AppSearch\Request;
$client = new Client([
'host' => 'http://localhost:3002',
'app-search' => [
'token' => '<insert here the API-KEY>'
]
]);
$app = $client->appSearch();
// Create a 'test' engine
$result = $appSearch->createEngine(
new Request\CreateEngine(
new Schema\Engine('test')
)
);
var_dump($result->name); // test
var_dump($result->document_count); // 0, since just created
For more information about the App Search API you can read the official documentation here.
You can consume the Workplace Search APIs using the Client::workplaceSearch()
function.
For instance, you can index a document in Workplace Search using the following endpoint:
use Elastic\EnterpriseSearch\Client;
use Elastic\EnterpriseSearch\WorkplaceSearch\Request;
use Elastic\EnterpriseSearch\WorkplaceSearch\Schema;
$client = new Client([
'host' => 'http://localhost:3002',
'workplace-search' => [
'token' => '<insert here the AUTH_TOKEN>'
]
]);
$workplace = $client->workplaceSearch();
// create a new document (id is optional)
$doc = new Schema\Document();
$doc->id = '1234';
$doc->title = "The Meaning of Time";
$doc->body = "Not much. It is a made up thing.";
$result = $workplace->indexDocuments(
new Request\IndexDocuments('<content source ID>', [$doc])
);
var_dump($result->results); // return the results
For more information about the Workplace Search API you can read the official documentation here.
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. Before you begin, a couple notes...
- Before opening a pull request, please create an issue to discuss the scope of your proposal.
- Please write simple code and concise documentation, when appropriate.