PHP client

The OpenSearch PHP client provides a safer and easier way to interact with your OpenSearch cluster. Rather than using OpenSearch from a browser and potentially exposing your data to the public, you can build an OpenSearch client that takes care of sending requests to your cluster. The client contains a library of APIs that let you perform different operations on your cluster and return a standard response body.

This getting started guide illustrates how to connect to OpenSearch, index documents, and run queries. For the client source code, see the opensearch-php repo.

Setup

To add the client to your project, install it using composer:

  1. composer require opensearch-project/opensearch-php

copy

To install a specific major version of the client, run the following command:

  1. composer require opensearch-project/opensearch-php:<version>

copy

Then require the autload file from composer in your code:

  1. require __DIR__ . '/vendor/autoload.php';

copy

Connecting to OpenSearch

To connect to the default OpenSearch host, create a client object with the address https://localhost:9200 if you are using the Security plugin:

  1. $client = (new \OpenSearch\ClientBuilder())
  2. ->setHosts(['https://localhost:9200'])
  3. ->setBasicAuthentication('admin', 'admin') // For testing only. Don't store credentials in code.
  4. ->setSSLVerification(false) // For testing only. Use certificate for validation
  5. ->build();

copy

Connecting to Amazon OpenSearch Service

The following example illustrates connecting to Amazon OpenSearch Service:

  1. $client = (new \OpenSearch\ClientBuilder())
  2. ->setSigV4Region('us-east-2')
  3. ->setSigV4Service('es')
  4. // Default credential provider.
  5. ->setSigV4CredentialProvider(true)
  6. // Using a custom access key and secret
  7. ->setSigV4CredentialProvider([
  8. 'key' => 'awskeyid',
  9. 'secret' => 'awssecretkey',
  10. ])
  11. ->build();

copy

Connecting to Amazon OpenSearch Serverless

The following example illustrates connecting to Amazon OpenSearch Serverless Service:

  1. $client = (new \OpenSearch\ClientBuilder())
  2. ->setSigV4Region('us-east-2')
  3. ->setSigV4Service('aoss')
  4. // Default credential provider.
  5. ->setSigV4CredentialProvider(true)
  6. // Using a custom access key and secret
  7. ->setSigV4CredentialProvider([
  8. 'key' => 'awskeyid',
  9. 'secret' => 'awssecretkey',
  10. ])
  11. ->build();

copy

Creating an index

To create an OpenSearch index with custom settings, use the following code:

  1. $indexName = 'test-index-name';
  2. // Create an index with non-default settings.
  3. $client->indices()->create([
  4. 'index' => $indexName,
  5. 'body' => [
  6. 'settings' => [
  7. 'index' => [
  8. 'number_of_shards' => 4
  9. ]
  10. ]
  11. ]
  12. ]);

copy

Indexing a document

You can index a document into OpenSearch using the following code:

  1. $client->create([
  2. 'index' => $indexName,
  3. 'id' => 1,
  4. 'body' => [
  5. 'title' => 'Moneyball',
  6. 'director' => 'Bennett Miller',
  7. 'year' => 2011
  8. ]
  9. ]);

copy

Searching for documents

The following code uses a multi_match query to search for “miller” in the title and director fields. It boosts the documents where “miller” appears in the title field:

  1. var_dump(
  2. $client->search([
  3. 'index' => $indexName,
  4. 'body' => [
  5. 'size' => 5,
  6. 'query' => [
  7. 'multi_match' => [
  8. 'query' => 'miller',
  9. 'fields' => ['title^2', 'director']
  10. ]
  11. ]
  12. ]
  13. ])
  14. );

copy

Deleting a document

You can delete a document using the following code:

  1. $client->delete([
  2. 'index' => $indexName,
  3. 'id' => 1,
  4. ]);

copy

Deleting an index

You can delete an index using the following code:

  1. $client->indices()->delete([
  2. 'index' => $indexName
  3. ]);

copy

Sample program

The following sample program creates a client, adds an index with non-default settings, inserts a document, searches for the document, deletes the document, and then deletes the index:

  1. <?php
  2. require __DIR__ . '/vendor/autoload.php';
  3. $client = (new \OpenSearch\ClientBuilder())
  4. ->setHosts(['https://localhost:9200'])
  5. ->setBasicAuthentication('admin', 'admin') // For testing only. Don't store credentials in code.
  6. ->setSSLVerification(false) // For testing only. Use certificate for validation
  7. ->build();
  8. $indexName = 'test-index-name';
  9. // Print OpenSearch version information on console.
  10. var_dump($client->info());
  11. // Create an index with non-default settings.
  12. $client->indices()->create([
  13. 'index' => $indexName,
  14. 'body' => [
  15. 'settings' => [
  16. 'index' => [
  17. 'number_of_shards' => 4
  18. ]
  19. ]
  20. ]
  21. ]);
  22. $client->create([
  23. 'index' => $indexName,
  24. 'id' => 1,
  25. 'body' => [
  26. 'title' => 'Moneyball',
  27. 'director' => 'Bennett Miller',
  28. 'year' => 2011
  29. ]
  30. ]);
  31. // Search for it
  32. var_dump(
  33. $client->search([
  34. 'index' => $indexName,
  35. 'body' => [
  36. 'size' => 5,
  37. 'query' => [
  38. 'multi_match' => [
  39. 'query' => 'miller',
  40. 'fields' => ['title^2', 'director']
  41. ]
  42. ]
  43. ]
  44. ])
  45. );
  46. // Delete a single document
  47. $client->delete([
  48. 'index' => $indexName,
  49. 'id' => 1,
  50. ]);
  51. // Delete index
  52. $client->indices()->delete([
  53. 'index' => $indexName
  54. ]);
  55. ?>

copy